Skip to content

Instantly share code, notes, and snippets.

@jstaffans
Forked from sap1ens/ebs-snapshot-for-volume.sh
Last active February 25, 2019 14:14
Show Gist options
  • Save jstaffans/f6812e76bf6c63c68f91df40f99077d2 to your computer and use it in GitHub Desktop.
Save jstaffans/f6812e76bf6c63c68f91df40f99077d2 to your computer and use it in GitHub Desktop.
Bash script for Automatic EBS Volume Snapshots and Cleanup on Amazon Web Services. Original - https://github.com/CaseyLabs/aws-ec2-ebs-automatic-snapshot-bash
#!/bin/bash
# Original was modified to do backup only for one specified volume.
set -ue
set -o pipefail
export PATH=$PATH:/usr/local/bin/:/usr/bin
## START SCRIPT
# Set Variables
instance_id="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`"
volume_id="TODO: get based on instance_id"
# something like:
# volume_id=`aws ec2 describe-volumes --filters Name=attachment.instance-id,Values="$instance_id" | jq ...`
today=`date +"%m-%d-%Y"+"%T"`
logfile="/var/log/ebs-snapshot.log"
# How many days do you wish to retain backups for? Default: 7 days
retention_days="7"
retention_date_in_seconds=`date +%s --date "$retention_days days ago"`
log_info() {
echo "$@" >> $logfile
}
# Start log file: today's date
log_info $today
# Take a snapshot of the volume
description="$(hostname)-backup-$(date +%Y-%m-%d)"
log_info "Volume ID is $volume_id"
# Next, we're going to take a snapshot of the current volume, and capture the resulting snapshot ID
snapresult=$(aws ec2 create-snapshot --output=text --description $description --volume-id $volume_id --query SnapshotId)
log_info "New snapshot is $snapresult"
# And then we're going to add a "CreatedBy:AutomatedBackup" tag to the resulting snapshot.
# Why? Because we only want to purge snapshots taken by the script later, and not delete snapshots manually taken.
aws ec2 create-tags --resource $snapresult --tags Key=CreatedBy,Value=AutomatedBackup
# Get all snapshot IDs associated with each volume attached to this instance
rm /tmp/snapshot_info.txt --force
aws ec2 describe-snapshots --output=text --filters "Name=volume-id,Values=$volume_id" "Name=tag:CreatedBy,Values=AutomatedBackup" --query Snapshots[].SnapshotId | tr '\t' '\n' | sort | uniq >> /tmp/snapshot_info.txt 2>&1
# Purge all instance volume snapshots created by this script that are older than 7 days
for snapshot_id in $(cat /tmp/snapshot_info.txt)
do
log_info "Checking $snapshot_id..."
snapshot_date=$(aws ec2 describe-snapshots --output=text --snapshot-ids $snapshot_id --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}')
snapshot_date_in_seconds=`date "--date=$snapshot_date" +%s`
if (( $snapshot_date_in_seconds <= $retention_date_in_seconds )); then
log_info "Deleting snapshot $snapshot_id ..."
aws ec2 delete-snapshot --snapshot-id $snapshot_id
else
log_info "Not deleting snapshot $snapshot_id ..."
fi
done
# One last carriage-return in the logfile...
log_info ""
echo "Results logged to $logfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment