Skip to content

Instantly share code, notes, and snippets.

@yemster
Forked from nguyendangminh/snapshotter.py
Created August 19, 2019 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemster/0ada7bf759f114b5588b8600c1be2fab to your computer and use it in GitHub Desktop.
Save yemster/0ada7bf759f114b5588b8600c1be2fab to your computer and use it in GitHub Desktop.
AWS EBS snapshotter
#!/usr/bin/env python
# Shameless copy from qwiklab
import boto.ec2, os, datetime
MAX_SNAPSHOTS = 2 # Number of snapshots to keep
# Connect to EC2 in this region
region = os.environ.get('EC2_REGION')
connection = boto.ec2.connect_to_region(region)
# Get a list of all volumes
volumes = connection.get_all_volumes()
# Cteate a snapshot of each volume
for v in volumes:
connection.create_snapshot(v.id, description = str(datetime.datetime.now()))
# Too many snapshots?
snapshots = v.snapshots()
if len(snapshots) > MAX_SNAPSHOTS:
# Delete oldest snapshots, but keep MAX_SNAPSHOTS available
snap_sorted = sorted([(s.id, s.start_time) for s in snapshots], key=lambda k: k[1])
for s in snap_sorted[:-MAX_SNAPSHOTS]:
print "Deleting snapshot", s[0]
connection.delete_snapshot(s[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment