2012-09-08 01:54:15 +00:00
#!/bin/bash -
2014-04-12 21:21:28 +00:00
# Date: 2014-04-12
# Version 0.10
2012-09-08 01:54:15 +00:00
# License Type: GNU GENERAL PUBLIC LICENSE, Version 3
2014-04-12 21:21:28 +00:00
# Author:
# Colin Johnson / https://github.com/colinbjohnson / colin@cloudavail.com
# Contributors:
# Alex Corley / https://github.com/anthroprose
# Jon Higgs / https://github.com/jonhiggs
# Mike / https://github.com/eyesis
# Jeff Vogt / https://github.com/jvogt
# Dave Stern / https://github.com/davestern
# Josef / https://github.com/J0s3f
# buckelij / https://github.com/buckelij
2012-09-08 01:54:15 +00:00
#confirms that executables required for succesful script execution are available
prerequisite_check( )
{
2012-09-24 23:34:15 +00:00
for prerequisite in basename ec2-create-snapshot ec2-create-tags ec2-describe-snapshots ec2-delete-snapshot date
2012-09-08 01:54:15 +00:00
do
#use of "hash" chosen as it is a shell builtin and will add programs to hash table, possibly speeding execution. Use of type also considered - open to suggestions.
hash $prerequisite & > /dev/null
if [ [ $? = = 1 ] ] #has exits with exit status of 70, executable was not found
then echo " In order to use `basename $0 `, the executable \" $prerequisite \" must be installed. " 1>& 2 ; exit 70
fi
done
}
#get_EBS_List gets a list of available EBS instances depending upon the selection_method of EBS selection that is provided by user input
2012-09-21 02:04:51 +00:00
get_EBS_List( )
2012-09-08 01:54:15 +00:00
{
case $selection_method in
volumeid)
if [ [ -z $volumeid ] ]
then echo " The selection method \"volumeid\" (which is $app_name 's default selection_method of operation or requested by using the -s volumeid parameter) requires a volumeid (-v volumeid) for operation. Correct usage is as follows: \"-v vol-6d6a0527\",\"-s volumeid -v vol-6d6a0527\" or \"-v \"vol-6d6a0527 vol-636a0112\"\" if multiple volumes are to be selected. " 1>& 2 ; exit 64
fi
ebs_selection_string = " $volumeid "
; ;
2014-04-12 21:21:28 +00:00
tag)
2012-09-08 01:54:15 +00:00
if [ [ -z $tag ] ]
2012-09-21 02:04:51 +00:00
then echo "The selected selection_method \"tag\" (-s tag) requires a valid tag (-t key=value) for operation. Correct usage is as follows: \"-s tag -t backup=true\" or \"-s tag -t Name=my_tag.\"" 1>& 2 ; exit 64
2012-09-08 01:54:15 +00:00
fi
ebs_selection_string = " --filter tag: $tag "
; ;
*) echo "If you specify a selection_method (-s selection_method) for selecting EBS volumes you must select either \"volumeid\" (-s volumeid) or \"tag\" (-s tag)." 1>& 2 ; exit 64 ; ;
esac
#creates a list of all ebs volumes that match the selection string from above
ebs_backup_list_complete = ` ec2-describe-volumes --show-empty-fields --region $region $ebs_selection_string 2>& 1`
#takes the output of the previous command
ebs_backup_list_result = ` echo $? `
if [ [ $ebs_backup_list_result -gt 0 ] ]
2012-09-14 23:59:56 +00:00
then echo -e " An error occured when running ec2-describe-volumes. The error returned is below:\n $ebs_backup_list_complete " 1>& 2 ; exit 70
2012-09-08 01:54:15 +00:00
fi
ebs_backup_list = ` echo " $ebs_backup_list_complete " | grep ^VOLUME | cut -f 2`
}
2012-09-21 02:04:51 +00:00
create_EBS_Snapshot_Tags( )
{
#snapshot tags holds all tags that need to be applied to a given snapshot - by aggregating tags we ensure that ec2-create-tags is called only onece
snapshot_tags = ""
2014-04-12 21:21:28 +00:00
#if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags
2012-09-21 02:04:51 +00:00
if $name_tag_create
then
2014-03-02 18:47:30 +00:00
# possible duplicate code ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2`
2012-09-21 02:04:51 +00:00
ec2_snapshot_resource_id = ` echo " $ec2_create_snapshot_result " | cut -f 2`
2014-04-12 21:21:28 +00:00
snapshot_tags = " $snapshot_tags --tag Name=ec2ab_ ${ ebs_selected } _ $current_date "
2012-09-21 02:04:51 +00:00
fi
2013-10-15 02:51:06 +00:00
#if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags
if $hostname_tag_create
then
snapshot_tags = " $snapshot_tags --tag InitiatingHost='`hostname -f`' "
fi
2014-04-12 21:21:28 +00:00
#if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags
if [ [ -n $purge_after_date_fe ] ]
2012-09-21 02:04:51 +00:00
then
2014-04-12 21:21:28 +00:00
snapshot_tags = " $snapshot_tags --tag PurgeAfterFE= $purge_after_date_fe --tag PurgeAllow=true "
2012-09-21 02:04:51 +00:00
fi
2013-01-16 18:34:31 +00:00
2013-02-17 21:53:38 +00:00
#if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags
if $user_tags
2013-01-16 18:34:31 +00:00
then
2014-04-12 21:21:28 +00:00
snapshot_tags = " $snapshot_tags --tag Volume= ${ ebs_selected } --tag Created= $current_date "
2013-01-16 18:34:31 +00:00
fi
2012-09-21 02:04:51 +00:00
#if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags
if [ [ -n $snapshot_tags ] ]
2012-10-06 22:27:59 +00:00
then echo " Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: "
ec2-create-tags $ec2_snapshot_resource_id --region $region $snapshot_tags
2012-09-21 02:04:51 +00:00
fi
}
2014-04-12 21:21:28 +00:00
get_date_binary( )
2012-10-06 22:27:59 +00:00
{
2012-11-09 07:26:38 +00:00
#`uname -o (operating system) would be ideal, but OS X / Darwin does not support to -o option`
#`uname` on OS X defaults to `uname -s` and `uname` on GNU/Linux defaults to `uname -s`
uname_result = ` uname`
case $uname_result in
Darwin) date_binary = "osx-posix" ; ;
Linux) date_binary = "linux-gnu" ; ;
2012-10-06 22:27:59 +00:00
*) date_binary = "unknown" ; ;
esac
2012-11-09 07:26:38 +00:00
}
2014-04-12 21:21:28 +00:00
get_purge_after_date_fe( )
2012-11-09 07:26:38 +00:00
{
2014-04-12 21:21:28 +00:00
case $purge_after_input in
#any number of numbers followed by a letter "d" or "days" multiplied by 86400 (number of seconds in a day)
[ 0-9] *d) purge_after_value_seconds = $(( ${ purge_after_input %? } * 86400 )) ; ;
#any number of numbers followed by a letter "h" or "hours" multiplied by 3600 (number of seconds in an hour)
[ 0-9] *h) purge_after_value_seconds = $(( ${ purge_after_input %? } * 3600 )) ; ;
#any number of numbers followed by a letter "m" or "minutes" multiplied by 60 (number of seconds in a minute)
[ 0-9] *m) purge_after_value_seconds = $(( ${ purge_after_input %? } * 60 )) ; ;
#no trailing digits default is days - multiply by 86400 (number of minutes in a day)
*) purge_after_value_seconds = $(( $purge_after_input * 86400 )) ; ;
2012-11-09 07:26:38 +00:00
esac
2014-04-12 21:21:28 +00:00
#based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future
2012-11-09 07:26:38 +00:00
case $date_binary in
2014-04-12 21:21:28 +00:00
linux-gnu) echo ` date -d +${ purge_after_value_seconds } sec -u +%s` ; ;
osx-posix) echo ` date -v +${ purge_after_value_seconds } S -u +%s` ; ;
*) echo ` date -d +${ purge_after_value_seconds } sec -u +%s` ; ;
2012-11-09 07:26:38 +00:00
esac
2012-10-06 22:27:59 +00:00
}
2012-09-24 23:34:15 +00:00
purge_EBS_Snapshots( )
{
2014-04-12 21:21:28 +00:00
#snapshot_tag_list is a string that contains all snapshots with either the key PurgeAllow or PurgeAfterFE set
snapshot_tag_list = ` ec2-describe-tags --show-empty-fields --region $region --filter resource-type= snapshot --filter key = PurgeAllow,PurgeAfterFE`
2012-09-24 23:34:15 +00:00
#snapshot_purge_allowed is a list of all snapshot_ids with PurgeAllow=true
2012-11-09 04:35:39 +00:00
snapshot_purge_allowed = ` echo " $snapshot_tag_list " | grep .*PurgeAllow'\s' true | cut -f 3`
2014-04-12 21:21:28 +00:00
2012-09-24 23:34:15 +00:00
for snapshot_id_evaluated in $snapshot_purge_allowed
do
2014-04-12 21:21:28 +00:00
#gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s)
purge_after_date_fe_tag = ` echo " $snapshot_tag_list " | grep .*$snapshot_id_evaluated '\s' PurgeAfterFE.* | cut -f 5`
2012-12-30 06:50:08 +00:00
#if purge_after_date is not set then we have a problem. Need to alert user.
2014-04-12 21:21:28 +00:00
if [ [ -z $purge_after_date_fe_tag ] ]
#Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date.
then echo " A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfterFE=xxxxxxxxxx\" date where PurgeAfterFE is UNIX time. $app_name is unable to determine if $snapshot_id_evaluated should be purged. " 1>& 2
2012-09-24 23:34:15 +00:00
else
2014-04-12 21:21:28 +00:00
#perform comparison - if $purge_after_date_epoch is a lower number than $current_date_epoch than the PurgeAfterFE date is earlier than the current date - and the snapshot can be safely removed
if [ [ $purge_after_date_fe_tag < $current_date ] ]
2012-09-24 23:34:15 +00:00
then
2014-04-12 21:21:28 +00:00
echo " The snapshot \" $snapshot_id_evaluated \" with the PurgeAfterFE date of $purge_after_date_fe_tag will be deleted. "
2012-09-24 23:34:15 +00:00
ec2-delete-snapshot --region $region $snapshot_id_evaluated
fi
fi
done
}
2012-09-08 01:54:15 +00:00
app_name = ` basename $0 `
#sets defaults
selection_method = "volumeid"
2012-10-06 22:27:59 +00:00
#date_binary allows a user to set the "date" binary that is installed on their system and, therefore, the options that will be given to the date binary to perform date calculations
date_binary = ""
2012-09-14 23:59:56 +00:00
#sets the "Name" tag set for a snapshot to false - using "Name" requires that ec2-create-tags be called in addition to ec2-create-snapshot
2012-09-21 02:04:51 +00:00
name_tag_create = false
2013-10-15 02:51:06 +00:00
#sets the "InitiatingHost" tag set for a snapshot to false
hostname_tag_create = false
2014-04-12 21:21:28 +00:00
#sets the user_tags feature to false - user_tag creates tags on snapshots - by default each snapshot is tagged with volume_id and current_date timestamp
2013-02-17 21:53:38 +00:00
user_tags = false
2014-04-12 21:21:28 +00:00
#sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfterFE" tag that is earlier than current date
2012-09-21 02:04:51 +00:00
purge_snapshots = false
2012-09-08 01:54:15 +00:00
#handles options processing
2014-03-02 18:47:30 +00:00
2013-10-15 02:58:36 +00:00
while getopts :s:c:r:v:t:k:pnhu opt
2013-02-17 21:53:38 +00:00
do
case $opt in
s) selection_method = " $OPTARG " ; ;
c) cron_primer = " $OPTARG " ; ;
r) region = " $OPTARG " ; ;
v) volumeid = " $OPTARG " ; ;
t) tag = " $OPTARG " ; ;
2014-04-12 21:21:28 +00:00
k) purge_after_input = " $OPTARG " ; ;
2013-02-17 21:53:38 +00:00
n) name_tag_create = true; ;
2013-10-15 02:51:06 +00:00
h) hostname_tag_create = true; ;
2013-02-17 21:53:38 +00:00
p) purge_snapshots = true; ;
u) user_tags = true; ;
*) echo "Error with Options Input. Cause of failure is most likely that an unsupported parameter was passed or a parameter was passed without a corresponding option." 1>& 2 ; exit 64; ;
esac
done
2012-09-08 01:54:15 +00:00
2012-11-12 03:12:59 +00:00
#sources "cron_primer" file for running under cron or other restricted environments - this file should contain the variables and environment configuration required for ec2-automate-backup to run correctly
if [ [ -n $cron_primer ] ]
2014-04-12 21:21:28 +00:00
then if [ [ -f $cron_primer ] ]
2012-11-12 03:12:59 +00:00
then source $cron_primer
else
echo " Cron Primer File \" $cron_primer \" Could Not Be Found. " 1>& 2 ; exit 70
fi
fi
2013-02-17 21:53:38 +00:00
#if region is not set then:
if [ [ -z $region ] ]
#if the environment variable $EC2_REGION is not set set to us-east-1
then if [ [ -z $EC2_REGION ] ]
2014-04-12 21:21:28 +00:00
#if both
2013-02-17 21:53:38 +00:00
then region = "us-east-1"
2013-01-31 15:19:58 +00:00
else
2013-02-17 21:53:38 +00:00
region = $EC2_REGION
2013-01-31 15:19:58 +00:00
fi
fi
2012-11-12 03:12:59 +00:00
#calls prerequisitecheck function to ensure that all executables required for script execution are available
prerequisite_check
2012-09-21 02:04:51 +00:00
#sets date variable
2014-04-12 21:21:28 +00:00
current_date = ` date -u +%s`
#sets the PurgeAfterFE tag to the number of seconds that a snapshot should be retained
if [ [ -n $purge_after_input ] ]
2012-10-06 22:27:59 +00:00
then
2014-04-12 21:21:28 +00:00
#if the date_binary is not set, call the get_date_binary function
2012-10-06 22:27:59 +00:00
if [ [ -z $date_binary ] ]
2014-04-12 21:21:28 +00:00
then get_date_binary
2012-10-06 22:27:59 +00:00
fi
2014-04-12 21:21:28 +00:00
purge_after_date_fe = ` get_purge_after_date_fe`
echo " Snapshots taken by $app_name will be eligible for purging after the following date (the purge after date given in seconds from epoch): $purge_after_date_fe . "
2012-09-21 02:04:51 +00:00
fi
2012-09-08 01:54:15 +00:00
#get_EBS_List gets a list of EBS instances for which a snapshot is desired. The list of EBS instances depends upon the selection_method that is provided by user input
2012-09-21 02:04:51 +00:00
get_EBS_List
2012-09-08 01:54:15 +00:00
#the loop below is called once for each volume in $ebs_backup_list - the currently selected EBS volume is passed in as "ebs_selected"
for ebs_selected in $ebs_backup_list
do
2014-04-12 21:21:28 +00:00
ec2_snapshot_description = " ec2ab_ ${ ebs_selected } _ $current_date "
2012-09-14 23:59:56 +00:00
ec2_create_snapshot_result = ` ec2-create-snapshot --region $region -d $ec2_snapshot_description $ebs_selected 2>& 1`
if [ [ $? != 0 ] ]
then echo -e " An error occured when running ec2-create-snapshot. The error returned is below:\n $ec2_create_snapshot_result " 1>& 2 ; exit 70
2012-09-21 02:04:51 +00:00
else
2012-09-14 23:59:56 +00:00
ec2_snapshot_resource_id = ` echo " $ec2_create_snapshot_result " | cut -f 2`
2014-04-12 21:21:28 +00:00
fi
2012-09-21 02:04:51 +00:00
create_EBS_Snapshot_Tags
2012-09-24 23:34:15 +00:00
done
#if purge_snapshots is true, then run purge_EBS_Snapshots function
if $purge_snapshots
then echo "Snapshot Purging is Starting Now."
purge_EBS_Snapshots
fi