Skip to content

Instantly share code, notes, and snippets.

@nguyendangminh
Last active October 9, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nguyendangminh/6669d8ddf3b09427a949a7869f652cc0 to your computer and use it in GitHub Desktop.
Save nguyendangminh/6669d8ddf3b09427a949a7869f652cc0 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