From 6759a97c3d28b4ee53c67ba5dd4bd15c2811c384 Mon Sep 17 00:00:00 2001 From: Jeff Vogt Date: Mon, 14 Oct 2013 19:51:06 -0700 Subject: [PATCH 1/3] Added script flag `-h` to enable InitiatingHost as a tag to the snapshot --- ec2-automate-backup/README.md | 2 ++ ec2-automate-backup/ec2-automate-backup.sh | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/ec2-automate-backup/README.md b/ec2-automate-backup/README.md index b09556b..05786c4 100644 --- a/ec2-automate-backup/README.md +++ b/ec2-automate-backup/README.md @@ -25,6 +25,8 @@ ec2-automate-backup requires one of the following two parameters be provided: `-n` - tag snapshots "Name" tag as well as description +`-h` - tag snapshots "InitiatingHost" tag to specify which host ran the script + `-k ` - the period after which a snapshot can be purged. For example, running "ec2-automate-backup.sh -v "vol-6d6a0527 vol-636a0112" -k 31" would allow snapshots to be removed after 31 days. purge_after_days creates two tags for each volume that was backed up - a PurgeAllow tag which is set to PurgeAllow=true and a PurgeAfter tag which is set to the present day (in UTC) + the value provided by -k. `-p` - the -p flag will purge (meaning delete) all snapshots that were created more than "purge after days" ago. ec2-automate-backup looks at two tags to determine which snapshots should be deleted - the PurgeAllow and PurgeAfter tags. The tags must be set as follows: PurgeAllow=true and PurgeAfter=YYYY-MM-DD where YYYY-MM-DD must be before the present date. diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 6c61276..8bef46c 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -56,6 +56,12 @@ create_EBS_Snapshot_Tags() ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` snapshot_tags="$snapshot_tags --tag Name=ec2ab_${ebs_selected}_$date_current" fi + #if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags + if $hostname_tag_create + then + ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` + snapshot_tags="$snapshot_tags --tag InitiatingHost='`hostname -f`'" + fi #if $purge_after_days is true, then append $purge_after_date to the variable $snapshot_tags if [[ -n $purge_after_days ]] then @@ -156,6 +162,8 @@ date_binary="" #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 name_tag_create=false +#sets the "InitiatingHost" tag set for a snapshot to false +hostname_tag_create=false #sets the user_tags feature to false - user_tag creates tags on snapshots - by default each snapshot is tagged with volume_id and current_data timestamp user_tags=false #sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfter" tag that is earlier than current date @@ -171,6 +179,7 @@ while getopts :s:c:r:v:t:k:pnu opt t) tag="$OPTARG";; k) purge_after_days="$OPTARG";; n) name_tag_create=true;; + h) hostname_tag_create=true;; 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;; From 348e628c572c1e37bbe42aeb77846cdc4188514e Mon Sep 17 00:00:00 2001 From: Jeff Vogt Date: Mon, 14 Oct 2013 19:58:36 -0700 Subject: [PATCH 2/3] forgot to add the new flag in getopts --- ec2-automate-backup/ec2-automate-backup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 8bef46c..07c4894 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -169,7 +169,7 @@ user_tags=false #sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfter" tag that is earlier than current date purge_snapshots=false #handles options processing -while getopts :s:c:r:v:t:k:pnu opt +while getopts :s:c:r:v:t:k:pnhu opt do case $opt in s) selection_method="$OPTARG";; From a1db669e6409058a300e302a24433f31c6af4c56 Mon Sep 17 00:00:00 2001 From: Jeff Vogt Date: Mon, 14 Oct 2013 20:01:38 -0700 Subject: [PATCH 3/3] forgot to remove an unneeded line from the copypaste --- ec2-automate-backup/ec2-automate-backup.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 07c4894..4c67bab 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -59,7 +59,6 @@ create_EBS_Snapshot_Tags() #if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags if $hostname_tag_create then - ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` snapshot_tags="$snapshot_tags --tag InitiatingHost='`hostname -f`'" fi #if $purge_after_days is true, then append $purge_after_date to the variable $snapshot_tags