Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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 stdin
INSTANCE="$1"    
DESCR="$2"

if [  $1 -a $2 ]
  then

  # Get attached volumes for specific instance
  TMPFILE="/tmp/`date +%N`"
  ec2-describe-volumes | grep $INSTANCE | awk '{print $2}' > $TMPFILE

  while 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 1
  
else
  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