#!/bin/bash## Created by Jason Buscema <jbuscema@lambesis.com>## Create snapshots of the attached EBS volumes for a specific instance. Script# will look for ALL volumes assigned to an instance and create snapshots of# them.# You will need to have the Amazon EC2 command line tools installed in# order for this script to work. Get them from http://aws.amazon.com/developertools/351# Usage# Call this script with an instance name and a readable name. IE:# ./backup-s3.sh [instance] [readable name]# ./backup-s3.sh i-123456 MyServer# Define our exports we'll need for the tools to work. There are better# ways of doing this.
export EC2_HOME=/root/.ec2
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=$EC2_HOME/pk-myprivatekeynamegoeshere.pem
export EC2_CERT=$EC2_HOME/cert-mycertnamegoeshere.pem
export PATH=~/.ec2/bin:$PATH
export JAVA_HOME=/usr/java/default
export PATH=$PATH:/usr/java/default
# Which instance are working with? Read from stdinINSTANCE="$1"DESCR="$2"if [ $1-a $2 ]
then # Get attached volumes for specific instanceTMPFILE="/tmp/`date +%N`"
ec2-describe-volumes | grep $INSTANCE| awk '{print $2}'>$TMPFILEwhile read line
do
echo -e "Creating snapshot for - $DESCR for $INSTANCE from $line (via `hostname`)"
ec2-create-snapshot $line-d "$DESCR snap for $INSTANCE from $line (via `hostname`)"
done <$TMPFILE # Remove our tmpfile
rm -rf $TMPFILE
exit 1else
echo "> ERROR - Need to pass in the instance ID and a name separated by a space. IE: 'backup-s3.sh i-1234567 Name'"
exit 1
fi