Last active
December 16, 2015 17:29
-
-
Save Aricg/5470931 to your computer and use it in GitHub Desktop.
Uses ec2-describe-snapshots and ec2-delete-snapshot to order and delete snapshosts for a given account. to be used with https://gist.github.com/Aricg/5482068
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 | |
| numbertokeep=15 | |
| for zone in $(cat tmp_zones) | |
| do | |
| for x in $(find /home/ubuntu/KEYS/* -type f | grep ".key"); | |
| do | |
| key="--region "$zone" -C ${x%.*}.pem -K ${x%.*}.key" | |
| echo $key | |
| #if tmp_info is more than a day old. get it again | |
| #find -type f -name tmp_info -mtime +1 -exec rm {} + | |
| rm tmp_info_snap | |
| if [[ ! -e tmp_info_snap ]] | |
| then | |
| #get the snaphot-id volume-id and date created of each snapshot | |
| ec2-describe-snapshots -o self $key | grep SNAPSHOT | awk '{ print $2 " " $3 " " $5 }' | sed 's,\+.*,,g' | sort -k2 > tmp_info_snap | |
| fi | |
| #gets volume-id we need to iterate throught these | |
| getvol() { | |
| getvol=() | |
| while read -d $'\n'; do | |
| getvol+=("$REPLY") | |
| done < <(cat tmp_info_snap | awk '{ print $2 }' | sort | uniq ) | |
| } | |
| getvol "$@" | |
| #here we are a iteratin` | |
| for vol in "${getvol[@]}"; | |
| do | |
| #um. yeah. Im sorry... | |
| #Vol is the field seperator, which redues the number of fields from 3 to 2, we print only lines that have 2 fields. | |
| #next we use head - to keep $numbertokeep lines from being listed, anything else listed will get called for deletion. the final awk grabs the volume name and puts it in an array of variables "tbd" | |
| for tbd in $(cat tmp_info_snap | awk -v volume="$vol" 'BEGIN { FS=volume;} {if (NF=="2") print $1 $2;}' | head -n -"$numbertokeep" | awk '{print $1}') | |
| do | |
| echo "deleteing $tbd of $vol" >> /tmp/snaplog | |
| ec2-delete-snapshot $key "$tbd" | |
| done | |
| done | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment