About Me

My photo
Cloud Operations Team Leader at Conduit - http://il.linkedin.com/in/shavit

Friday, February 24, 2012

EC2 Instance backup - auto backup for prod


Did you ever wonder how to automatically create a backup to only your prod (or some other) environment on Amazon ec2?

Well this is easy, all you need to do is to add a tag to the instances and set a cron job.

Copy the following script to your manage machine and replace the EC2 certs path ….. with your

------------------------------------------------------------------------------------------------------------ 
#!/bin/bash -x

##           Created by Shavit Netzer                                                  ##
##           This script will run every XXX time depend on you            ##
##           And will start backup depend on the Backup tag                ##

if [ -z $1 ]
 then
  echo "ERROR - need to provide params $0 WHEN[h,d,m]"
  exit
fi

export EC2_HOME=/path/to/your/ec2-api-tools
export JAVA_HOME=/path/to/your/jvm/java-6-sun-1.6.0.26
export EC2_PRIVATE_KEY=/path/to/your/pk-XXXX.pem
export EC2_CERT=/path/to/your/cert-XXXX.pem
export EC2_ID='XXXX'
export EC2_SECRET='XXXX'
export AWS_ELB_HOME=/path/to/your/ElasticLoadBalancing
export AWS_AUTO_SCALING_HOME=/path/to/your/AutoScaling
export AWS_CLOUDWATCH_HOME=/path/to/your/CloudWatch

listofinstances=""

if [ "$1" == "h" ]
 then
  listofinstances=`/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-describe-instances --region us-east-1 | sed "s#\t#,#g;" | grep ^TAG | grep ",Backup," | grep h$`
fi
if [ "$1" == "d" ]
 then
  listofinstances=`/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-describe-instances --region us-east-1 | sed "s#\t#,#g;" | grep ^TAG | grep ",Backup," | grep d$`
fi
if [ "$1" == "m" ]
 then
  listofinstances=`/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-describe-instances --region us-east-1 | sed "s#\t#,#g;" | grep ^TAG | grep ",Backup," | grep m$`
fi

if [ "$listofinstances" != "" ]
 then
  tagnames=`/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-describe-instances --region us-east-1 | sed "s#\t#,#g;" | grep ^TAG | grep ",Name,"`
  for instance in $listofinstances
   do
    instanceid=`echo $instance | awk -F, '{print $3}'`
    for tname in $tagnames
     do
      instancetag=`echo $tname | grep $instanceid`
      if [ "$instancetag" != "" ]
       then
                                echo "=== start $instanceid ==="
                                DATE=`date +%Y%m%d%H`
                                instancetagname=`echo $instancetag | awk -F, '{print $5}'`
                                underline="_"
                                echo "Create backup for instance $instanceid with name $instancetagname"
                                echo "/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-create-image $instanceid --name $DATE$underline$instancetagname --description $DATE$underline$instancetagname --no-reboot"
                                ami1=`/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-create-image $instanceid --name $DATE$underline$instancetagname --description $DATE$underline$instancetagname --no-reboot`
                                echo $ami1 | sed -e 's/IMAGE//' -e 's/ //g'
                                ami=`echo $ami1 | sed -e 's/IMAGE//' -e 's/ //g'`
                                echo "Adding TAG for the backup"
                                echo "/usr/src/ec2-api-tools-1.4.4.2/bin/ec2-create-tags $ami --tag Name=$DATE$underline$instancetagname"
                                /usr/src/ec2-api-tools-1.4.4.2/bin/ec2-create-tags $ami --tag Name=$DATE$underline$instancetagname
                                echo "=== end $instanceid ==="
      fi
    done
  done
fi

1 comment:

  1. Nice blog... Automatic backup EC2 instance is really very useful and this post shows its importance. I found script very useful. Thanks for sharing.

    ReplyDelete