Skip to content

Instantly share code, notes, and snippets.

@dlively1
Created January 10, 2013 21:14
Show Gist options
  • Save dlively1/4505862 to your computer and use it in GitHub Desktop.
Save dlively1/4505862 to your computer and use it in GitHub Desktop.
Amazon EC2 EBS volume snapshot
import boto
# variable settings for instance
snapshots = [("vol-ID", 7)]
instanceID = 'ID'
description = "automated backup"
print "Starting Snapshot Process"
#credentials from boto.cfg
ec2 = boto.connect_ec2()
instance = ec2.get_all_instances([instanceID])[0].instances[0]
for volume, keep_days in snapshots:
snaps = ec2.get_all_snapshots(filters={"volume-id":volume, "description": description})
print "%s: Creating Snapshot. %s automated snapshots currently exist. " % (volume, len(snaps))
ec2.create_snapshot(volume, description)
oldSnaps = sorted(snaps, key=lambda x: x.start_time)[:-keep_days]
print "Deleting snapshots for %s > %s: %s" % (volume, keep_days, oldSnaps)
for snap in oldSnaps:
ec2.delete_snapshot(snap.id)
print "Snapshot process finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment