ec2-delete-snapshot script

2009 July 26
tags: ec2
by Josh Hand

As part of Amazon's ec2 offering you can take advantage of their Elastic Block Store's. An Elastic Block Store is storage that persists independently from the life of an instance. One of the many advantages this gives you over and above persistent storage is it gives you the ability to take a snapshot of the store at any given time. Over time with multiple stores this can add up to quite a few snapshots. Your limited to 500 snapshots and it can be a painstaking task to clear these out once you reach the limit. So I decided to write a script to do the work for me.

Running ec2-describe-snapshots gives you a listing of your snapshots in chronological order, like so:

  SNAPSHOT snap-5c27e636 vol-99c96cf2 completed 2009-06-04T23:08:06+0000 100%
SNAPSHOT snap-5027e640 vol-bff814d8 completed 2009-06-04T23:08:19+0000 100%
SNAPSHOT snap-9c30f0f6 vol-99c96cf2 completed 2009-06-07T23:55:59+0000 100%
SNAPSHOT snap-9330f0fb vol-bff814d8 completed 2009-06-07T23:56:09+0000 100%

If you copy all the records from the output that you want to delete and put them in a file called snaps, you can run the following to turn this into a delete script:

cat snaps | sed s/SNAPSHOT/ec2-delete-snapshot/g | sed -e 's/vol.*//g' | sed '/^ec2/G' | sed 's/^$/sleep 5/g' > del.sh

This creates a file called del.sh, if you execute this script it will delete all the snapshots for you.

The script from the above output looks like so:

ec2-delete-snapshot   snap-5c27e636   
sleep 5
ec2-delete-snapshot   snap-5027e640   
sleep 5
ec2-delete-snapshot   snap-9c30f0f6   
sleep 5
ec2-delete-snapshot   snap-9330f0fb

2 Responses leave one →
  1. gmc dealer indianapolis
    Nov 24, 2009 at 3:39 AM

    Nice Post I already digged this

  1. David Gildeh
    Mar 12, 2010 at 12:10 PM

    I wanted to keep the last N snapshots and find an easy way to automate this so I wrote some shell/PHP scripts to run with CRON:

    http://www.sambastream.com/blogs/dgildeh/12-03-10/implementing-revolving-backups-aws-ec2

    Hope someone finds this useful!

Leave a Reply