Skip to content

Instantly share code, notes, and snippets.

@andrewjesaitis
Created July 14, 2015 16:51
Show Gist options
  • Save andrewjesaitis/b840e9b0b370a09d700c to your computer and use it in GitHub Desktop.
Save andrewjesaitis/b840e9b0b370a09d700c to your computer and use it in GitHub Desktop.
Simple AWS Snapshoting
#!/bin/bash
#set -x #uncomment to watch commands execute
#install the latest version of aws cli via pip -- pip install awscli -- note this install to /usr/local/bin and there maybe an old version of awscli in /usr/bin
#run $aws configure to set access key, secret key and region, script require output type to be text
#Finally, install ntpd -- apt-get install ntp -- because it often isn't install and if your server's clock is not sync'd with amazon's aws cli commands fail
#Run this via cron every night at 12:05 am -- crontab -e -- 0 5 * * * /path/to/backupEbs.sh >> /var/log/backup.log 2>&1
NUMBER_TO_KEEP=7
EBS_VOLUMES=( vol-XXXXX vol-XXXXX vol-XXXXX vol-XXXXX )
/usr/local/bin/aws ec2 create-snapshot --volume-id ${EBS_VOLUMES[0]} --description "Snapshot $(date +'%Y-%m-%d %H:%M:%S')"
/usr/local/bin/aws ec2 create-snapshot --volume-id ${EBS_VOLUMES[1]} --description "Snapshot $(date +'%Y-%m-%d %H:%M:%S')"
/usr/local/bin/aws ec2 create-snapshot --volume-id ${EBS_VOLUMES[2]} --description "Snapshot $(date +'%Y-%m-%d %H:%M:%S')"
/usr/local/bin/aws ec2 create-snapshot --volume-id ${EBS_VOLUMES[3]} --description "Snapshot $(date +'%Y-%m-%d %H:%M:%S')"
sleep 2h #big volumes can take a while to be created
for EBS_VOLUME in ${EBS_VOLUMES[@]}; do
/usr/local/bin/aws ec2 describe-snapshots | grep $EBS_VOLUME | sort -r -k 11 | sed 1,$(echo $NUMBER_TO_KEEP)d | awk '{print "Deleting snapshot: \
" $10 " Dated:" $11}; system("/usr/local/bin/aws ec2 delete-snapshot --snapshot-id " $10 )'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment