Automated snapshot backup of an Amazon EBS volume

I found the following Python script online, but it didn’t really work too well :

http://aws-musings.com/manage-ebs-snapshots-with-a-python-script/

EBS – Elastic Block Storage …

I had to easy_install boto, to get it to work.

I’m not sure the Debian python-boto package in Lenny is up to date.

Anyway, $server now has :

from boto.ec2.connection import EC2Connection
from boto.ec2.regioninfo import RegionInfo

from datetime import datetime
import sys

# Substitute your access key and secret key here
aws_access_key = 'MY_AWS_ACCESS_KEY'
aws_secret_key = 'MY_AWS_SECRET_KEY'
# Change to your region/endpoint...
region = RegionInfo(endpoint='eu-west-1.ec2.amazonaws.com', name='eu-west-1')

if len(sys.argv) < 3:
    print "Usage: python manage_snapshots.py volume_id number_of_snapshots_to_keep description"     
    print "volume id and number of snapshots to keep are required. description is optional"
    sys.exit(1) 

vol_id = sys.argv[1] 
keep = int(sys.argv[2]) 
conn = EC2Connection(aws_access_key, aws_secret_key, region=region) 
volumes = conn.get_all_volumes([vol_id]) 
print "%s" % repr(volumes) 
volume = volumes[0] 
description = 'Created by manage_snapshots.py at ' + datetime.today().isoformat(' ') 
if len(sys.argv) > 3:
    description = sys.argv[3]

if volume.create_snapshot(description):
    print 'Snapshot created with description: ' + description

snapshots = volume.snapshots()
snapshot = snapshots[0]

def date_compare(snap1, snap2):
    if snap1.start_time < snap2.start_time:
        return -1
    elif snap1.start_time == snap2.start_time:
        return 0
    return 1

snapshots.sort(date_compare)
delta = len(snapshots) - keep
for i in range(delta):
    print 'Deleting snapshot ' + snapshots[i].description
    snapshots[i].delete()

And then plonk something like the following in /etc/cron.daily/backup_ebs :

for volume in vol-xxxx vol-yyyyy vol-zzzz
do
	/path/to/above/python/script.py $volume 7 "Backup of $volume on $(date +%F-%H:%m)"
done

Which keeps 7 backups for each volume with a time/date stamp in each description.

9 thoughts on “Automated snapshot backup of an Amazon EBS volume”

  1. Nifty 🙂

    A slightly more Pythonic thing to do instead of the date_compare function to sort would be to use:
    from operator import attrgetter
    #...
    snapshots.sort(key=attrgetter("start_time"))

    And your for-loop could be:

    for snapshot in snapshots[:-keep]:

  2. There’s a problem with the script as it is listed: in the middle of the script, there are a whole bunch of lines that are missing newlines, and so they’re together in a single line.

  3. Thank you for sharing your script!

    Could you share a bit more about what was not good with the script in reference please?
    I’m trying it out and if it doesn’t work, I’m gonna try yours 🙂

  4. Eric – I suspect I wasn’t able to get it installed / working… sorry, it’s been some time since I’ve done anything with EC2/EBS so can’t comment more.

  5. Hi David,

    Thanks for replying!

    I’ve tried out the script from aws-musings.com and got “volume does not exist” error.
    Then I tried your script and that problem went away.
    I’ve only got “socket.gaierror: [Errno -2] Name or service not known”

    I’m supposed I’m much closer now, thanks to you 🙂

  6. Hey.

    I change it a little bit to run automatically backup all volumes instead of giving the script the volume id’s..
    It use default 4 snapshots to keep

    from boto.ec2.connection import EC2Connection
    from datetime import datetime
    import sys

    def date_compare(snap1, snap2):
    if snap1.start_time < snap2.start_time:
    return -1
    elif snap1.start_time == snap2.start_time:
    return 0
    return 1

    # Substitute your access key and secret key here
    aws_access_key = 'your_access_key'
    aws_secret_key = 'your_secret_key'

    #number of snapshoot to keep.
    keep = 4
    # connect to amazon mws service.
    conn = EC2Connection(aws_access_key, aws_secret_key)

    volumes = conn.get_all_volumes()
    for volume in volumes:
    description = 'manage_snapshots.py at ' + datetime.today().isoformat(' ')

    if volume.create_snapshot(description):
    print 'Snapshot created with description: ' + description

    snapshots = volume.snapshots()
    snapshots.sort(date_compare)
    delta = len(snapshots) – keep
    for i in range(delta):
    print 'Deleting snapshot ' + snapshots[i].volume_id
    snapshots[i].delete()

  7. region endpoint string is wrong. instead of using “eu-west-1.ec2.amazonaws.com”, please use “ec2.eu-west-1.amazonaws.com”

Leave a Reply

Your email address will not be published. Required fields are marked *