Google Bash Styleguide updates

This commit is contained in:
Colin Johnson 2014-04-20 00:34:18 +00:00
parent 3d8dad1568
commit 70362705d6
4 changed files with 322 additions and 360 deletions

View File

@ -56,7 +56,7 @@ The parameters are the same as ec2-automate-backup.sh with the exception of the
## Example of use: ## Example of use:
ec2-automate-backup-awscli.sh -r us-east-1 -s tag -t 'Backup,Values=True' -k 31 -p -n ec2-automate-backup-awscli.sh -r us-east-1 -s tag -t 'Backup,Values=true' -k 31 -p -n
# Additional Information: # Additional Information:

View File

@ -1,12 +1,12 @@
ec2ab_exec="/Temp/ec2-automate-backup.sh" ec2ab_exec="../ec2-automate-backup-awscli.sh"
#create two volumes #create two volumes
volume_1=`ec2-create-volume --size 2 GB --availability-zone us-east-1a` volume_1=$(ec2-create-volume --size 2 GB --availability-zone us-east-1a)
volume_2=`ec2-create-volume --size 2 GB --availability-zone us-east-1a` volume_2=$(ec2-create-volume --size 2 GB --availability-zone us-east-1a)
#get volume id of both volumes #get volume id of both volumes
volume_1_id=`echo "$volume_1" | cut -f 2` volume_1_id=$(echo "$volume_1" | cut -f 2)
volume_2_id=`echo "$volume_2" | cut -f 2` volume_2_id=$(echo "$volume_2" | cut -f 2)
#tag volume_1 with Backup=true #tag volume_1 with Backup=true
ec2-create-tags $volume_1_id --tag Backup=true ec2-create-tags $volume_1_id --tag Backup=true
@ -32,30 +32,30 @@ $ec2ab_exec -v "$volume_1_id $volume_2_id" -u
#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+604800" #test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+604800"
$ec2ab_exec -v "$volume_1_id $volume_2_id" -k 7 $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 7
echo "PurgeAfterFE=`date -v +604800S -u +%s`" echo "PurgeAfterFE=$(date -v +604800S -u +%s)"
echo "PurgeAfterFE=`date -d +604800sec -u +%s`" echo "PurgeAfterFE=$(date -d +604800sec -u +%s)"
#expected result - snapshot taken of volume_1 and volume_2 each with tags "PurgeAfterFE=$current_date+604800" and "PurgeAllow=true" #expected result - snapshot taken of volume_1 and volume_2 each with tags "PurgeAfterFE=$current_date+604800" and "PurgeAllow=true"
#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+604800" #test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+604800"
$ec2ab_exec -v "$volume_1_id $volume_2_id" -k 7 -n $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 7 -n
echo "PurgeAfterFE=`date -v+604800S -u +%s`" echo "PurgeAfterFE=$(date -v+604800S -u +%s)"
echo "PurgeAfterFE=`date -d +604800sec -u +%s`" echo "PurgeAfterFE=$(date -d +604800sec -u +%s)"
#expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+604800" #expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+604800"
#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+3600" #test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+3600"
$ec2ab_exec -v "$volume_1_id $volume_2_id" -k 1h -n $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 1h -n
echo "PurgeAfterFE=`date -v+3600S -u +%s`" echo "PurgeAfterFE=$(date -v+3600S -u +%s)"
echo "PurgeAfterFE=`date -d +3600sec -u +%s`" echo "PurgeAfterFE=$(date -d +3600sec -u +%s)"
#expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+3600" #expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+3600"
#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+3600" #test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+3600"
$ec2ab_exec -v "$volume_1_id $volume_2_id" -k 30m -n $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 30m -n
echo "PurgeAfterFE=`date -v+1800S -u +%s`" echo "PurgeAfterFE=$(date -v+1800S -u +%s)"
echo "PurgeAfterFE=`date -d +1800sec -u +%s`" echo "PurgeAfterFE=$(date -d +1800sec -u +%s)"
#expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+3600" #expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+3600"
#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+60" and purging snapshots #test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfterFE=$current_date+60" and purging snapshots
$ec2ab_exec -v "$volume_1_id $volume_2_id" -k 1m -n -p $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 1m -n -p
echo "PurgeAfterFE=`date -v+60S -u +%s`" echo "PurgeAfterFE=$(date -v+60S -u +%s)"
echo "PurgeAfterFE=`date -d +60sec -u +%s`" echo "PurgeAfterFE=$(date -d +60sec -u +%s)"
#expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+60" and if run twice after 60 seconds, volumes purged #expected result - snapshot taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current a with a tag "PurgeAfterFE=$current_date+60" and if run twice after 60 seconds, volumes purged

View File

@ -14,144 +14,129 @@
# buckelij / https://github.com/buckelij # buckelij / https://github.com/buckelij
#confirms that executables required for succesful script execution are available #confirms that executables required for succesful script execution are available
prerequisite_check() prerequisite_check() {
{ for prerequisite in basename cut date aws; do
for prerequisite in basename cut date aws #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.
do hash $prerequisite &> /dev/null
#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. if [[ $? == 1 ]]; then #has exits with exit status of 70, executable was not found
hash $prerequisite &> /dev/null echo "In order to use $app_name, the executable \"$prerequisite\" must be installed." 1>&2 ; exit 70
if [[ $? == 1 ]] #has exits with exit status of 70, executable was not found fi
then echo "In order to use `basename $0`, the executable \"$prerequisite\" must be installed." 1>&2 ; exit 70 done
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 #get_EBS_List gets a list of available EBS instances depending upon the selection_method of EBS selection that is provided by user input
get_EBS_List() get_EBS_List() {
{ case $selection_method in
case $selection_method in volumeid)
volumeid) if [[ -z $volumeid ]]; then
if [[ -z $volumeid ]] 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
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
fi ebs_selection_string="--volume-ids $volumeid"
ebs_selection_string="--volume-ids $volumeid" ;;
;; tag)
tag) if [[ -z $tag ]]; then
if [[ -z $tag ]] echo "The selected selection_method \"tag\" (-s tag) requires a valid tag (-t Backup,Values=true) for operation. Correct usage is as follows: \"-s tag -t Backup,Values=true.\"" 1>&2 ; exit 64
then echo "The selected selection_method \"tag\" (-s tag) requires a valid tag (-t Backup,Values=true) for operation. Correct usage is as follows: \"-s tag -t Backup,Values=true.\"" 1>&2 ; exit 64 fi
fi ebs_selection_string="--filters Name=tag:$tag"
ebs_selection_string="--filters Name=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 ;;
*) 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
esac #creates a list of all ebs volumes that match the selection string from above
#creates a list of all ebs volumes that match the selection string from above ebs_backup_list_complete=$(aws ec2 describe-volumes --region $region $ebs_selection_string --output text)
ebs_backup_list_complete=`aws ec2 describe-volumes --region $region $ebs_selection_string --output text` #takes the output of the previous command
#takes the output of the previous command ebs_backup_list_result=$(echo $?)
ebs_backup_list_result=`echo $?` if [[ $ebs_backup_list_result -gt 0 ]]; then
if [[ $ebs_backup_list_result -gt 0 ]] echo -e "An error occured when running ec2-describe-volumes. The error returned is below:\n$ebs_backup_list_complete" 1>&2 ; exit 70
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 fi
fi #returns the list of EBS volumes that matched ebs_selection_string. grep ^VOLUMES is to remove lines that begin "TAGS Backup"
#returns the list of EBS volumes that matched ebs_selection_string. grep ^VOLUMES is to remove lines that begin "TAGS Backup" ebs_backup_list=$(echo "$ebs_backup_list_complete" | grep ^VOLUMES | cut -f 7)
ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUMES | cut -f 7`
} }
create_EBS_Snapshot_Tags() 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 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=""
snapshot_tags="" #if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags
#if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags if $name_tag_create; then
if $name_tag_create snapshot_tags="$snapshot_tags Key=Name,Value=ec2ab_${ebs_selected}_$current_date"
then fi
snapshot_tags="$snapshot_tags Key=Name,Value=ec2ab_${ebs_selected}_$current_date" #if $hostname_tag_create is true then append --tag InitiatingHost=$(hostname -f) to the variable $snapshot_tags
fi if $hostname_tag_create; then
#if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags snapshot_tags="$snapshot_tags Key=InitiatingHost,Value='$(hostname -f)'"
if $hostname_tag_create fi
then #if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags
snapshot_tags="$snapshot_tags Key=InitiatingHost,Value='`hostname -f`'" if [[ -n $purge_after_date_fe ]]; then
fi snapshot_tags="$snapshot_tags Key=PurgeAfterFE,Value=$purge_after_date_fe Key=PurgeAllow,Value=true"
#if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags fi
if [[ -n $purge_after_date_fe ]] #if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags
then if $user_tags; then
snapshot_tags="$snapshot_tags Key=PurgeAfterFE,Value=$purge_after_date_fe Key=PurgeAllow,Value=true" snapshot_tags="$snapshot_tags Key=Volume,Value=${ebs_selected} Key=Created,Value=$current_date"
fi fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using aws ec2 create-tags
#if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags if [[ -n $snapshot_tags ]]; then
if $user_tags echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags"
then tags_argument="--tags $snapshot_tags"
snapshot_tags="$snapshot_tags Key=Volume,Value=${ebs_selected} Key=Created,Value=$current_date" aws_ec2_create_tag_result=$(aws ec2 create-tags --resources $ec2_snapshot_resource_id --region $region $tags_argument --output text 2>&1)
fi fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using aws ec2 create-tags
if [[ -n $snapshot_tags ]]
then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags"
tags_arugment="--tags $snapshot_tags"
aws_ec2_create_tag_result=`aws ec2 create-tags --resources $ec2_snapshot_resource_id --region $region $tags_arugment --output text 2>&1`
fi
} }
get_date_binary() get_date_binary() {
{ #$(uname -o) (operating system) would be ideal, but OS X / Darwin does not support to -o option
#`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` on OS X defaults to `uname -s` and `uname` on GNU/Linux defaults to `uname -s` uname_result=$(uname)
uname_result=`uname` case $uname_result in
case $uname_result in Darwin) date_binary="osx-posix" ;;
Darwin) date_binary="osx-posix" ;; Linux) date_binary="linux-gnu" ;;
Linux) date_binary="linux-gnu" ;; *) date_binary="unknown" ;;
*) date_binary="unknown" ;; esac
esac
} }
get_purge_after_date_fe() get_purge_after_date_fe() {
{
case $purge_after_input in 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) #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 )) ;; [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) #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 )) ;; [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) #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 ));; [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) #no trailing digits default is days - multiply by 86400 (number of minutes in a day)
*) purge_after_value_seconds=$(( $purge_after_input * 86400 ));; *) purge_after_value_seconds=$(( $purge_after_input * 86400 ));;
esac esac
#based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future #based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future
case $date_binary in case $date_binary in
linux-gnu) echo `date -d +${purge_after_value_seconds}sec -u +%s` ;; linux-gnu) echo $(date -d +${purge_after_value_seconds}sec -u +%s) ;;
osx-posix) echo `date -v +${purge_after_value_seconds}S -u +%s` ;; osx-posix) echo $(date -v +${purge_after_value_seconds}S -u +%s) ;;
*) echo `date -d +${purge_after_value_seconds}sec -u +%s` ;; *) echo $(date -d +${purge_after_value_seconds}sec -u +%s) ;;
esac esac
} }
purge_EBS_Snapshots() purge_EBS_Snapshots() {
{ # snapshot_tag_list is a string containing any snapshot that contains a tag
# snapshot_tag_list is a string containing any snapshot that contains a tag # with the key value/pair PurgeAllow=true
# with the key value/pair PurgeAllow=true snapshot_tag_list=$(aws ec2 describe-snapshots --region $region --filters Name=tag:PurgeAllow,Values=true --output text | grep ^SNAPSHOTS)
snapshot_tag_list=`aws ec2 describe-snapshots --region $region --filters Name=tag:PurgeAllow,Values=true --output text | grep ^SNAPSHOTS` # snapshot_purge_allowed is a string containing Snapshot IDs that are
# snapshot_purge_allowed is a string containing Snapshot IDs that are # allowed to be purged
# allowed to be purged snapshot_purge_allowed=$(echo "$snapshot_tag_list" | cut -f 5)
snapshot_purge_allowed=`echo "$snapshot_tag_list" | cut -f 5`
for snapshot_id_evaluated in $snapshot_purge_allowed; do
for snapshot_id_evaluated in $snapshot_purge_allowed #gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s)
do purge_after_fe=$(aws ec2 describe-snapshots --region $region --snapshot-ids $snapshot_id_evaluated --output text | grep ^TAGS.*PurgeAfterFE | cut -f 3)
#gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s) #if purge_after_date is not set then we have a problem. Need to alert user.
purge_after_fe=`aws ec2 describe-snapshots --region $region --snapshot-ids $snapshot_id_evaluated --output text | grep ^TAGS.*PurgeAfterFE | cut -f 3` if [[ -z $purge_after_fe ]]; then
#if purge_after_date is not set then we have a problem. Need to alert user. #Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date.
if [[ -z $purge_after_fe ]] echo "Snapshot with the Snapshot ID \"$snapshot_id_evaluated\" has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfterFE=xxxxxxxxxx\" key/value pair. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2
#Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date. else
then echo "A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfterFE=xxxxxxxxxx\" key/value pair. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2 # if $purge_after_fe is less than $current_date then
else # PurgeAfterFE is earlier than the current date
# if $purge_after_fe is less than $current_date then # and the snapshot can be safely purged
# PurgeAfterFE is earlier than the current date if [[ $purge_after_fe < $current_date ]]; then
# and the snapshot can be safely purged echo "Snapshot \"$snapshot_id_evaluated\" with the PurgeAfterFE date of \"$purge_after_fe\" will be deleted."
if [[ $purge_after_fe < $current_date ]] aws_ec2_delete_snapshot_result=$(aws ec2 delete-snapshot --region $region --snapshot-id $snapshot_id_evaluated --output text 2>&1)
then fi
echo "The snapshot \"$snapshot_id_evaluated\" with the PurgeAfterFE date of $purge_after_fe will be deleted." fi
aws_ec2_delete_snapshot_result=`aws ec2 delete-snapshot --region $region --snapshot-id $snapshot_id_evaluated --output text 2>&1` done
fi
fi
done
} }
app_name=`basename $0` app_name=$(basename $0)
#sets defaults #sets defaults
selection_method="volumeid" selection_method="volumeid"
#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 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
@ -166,78 +151,74 @@ user_tags=false
purge_snapshots=false purge_snapshots=false
#handles options processing #handles options processing
while getopts :s:c:r:v:t:k:pnhu opt while getopts :s:c:r:v:t:k:pnhu opt; do
do case $opt in
case $opt in s) selection_method="$OPTARG" ;;
s) selection_method="$OPTARG";; c) cron_primer="$OPTARG" ;;
c) cron_primer="$OPTARG";; r) region="$OPTARG" ;;
r) region="$OPTARG";; v) volumeid="$OPTARG" ;;
v) volumeid="$OPTARG";; t) tag="$OPTARG" ;;
t) tag="$OPTARG";; k) purge_after_input="$OPTARG" ;;
k) purge_after_input="$OPTARG";; n) name_tag_create=true ;;
n) name_tag_create=true;; h) hostname_tag_create=true ;;
h) hostname_tag_create=true;; p) purge_snapshots=true ;;
p) purge_snapshots=true;; u) user_tags=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 ;;
*) 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
esac done
done
#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 #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 ]] if [[ -n $cron_primer ]]; then
then if [[ -f $cron_primer ]] if [[ -f $cron_primer ]]; then
then source $cron_primer source $cron_primer
else else
echo "Cron Primer File \"$cron_primer\" Could Not Be Found." 1>&2 ; exit 70 echo "Cron Primer File \"$cron_primer\" Could Not Be Found." 1>&2 ; exit 70
fi fi
fi fi
#if region is not set then: #if region is not set then:
if [[ -z $region ]] if [[ -z $region ]]; then
#if the environment variable $EC2_REGION is not set set to us-east-1 #if the environment variable $EC2_REGION is not set set to us-east-1
then if [[ -z $EC2_REGION ]] if [[ -z $EC2_REGION ]]; then
#if both region="us-east-1"
then region="us-east-1" else
else region=$EC2_REGION
region=$EC2_REGION fi
fi
fi fi
#calls prerequisitecheck function to ensure that all executables required for script execution are available #calls prerequisitecheck function to ensure that all executables required for script execution are available
prerequisite_check prerequisite_check
#sets date variable #sets date variable
current_date=`date -u +%s` current_date=$(date -u +%s)
#sets the PurgeAfterFE tag to the number of seconds that a snapshot should be retained #sets the PurgeAfterFE tag to the number of seconds that a snapshot should be retained
if [[ -n $purge_after_input ]] if [[ -n $purge_after_input ]]; then
then #if the date_binary is not set, call the get_date_binary function
#if the date_binary is not set, call the get_date_binary function if [[ -z $date_binary ]]; then
if [[ -z $date_binary ]] get_date_binary
then get_date_binary fi
fi purge_after_date_fe=$(get_purge_after_date_fe)
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."
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."
fi fi
#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 #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
get_EBS_List get_EBS_List
#the loop below is called once for each volume in $ebs_backup_list - the currently selected EBS volume is passed in as "ebs_selected" #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 for ebs_selected in $ebs_backup_list; do
do ec2_snapshot_description="ec2ab_${ebs_selected}_$current_date"
ec2_snapshot_description="ec2ab_${ebs_selected}_$current_date" ec2_create_snapshot_result=$(aws ec2 create-snapshot --region $region --description $ec2_snapshot_description --volume-id $ebs_selected --output text 2>&1)
ec2_create_snapshot_result=`aws ec2 create-snapshot --region $region --description $ec2_snapshot_description --volume-id $ebs_selected --output text 2>&1` if [[ $? != 0 ]]; then
if [[ $? != 0 ]] echo -e "An error occured when running ec2-create-snapshot. The error returned is below:\n$ec2_create_snapshot_result" 1>&2 ; exit 70
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 else
else ec2_snapshot_resource_id=$(echo "$ec2_create_snapshot_result" | cut -f 4)
ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 4` fi
fi create_EBS_Snapshot_Tags
create_EBS_Snapshot_Tags
done done
#if purge_snapshots is true, then run purge_EBS_Snapshots function #if purge_snapshots is true, then run purge_EBS_Snapshots function
if $purge_snapshots if $purge_snapshots; then
then echo "Snapshot Purging is Starting Now." echo "Snapshot Purging is Starting Now."
purge_EBS_Snapshots purge_EBS_Snapshots
fi fi

View File

@ -14,145 +14,130 @@
# buckelij / https://github.com/buckelij # buckelij / https://github.com/buckelij
#confirms that executables required for succesful script execution are available #confirms that executables required for succesful script execution are available
prerequisite_check() prerequisite_check() {
{ for prerequisite in basename cut date ec2-create-snapshot ec2-create-tags ec2-delete-snapshot ec2-describe-snapshots; do
for prerequisite in basename cut date ec2-create-snapshot ec2-create-tags ec2-delete-snapshot ec2-describe-snapshots #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.
do hash $prerequisite &> /dev/null
#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. if [[ $? == 1 ]]; then #has exits with exit status of 70, executable was not found
hash $prerequisite &> /dev/null echo "In order to use $app_name, the executable \"$prerequisite\" must be installed." 1>&2 ; exit 70
if [[ $? == 1 ]] #has exits with exit status of 70, executable was not found fi
then echo "In order to use `basename $0`, the executable \"$prerequisite\" must be installed." 1>&2 ; exit 70 done
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 #get_EBS_List gets a list of available EBS instances depending upon the selection_method of EBS selection that is provided by user input
get_EBS_List() get_EBS_List() {
{ case $selection_method in
case $selection_method in volumeid)
volumeid) if [[ -z $volumeid ]]; then
if [[ -z $volumeid ]] 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
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
fi ebs_selection_string="$volumeid"
ebs_selection_string="$volumeid" ;;
;; tag)
tag) if [[ -z $tag ]]; then
if [[ -z $tag ]] 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
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 fi
fi ebs_selection_string="--filter tag:$tag"
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 ;;
*) 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
esac #creates a list of all ebs volumes that match the selection string from above
#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)
ebs_backup_list_complete=`ec2-describe-volumes --show-empty-fields --region $region $ebs_selection_string 2>&1` #takes the output of the previous command
#takes the output of the previous command ebs_backup_list_result=$(echo $?)
ebs_backup_list_result=`echo $?` if [[ $ebs_backup_list_result -gt 0 ]]; then
if [[ $ebs_backup_list_result -gt 0 ]] echo -e "An error occured when running ec2-describe-volumes. The error returned is below:\n$ebs_backup_list_complete" 1>&2 ; exit 70
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 fi
fi #returns the list of EBS volumes that matched ebs_selection_string.
#returns the list of EBS volumes that matched ebs_selection_string. ebs_backup_list=$(echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2)
ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2`
} }
create_EBS_Snapshot_Tags() 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 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=""
snapshot_tags="" #if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags
#if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags if $name_tag_create; then
if $name_tag_create snapshot_tags="$snapshot_tags --tag Name=ec2ab_${ebs_selected}_$current_date"
then fi
snapshot_tags="$snapshot_tags --tag Name=ec2ab_${ebs_selected}_$current_date" #if $hostname_tag_create is true then append --tag InitiatingHost=$(hostname -f) to the variable $snapshot_tags
fi if $hostname_tag_create; then
#if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags snapshot_tags="$snapshot_tags --tag InitiatingHost='$(hostname -f)'"
if $hostname_tag_create fi
then #if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags
snapshot_tags="$snapshot_tags --tag InitiatingHost='`hostname -f`'" if [[ -n $purge_after_date_fe ]]; then
fi snapshot_tags="$snapshot_tags --tag PurgeAfterFE=$purge_after_date_fe --tag PurgeAllow=true"
#if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags fi
if [[ -n $purge_after_date_fe ]] #if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags
then if $user_tags; then
snapshot_tags="$snapshot_tags --tag PurgeAfterFE=$purge_after_date_fe --tag PurgeAllow=true" snapshot_tags="$snapshot_tags --tag Volume=${ebs_selected} --tag Created=$current_date"
fi fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags
#if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags if [[ -n $snapshot_tags ]]; then
if $user_tags echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags"
then ec2-create-tags $ec2_snapshot_resource_id --region $region $snapshot_tags
snapshot_tags="$snapshot_tags --tag Volume=${ebs_selected} --tag Created=$current_date" fi
fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags
if [[ -n $snapshot_tags ]]
then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags"
ec2-create-tags $ec2_snapshot_resource_id --region $region $snapshot_tags
fi
} }
get_date_binary() get_date_binary() {
{ #$(uname -o) (operating system) would be ideal, but OS X / Darwin does not support to -o option
#`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` on OS X defaults to `uname -s` and `uname` on GNU/Linux defaults to `uname -s` uname_result=$(uname)
uname_result=`uname` case $uname_result in
case $uname_result in Darwin) date_binary="osx-posix" ;;
Darwin) date_binary="osx-posix" ;; Linux) date_binary="linux-gnu" ;;
Linux) date_binary="linux-gnu" ;; *) date_binary="unknown" ;;
*) date_binary="unknown" ;; esac
esac
} }
get_purge_after_date_fe() get_purge_after_date_fe() {
{
case $purge_after_input in 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) #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 )) ;; [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) #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 )) ;; [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) #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 ));; [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) #no trailing digits default is days - multiply by 86400 (number of minutes in a day)
*) purge_after_value_seconds=$(( $purge_after_input * 86400 ));; *) purge_after_value_seconds=$(( $purge_after_input * 86400 ));;
esac esac
#based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future #based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future
case $date_binary in case $date_binary in
linux-gnu) echo `date -d +${purge_after_value_seconds}sec -u +%s` ;; linux-gnu) echo $(date -d +${purge_after_value_seconds}sec -u +%s) ;;
osx-posix) echo `date -v +${purge_after_value_seconds}S -u +%s` ;; osx-posix) echo $(date -v +${purge_after_value_seconds}S -u +%s) ;;
*) echo `date -d +${purge_after_value_seconds}sec -u +%s` ;; *) echo $(date -d +${purge_after_value_seconds}sec -u +%s) ;;
esac esac
} }
purge_EBS_Snapshots() purge_EBS_Snapshots() {
{ # snapshot_tag_list is a string containing any snapshot that contains
# snapshot_tag_list is a string containing any snapshot that contains # either the key PurgeAllow or the key PurgeAfterFE
# either the key PurgeAllow or the key PurgeAfterFE # note that filtering for *both* keys is a requirement or else the
# note that filtering for *both* keys is a requirement or else the # PurgeAfterFE key/value pair will not be returned
# PurgeAfterFE key/value pair will not be returned snapshot_tag_list=$(ec2-describe-tags --show-empty-fields --region $region --filter resource-type=snapshot --filter key=PurgeAllow,PurgeAfterFE)
snapshot_tag_list=`ec2-describe-tags --show-empty-fields --region $region --filter resource-type=snapshot --filter key=PurgeAllow,PurgeAfterFE` # snapshot_purge_allowed is a string containing Snapshot IDs that are
# snapshot_purge_allowed is a string containing Snapshot IDs that are # allowed to be purged
# allowed to be purged snapshot_purge_allowed=$(echo "$snapshot_tag_list" | grep .*PurgeAllow'\s'true | cut -f 3)
snapshot_purge_allowed=`echo "$snapshot_tag_list" | grep .*PurgeAllow'\s'true | cut -f 3`
for snapshot_id_evaluated in $snapshot_purge_allowed for snapshot_id_evaluated in $snapshot_purge_allowed; do
do #gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s)
#gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s) purge_after_fe=$(echo "$snapshot_tag_list" | grep .*$snapshot_id_evaluated'\s'PurgeAfterFE.* | cut -f 5)
purge_after_fe=`echo "$snapshot_tag_list" | grep .*$snapshot_id_evaluated'\s'PurgeAfterFE.* | cut -f 5` #if purge_after_date is not set then we have a problem. Need to alert user.
#if purge_after_date is not set then we have a problem. Need to alert user. if [[ -z $purge_after_fe ]]; then
if [[ -z $purge_after_fe ]] #Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date.
#Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date. echo "Snapshot with the Snapshot ID \"$snapshot_id_evaluated\" has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfterFE=xxxxxxxxxx\" key/value pair. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2
then echo "A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfterFE=xxxxxxxxxx\" key/value pair. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2 else
else # if $purge_after_fe is less than $current_date then
# if $purge_after_fe is less than $current_date then # PurgeAfterFE is earlier than the current date
# PurgeAfterFE is earlier than the current date # and the snapshot can be safely purged
# and the snapshot can be safely purged if [[ $purge_after_fe < $current_date ]]; then
if [[ $purge_after_fe < $current_date ]] echo "Snapshot \"$snapshot_id_evaluated\" with the PurgeAfterFE date of \"$purge_after_fe\" will be deleted."
then ec2-delete-snapshot --region $region $snapshot_id_evaluated
echo "Snapshot \"$snapshot_id_evaluated\" with the PurgeAfterFE date of $purge_after_fe will be deleted." fi
ec2-delete-snapshot --region $region $snapshot_id_evaluated fi
fi done
fi
done
} }
app_name=`basename $0` app_name=$(basename $0)
#sets defaults #sets defaults
selection_method="volumeid" selection_method="volumeid"
#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 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
@ -167,78 +152,74 @@ user_tags=false
purge_snapshots=false purge_snapshots=false
#handles options processing #handles options processing
while getopts :s:c:r:v:t:k:pnhu opt while getopts :s:c:r:v:t:k:pnhu opt; do
do case $opt in
case $opt in s) selection_method="$OPTARG" ;;
s) selection_method="$OPTARG";; c) cron_primer="$OPTARG" ;;
c) cron_primer="$OPTARG";; r) region="$OPTARG" ;;
r) region="$OPTARG";; v) volumeid="$OPTARG" ;;
v) volumeid="$OPTARG";; t) tag="$OPTARG" ;;
t) tag="$OPTARG";; k) purge_after_input="$OPTARG" ;;
k) purge_after_input="$OPTARG";; n) name_tag_create=true ;;
n) name_tag_create=true;; h) hostname_tag_create=true ;;
h) hostname_tag_create=true;; p) purge_snapshots=true ;;
p) purge_snapshots=true;; u) user_tags=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 ;;
*) 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
esac done
done
#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 #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 ]] if [[ -n $cron_primer ]]; then
then if [[ -f $cron_primer ]] if [[ -f $cron_primer ]]; then
then source $cron_primer source $cron_primer
else else
echo "Cron Primer File \"$cron_primer\" Could Not Be Found." 1>&2 ; exit 70 echo "Cron Primer File \"$cron_primer\" Could Not Be Found." 1>&2 ; exit 70
fi fi
fi fi
#if region is not set then: #if region is not set then:
if [[ -z $region ]] if [[ -z $region ]]; then
#if the environment variable $EC2_REGION is not set set to us-east-1 #if the environment variable $EC2_REGION is not set set to us-east-1
then if [[ -z $EC2_REGION ]] if [[ -z $EC2_REGION ]]; then
#if both region="us-east-1"
then region="us-east-1" else
else region=$EC2_REGION
region=$EC2_REGION fi
fi
fi fi
#calls prerequisitecheck function to ensure that all executables required for script execution are available #calls prerequisitecheck function to ensure that all executables required for script execution are available
prerequisite_check prerequisite_check
#sets date variable #sets date variable
current_date=`date -u +%s` current_date=$(date -u +%s)
#sets the PurgeAfterFE tag to the number of seconds that a snapshot should be retained #sets the PurgeAfterFE tag to the number of seconds that a snapshot should be retained
if [[ -n $purge_after_input ]] if [[ -n $purge_after_input ]]; then
then #if the date_binary is not set, call the get_date_binary function
#if the date_binary is not set, call the get_date_binary function if [[ -z $date_binary ]]; then
if [[ -z $date_binary ]] get_date_binary
then get_date_binary fi
fi purge_after_date_fe=$(get_purge_after_date_fe)
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."
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."
fi fi
#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 #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
get_EBS_List get_EBS_List
#the loop below is called once for each volume in $ebs_backup_list - the currently selected EBS volume is passed in as "ebs_selected" #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 for ebs_selected in $ebs_backup_list; do
do ec2_snapshot_description="ec2ab_${ebs_selected}_$current_date"
ec2_snapshot_description="ec2ab_${ebs_selected}_$current_date" ec2_create_snapshot_result=$(ec2-create-snapshot --region $region -d $ec2_snapshot_description $ebs_selected 2>&1)
ec2_create_snapshot_result=`ec2-create-snapshot --region $region -d $ec2_snapshot_description $ebs_selected 2>&1` if [[ $? != 0 ]]; then
if [[ $? != 0 ]] echo -e "An error occured when running ec2-create-snapshot. The error returned is below:\n$ec2_create_snapshot_result" 1>&2 ; exit 70
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 else
else ec2_snapshot_resource_id=$(echo "$ec2_create_snapshot_result" | cut -f 2)
ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` fi
fi create_EBS_Snapshot_Tags
create_EBS_Snapshot_Tags
done done
#if purge_snapshots is true, then run purge_EBS_Snapshots function #if purge_snapshots is true, then run purge_EBS_Snapshots function
if $purge_snapshots if $purge_snapshots; then
then echo "Snapshot Purging is Starting Now." echo "Snapshot Purging is Starting Now."
purge_EBS_Snapshots purge_EBS_Snapshots
fi fi