Automatic Elastic Block snapshots with a cron job
Amazon EC2 really is amazing, and the Elastic Block storage is pretty darn good too, however I wish you could automate the snapshot process form the EC2 console.
It is however pretty easy to do yourself using a simple cron job.
Before you start make sure you have a JRE:
sudo apt-get install sun-java6-jre
You will also need the EC2 API tools:
wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip unzip ec2-api-tools.zip
At this point you should make a metal note of where you unzipped the api tools to.
Now that you have the prerequisites you need the following simple script:
#!/bin/bash export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre/ export EC2_HOME=/root/ec2-api-tools-1.3-42584 export EC2_PRIVATE_KEY=/data/misc/pk-Umbongo.pem export EC2_CERT=/data/misc/cert-Umbongo.pem export AWS_ACCESS_KEY_ID="Twinkle Twinkle Little star" export AWS_SECRET_ACCESS_KEY="If your happy and your know it, clap your hands" $EC2_HOME/bin/ec2-create-snapshot vol-999999
Obviously you need to specify your volume id where i have vol-999999
Once you have modified this file to be executable you are ready to test it.
chmod +x snapshot.sh ./snapshot.sh
Once your have ran it go to the EC2 Console and verify that the snapshot process has started.
And thats it, the first time you run this script the snapshot will take a while to complete but the next one will be much quicker as the snapshot process is incremental, so only the changes since the last snapshot will be read.
Oh, don’t forget to create a cron job for this (*/5 * * * * /path/snapshot/sh)
