Amazon EC2 lets you create snapshots of the EBS volumes using the Amazon Console or the EC2 Java-based command-line tools. The Console works fine until you have to manage a decent amount of volumes or need frequent snapshots. As for the Java-based command-line tools, well, they are Java-based...
Another alternative is to use the .NET API so you can customize what volumes are backed up and how often, the retention criteria, and anything you want while using your favorite programming language. You have to start by downloading the AWS dll from Amazon. Click on Download the DLL and Samples.
The following program takes a snapshot of all the volumes that are in use in your account. The snapshot name matches the name of the source volume:
using System; using System.Configuration; using System.Collections.Generic; using System.Net; using System.Linq; using Amazon; using Amazon.EC2; using Amazon.EC2.Model; namespace AWSBackup { class Program { static void Main (string [] args) { // The following line is not needed in .NET. // In Mono, it is not needed if you trust the AWS certificate chain // See http://stackoverflow.com/questions/3285489/mono-problems-with-cert-and-mozroots ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { return true; }; var volumes = GetVolumes (); foreach (var v in volumes) { if (String.IsNullOrEmpty (v.VolumeId) || String.CompareOrdinal (v.Status, "in-use") != 0) continue; var name = v.Tag.FirstOrDefault (t => t.Key == "Name").Value; var snapshot = CreateSnapshot (v, name); Console.WriteLine ("Snapshot of {0} initiated at {1}", name, snapshot.StartTime); } } static IEnumerable<Volume> GetVolumes () { AmazonEC2 ec2 = GetEC2Client (); var request = new DescribeVolumesRequest (); var response = ec2.DescribeVolumes (request); return response.DescribeVolumesResult.Volume; } static Snapshot CreateSnapshot (Volume v, string name) { AmazonEC2 ec2 = GetEC2Client (); var request = new CreateSnapshotRequest () .WithVolumeId (v.VolumeId) .WithDescription (name); var response = ec2.CreateSnapshot (request); var snapshot = response.CreateSnapshotResult.Snapshot; if (!String.IsNullOrEmpty (name)) { ec2.CreateTags (new CreateTagsRequest () .WithResourceId (snapshot.SnapshotId) .WithTag (new Tag { Key = "Name", Value = name }) ); } return snapshot; } static AmazonEC2 GetEC2Client () { var settings = ConfigurationManager.AppSettings; return AWSClientFactory.CreateAmazonEC2Client (settings ["AWSAccessKey"], settings ["AWSSecretKey"]); } } }
...And that's it! Instead of a shell script that executes several programs per volume, we have a single program that runs on both MS.NET and Mono. Add it to your crontab or the Windows Task Scheduler and you get automatic backups for your volumes. Automatic deletion of old backups is left as an exercise for the reader.
C# source, application configuration file, and Makefile can be downloaded from here: ec2-backup.zip.