From eee4ad0637648f02da3c767709c9a995ac7773e5 Mon Sep 17 00:00:00 2001 From: Colin Johnson Date: Mon, 24 Sep 2012 23:34:15 +0000 Subject: [PATCH] ec2-automate-backup now includes support for purging old snapshots. --- .../ec2-automate-backup-Test-Plan.txt | 5 ++- ec2-automate-backup/README.txt | 3 +- ec2-automate-backup/ec2-automate-backup.sh | 41 +++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/aws-missing-tools-resources/QA/ec2-automate-backup/ec2-automate-backup-Test-Plan.txt b/aws-missing-tools-resources/QA/ec2-automate-backup/ec2-automate-backup-Test-Plan.txt index e0bbce9..e465048 100644 --- a/aws-missing-tools-resources/QA/ec2-automate-backup/ec2-automate-backup-Test-Plan.txt +++ b/aws-missing-tools-resources/QA/ec2-automate-backup/ec2-automate-backup-Test-Plan.txt @@ -23,6 +23,7 @@ $ec2ab_exec -v "$volume_1_id $volume_2_id" -n $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 14 #expected result - snapshot only taken of volume_1 and volume_2 each with tags "PurgeAfter=$date_current+14" and "PurgeAllow=true" # -#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfter=1" +#test backing up only EBS volume with IDs "x" and "y" and tagging them with a tag "PurgeAfter=14" $ec2ab_exec -v "$volume_1_id $volume_2_id" -k 14 -n -#expected result - snapshot only taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current \ No newline at end of file +#expected result - snapshot only taken of volume_1 and volume_2 each with tag "name" set to ec2ab_$volume_id_$date_current +# \ No newline at end of file diff --git a/ec2-automate-backup/README.txt b/ec2-automate-backup/README.txt index 567a4eb..d0c7be3 100644 --- a/ec2-automate-backup/README.txt +++ b/ec2-automate-backup/README.txt @@ -23,7 +23,8 @@ ec2-automate-backup requires one of the following two parameters be provided: -r - the region that contains the EBS volumes for which you wish to have a snapshot created. -s - the selection method by which EBS volumes will be selected. Currently supported selection methods are "volumeid" and "tag." The selection method "volumeid" identifies EBS volumes for which a snapshot should be taken by volume id whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a key=value format tag. -n - tag snapshots "Name" tag as well as description --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 +-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. # # Potential Uses and Methods of Use: # diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 3c03db5..5984909 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -1,13 +1,13 @@ #!/bin/bash - # Author: Colin Johnson / colin@cloudavail.com -# Date: 2012-09-21 +# Date: 2012-09-24 # Version 0.1 # License Type: GNU GENERAL PUBLIC LICENSE, Version 3 # #confirms that executables required for succesful script execution are available prerequisite_check() { - for prerequisite in basename ec2-create-snapshot ec2-create-tags + for prerequisite in basename ec2-create-snapshot ec2-create-tags ec2-describe-snapshots ec2-delete-snapshot date 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 @@ -67,6 +67,35 @@ create_EBS_Snapshot_Tags() fi } +purge_EBS_Snapshots() +{ + #snapshot_tag_list is a string that contains all snapshots with either the key PurgeAllow or PurgeAfter set + snapshot_tag_list=`ec2-describe-tags --show-empty-fields --region $region --filter resource-type=snapshot --filter key=PurgeAllow,PurgeAfter` + #snapshot_purge_allowed is a list of all snapshot_ids with PurgeAllow=true + snapshot_purge_allowed=`echo "$snapshot_tag_list" | grep .*PurgeAllow'\t'true | cut -f 3` + + for snapshot_id_evaluated in $snapshot_purge_allowed + do + #gets the "PurgeAfter" date which is in UTC with YYYY-MM-DD format (or %Y-%m-%d) + purge_after_date=`echo "$snapshot_tag_list" | grep .*$snapshot_id_evaluated'\t'PurgeAfter.* | cut -f 5` + #if purge_after_date is not set then we have a problem. Need to alter user. + if [[ -z $purge_after_date ]] + #Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfter date. + then echo "A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfter=YYYY-MM-DD\" date. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2 + else + #convert both the date_current and purge_after_date into epoch time to allow for comparison + date_current_epoch=`date -j -f "%Y-%m-%d" "$date_current" "+%s"` + purge_after_date_epoch=`date -j -f "%Y-%m-%d" "$purge_after_date" "+%s"` + #perform compparison - if $purge_after_date_epoch is a lower number than $date_current_epoch than the PurgeAfter date is earlier than the current date - and the snapshot can be safely removed + if [[ $purge_after_date_epoch < $date_current_epoch ]] + then + echo "The snapshot \"$snapshot_id_evaluated\" with the Purge After date of $purge_after_date will be deleted." + ec2-delete-snapshot --region $region $snapshot_id_evaluated + fi + fi + done +} + #calls prerequisitecheck function to ensure that all executables required for script execution are available prerequisite_check @@ -117,4 +146,10 @@ do ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` fi create_EBS_Snapshot_Tags -done \ No newline at end of file +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 \ No newline at end of file