Skip to content

Instantly share code, notes, and snippets.

@drei01
Created September 15, 2015 16:05
Show Gist options
  • Save drei01/6410d17249c612649ef3 to your computer and use it in GitHub Desktop.
Save drei01/6410d17249c612649ef3 to your computer and use it in GitHub Desktop.
Before running this script, add the following to your ~/.bashrc
#!/bin/bash
# initialize the EC2 environment
[ -e /etc/profile.d/aws-apitools-common.sh ] && source /etc/profile.d/aws-apitools-common.sh
# load the access keys
source ~/.bashrc
# locate the instance ID of this VM
instanceid="$(ec2-describe-instances --region $AWS_REGION | grep $(hostname -f) | cut -f2 | head -1)"
if [ -z "$instanceid" ]; then
echo "Failed to locate instance ID" >&2
exit 5
fi
# locate the EBS volume ID for the root file system
rootvolumeid="$(ec2-describe-volumes --region $AWS_REGION | grep "$instanceid" | grep $(mount | egrep 'on / type' | cut -d' ' -f1 | sed -e 's/xvd/sd/;') | cut -f2)"
if [ -z "$rootvolumeid" ]; then
echo "Failed to locate root volume ID" >&2
exit 5
fi
# create the snapshot
snapshotid="$(ec2-create-snapshot --region $AWS_REGION -d "backup-${instanceid}-$(date +"%Y%m%d")" $rootvolumeid | cut -f2)"
if [ -z "$snapshotid" ]; then
echo "Failed to take snapshot" >&2
exit 4
fi
# wait for the snapshot to complete
while [ "$(ec2-describe-snapshots --region $AWS_REGION | grep "$snapshotid" | cut -f4)" != "completed" ]; do
sleep 5
done
# delete old snapshots
ec2-describe-snapshots | grep "$rootvolumeid" | cut -f2 | grep -v "$snapshotid" | while read snapid; do
if ! ec2-delete-snapshot "$snapid" > /dev/null; then
echo "Failed to delete old snapshot $snapid" >&2
exit 4
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment