Friday, April 20, 2012

Bash Script to Schedule the Creation Snapshots for EC2 Volumes

Glenn's Wimpy Disclaimer

Usually I code in either Java, Groovy or Scala, however this was written in bash.  There was one reason for this:  No one else in my company can support any of the languages above easily.   
And as a DevOp I thought this could be useful to others.  With that being said please add comments on what it would take to make the script a little more professional.


What does it do?

In short this script should be launched daily by CRON and it will look for tagged volumes backup the volume based on the date associated with the tag.


Initializing the code

#!/bin/bash
echo "starting volume backup"
#The interval between each backup
dayCount=7
#The date the backups should removed.
EXPIRE_DATE=$(date -d "+$dayCount day"  +%Y%m%d)
#The backup activate date.  i.e. any volume with a date older than this will be backed up.
START_DATE=$(date -d "-$dayCount day"  +%Y%m%d)
#Todays Date.
CURRENT_DATE=$(date +%Y%m%d);


The code above is fairly simple initialize number of days between backups as well as establish the backup expire date and the date the volume should be backed up.


Primary Loop

# get all the volumes and review each one searching for the backupdate tag
ec2-describe-volumes | while read line
do
#get volume id
 volume=$(echo $line|cut -f3 -d' ')
#Get the backup key (if present)
 key=$(echo $line|cut -f4 -d' ')
#get the backup date
 value=$(echo $line|cut -f5 -d' ')
#if a backup tag is present for the volume check to see if its date signifies whether we should backup or not.
 if [ "$key" == "backupdate" ]
 then
  let vart=$value-$START_DATE
# should we backup the volume
  if [ $vart -lt 0 ]
  then
    echo "Backing up $volume"
    echo "for date $value"
    echo "new date $DATE"
#create the snapshot (backup) of the volume
    snapresult=$(ec2-create-snapshot $volume -d \"backup$DATE\")
#get the snapshot Id
    snapshot=$(echo $snapresult|cut -f2 -d' ')
#update the backupdate flag with the current date on the volume
    ec2-create-tags $volume --tag backupdate="$CURRENT_DATE"
#tag snapshot with metadata with the information on when to remove the snapshot
    ec2-create-tags $snapshot --tag backupdate="$EXPIRE_DATE"
    ec2-create-tags $snapshot --tag Name=$EXPIRE_DATE
  fi
 fi
# if no more entries exit the loop
 if [ -z "$line" ]
 then
   exit
 fi
done
echo "Finished volume backup" 

The comments in the code above should explain how volumes are acquired as well as how snapshots are created and tagged.

Once I finished the code above I added it to a CRON job that runs at 5:00 am every morning.

Hope this helps you out a little,
--Glenn