Skip to content

Instantly share code, notes, and snippets.

@sap1ens
Last active November 18, 2020 06:01
Show Gist options
  • Save sap1ens/03d9fbe8b58104f56f64 to your computer and use it in GitHub Desktop.
Save sap1ens/03d9fbe8b58104f56f64 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
volume_id="DEFINE_ME"
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"
@trevoro
Copy link

trevoro commented Feb 19, 2015

Looks clean.
If I was being nitpicky i'd say stick to 80 character width and write a function to make it easier to write to your logfile.

log_info() {
  echo "$@" >> $logfile
}

log_info "Not deleting snapshot $snapshot_id..."

@sap1ens
Copy link
Author

sap1ens commented Feb 19, 2015

Ok, thanks! I'll do that

@asgim
Copy link

asgim commented Jun 22, 2016

Thanks for the script.
What if I wanted to backup multiple volumes, could you please provide the code to do so.

@imkishre
Copy link

Can you please let me know how to create multiple volumes from multiple snapshots?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment