Skip to content

Instantly share code, notes, and snippets.

@okochang
Created October 4, 2012 09:12
Show Gist options
  • Save okochang/3832436 to your computer and use it in GitHub Desktop.
Save okochang/3832436 to your computer and use it in GitHub Desktop.
create new snapshot and delete old snapshot
#!/bin/sh
##Setup
TODAY=`date +\%Y\%m\%d`
INSTANCE_NAME="set your instance name"
LOG="path to your log file"
EBS_VOLUME="set your ebs id"
EC2_REGION=ap-northeast-1
EBS_GENERATION=5
TRANS_ADDRESS="set your email address"
export export JAVA_HOME="set your java home"
export EC2_HOME="set your ec2-api-tools install dir"
export PATH=$PATH:$EC2_HOME/bin:$AWS_RDS_HOME/bin:$JAVA_HOME/bin
export EC2_CERT="set your ec2_cert path"
export EC2_PRIVATE_KEY="set your ec2_privatekey path"
export EC2_URL=https://ec2.ap-northeast-1.amazonaws.com
## start backup
echo "start backup" > ${LOG}
echo `date +\%Y\%m\%d` >> ${LOG}
##create snapshot
ec2-create-snapshot $EBS_VOLUME -d $INSTANCE_NAME-$TODAY
CREATE_CHECK=$?
##sleep
sleep 5m
##check snapshot
SNAPSHOTS=`ec2-describe-snapshots \
| grep -e $EBS_VOLUME | sort -k5 -r | awk '{print $2}'`
SNAP_CHECK=$?
##
COUNT=1
for SNAPSHOT in $SNAPSHOTS; do
if [ $COUNT -le $EBS_GENERATION ]; then
echo $SNAPSHOT "remained"
else
ec2-delete-snapshot \
-K $EC2_PRIVATE_KEY \
-C $EC2_CERT \
--region $EC2_REGION \
$SNAPSHOT
echo $SNAPSHOT "deleted"
fi
COUNT=`expr $COUNT + 1`
done
DELETE_CHECK=$?
## Backup log
ec2-describe-snapshots |grep -e $EBS_VOLUME | sort -k5 -r >> ${LOG}
## Backup Report
if [ ${CREATE_CHECK} = 0 -a ${SNAP_CHECK} = 0 -a ${DELETE_CHECK} = 0 ]
then
echo "backup Succeeded" >> ${LOG}
cat ${LOG} | mail -s "Backup Succeeded" ${TRANS_ADDRESS}
else
echo "backup Failed" >> ${LOG}
cat ${LOG} | mail -s "Backup Failed" ${TRANS_ADDRESS}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment