Created
December 4, 2013 17:31
-
-
Save Aricg/7791895 to your computer and use it in GitHub Desktop.
take snapshot_id volume_id snapshot_datestamp from ec2-describe-snapshots and remove from that list any IN-USE snapshots as described by ec2-describe-volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #Hey guys. This is really slow. with 1,000 snapshots and 15 excludes it's taking 40 seconds. | |
| #this is the full list of all of a clients snapshots prepared in the format: "snapshot_id volume_id snapshot_datestamp" | |
| #amazons describe-snapshots api does not contain information about whether a snapshot is IN-USE only describe-volumes has this information, so I must build an array of both datas and then compare them | |
| fulllist=$(amazon_describe_snapshots | grep SNAPSHOT | awk '{ print $2 " " $3 " " $5 }' | sed 's,\+.*,,g' | sort -k2) | |
| #example of what the full list looks like | |
| #snap-4b332d55 vol-044c9f6c 2013-11-15T05:03:46 | |
| #snap-a8b652b9 vol-044c9f6c 2013-11-16T05:04:23 | |
| #snap-bbd536aa vol-044c9f6c 2013-11-17T05:04:06 | |
| #snap-5b739d4a vol-044c9f6c 2013-11-18T05:04:22 | |
| #snap-5dca3e4c vol-044c9f6c 2013-11-19T05:04:31 | |
| #snap-35906224 vol-044c9f6c 2013-11-20T05:04:36 | |
| #snap-e56f96f4 vol-044c9f6c 2013-11-21T05:04:47 | |
| #snap-860ccb97 vol-044c9f6c 2013-11-22T05:04:31 | |
| #snap-85b77a94 vol-044c9f6c 2013-11-23T05:04:31 | |
| #snap-e74189f6 vol-044c9f6c 2013-11-24T05:04:08 | |
| #we must exclude the following snapshots_id's from the above list, the reason: they are currently in-use, so no matter how old they are we still don't want to try to delete them. | |
| exclusionlist=$(amazon_describe_volumes | grep "in-use" | grep snap | awk {'print $4'} | sort | uniq ) | |
| #example of what the exclusion list looks like | |
| #snap-85b77a94 | |
| #snap-3f1fc554 | |
| #snap-84c28fef | |
| #snap-89baa8a2 | |
| #snap-900156fb | |
| #snap-940156ff | |
| #snap-9ac28ff1 | |
| #snap-a8aae7c3 | |
| #snap-aeaae7c5 | |
| #snap-bc98d5d7 | |
| #Build the arrays | |
| getexclusionlist=() | |
| while read -d $'\n'; do | |
| getexclusionlist+=("$REPLY") | |
| done < <(echo "$exclusionlist") | |
| getfulllist=() | |
| while read -d $'\n'; do | |
| getfulllist+=("$REPLY") | |
| done < <(echo "$fulllist") | |
| #for each element of the array we built of the full list we --> | |
| for snapvoldate in "${getfulllist[@]}"; | |
| do | |
| #compare it to the list of snapshot_id's that need to be excluded --> | |
| for excludethis in "${getexclusionlist[@]}"; | |
| do | |
| #But wait, we only need the first element of our full list. --> | |
| #So build a temporary variable of only the first field of our full list array | |
| snaponly=$(echo $snapvoldate | awk '{print $1}') | |
| #If we find a match then this element of the full list must be excluded, also we are done here we leave this loop -> | |
| if [[ "$snaponly" == "$excludethis" ]]; then break | |
| fi | |
| done | |
| #If we just left the loop due to the above break then we know that $snaponly will equal $excludethis, if this is the case do nothing. | |
| #If they dont match, we must have made it to the end of the exclusion list and the snapshot_id never matched the exclusion list, so we can print it. | |
| if [[ "$snaponly" != "$excludethis" ]]; then excludedlist+="$snapvoldate"$'\n' | |
| fi | |
| done | |
| printfinishedlist=() | |
| while read -d $'\n'; do | |
| printfinishedlist+=("$REPLY") | |
| done < <(echo "$excludedlist") | |
| echo $excludedlist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment