I'm running an EBS-backed instance which acts as a software development team's build server (running Jenkins and host of other services). The server is running Linux (latest Ubuntu from the official AMIs).
I'd like to take regular, automated snapshots of the instance's associated EBS volume. I only need to keep one latest backup (i.e. old snapshots should be pruned), and a good frequency would be once a day.
It seems that Amazon does not provide such backup service out of the box, so you have to either go with 3rd party scripts or roll your own solution.
My question is, what is the simplest way to achieve this? I'd like a minimal amount of hassle, configuration, and external dependencies. Setting this up as some kind of timed script on the Linux box itself is, to my knowledge, a valid option.
Based on Jonik's concept, I created a python script using boto. You provide it a list of volumes to snapshot, and how many trailing snapshots to keep for each volume:
I set this up as Jenkins job (via the Python plugin), configured to run daily. If you are using IAM to manage credentials, note that this will require in ec2 policies: DescribeRegions, DescribeVolumes, CreateSnapshot, DeleteSnapshot, DescribeSnapshots, CreateTags (because of boto's implementation).
Okay, for what it's worth, here's what I did. I hope my feeble scripts encourage people to post better solutions!
I wrote two simple bash scripts and automated them using cron. (For now I run these on a local server, as I think (?) it's not recommended to put AWS's certificates in the instances/AMIs/EBSs themselves.)
To create a new snapshot:
To prune all except latest snapshot:
(This parses appropriate snapshot information from
ec2-describe-snapshots
output and creates a temp file with [timestamp tab snapshot-id] entries (e.g.2011-06-01T10:24:36+0000 snap-60507609
) where the newest snapshot is on the last line.)Notes:
--region
with all commands. Otherwise e.g.ec2-create-snapshot
would fail with volume ID being unknown. (YMMV if you use the default region "us-east-1".)Disclaimer: This became partly an exercise in Bash/Unix programming for me, especially the prune script. I readily admit you'd most likely get a much clearer result with e.g. Python, when you need logic like "do something for all but the last item in a list". And even with Bash you could probably do this more elegantly (for instance, you don't really need temp files). So, please feel free to post other solutions!
If you're open to external utilities, check out Skeddly.
Disclosure: I'm the CEO of Eleven41 Software, the company behind Skeddly.
I expanded on the idea of Jonik's script to allow multiple snapshots to be retained. The code is too long to fit in a comment so I'm adding a new answer. This code assumes all the right environment variables have been set up for the CLI tools. Also, this defaults to taking a snapshot of the current instance.
I have written a script in PHP that will automate EBS Snapshots and delete old ones. It will even email the results of the snapshots to you. You must configure the AWS PHP SDK and PHPMailer for email functionality, but both of these steps are pretty easy. Then you just run the script every night with CRON or Windows Scheduled Tasks. Detailed instructions and code can be found on my blog:
http://www.caleblloyd.com/software/automatically-take-ebs-snapshots-and-delete-old-ones-with-php-script/