Advertisement
Dooley28

EC2 Snapshot Management

Apr 1st, 2011
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.95 KB | None | 0 0
  1. #Backup all instances
  2. for region in $(ec2-describe-regions |grep -Ev "ATTACHMENT" |cut -f2);do
  3.   for volid in $(ec2-describe-volumes --region $region |grep -Ev "ATTACHMENT" |cut -f2);do
  4.     echo "Creating snapshot from $volid in $region";
  5.     ec2-create-snapshot --region $region $volid
  6.   done;
  7. done;
  8.  
  9. is_older()
  10. {
  11.   DATE=$(date -d "$1" +%Y-%m-%d)
  12.   TODAY=$(date -d "$(date +%Y-%m-%d) $2 $3 $4 $5" +%s)
  13.   COMPARE=$(date -d "$DATE" +%s)
  14.   test $(($TODAY - $COMPARE)) -le 0 && return 1 || return 0
  15. }
  16.  
  17. #Purge old records
  18. for region in $(ec2-describe-regions |grep -Ev "ATTACHMENT" |cut -f2);do
  19.   for snapid in $(ec2-describe-snapshots --region $region |grep -Ev "ATTACHMENT"| cut -f2 );do
  20.     snapdate=$(ec2-describe-snapshots --region $region $snapid | cut -f5 | cut -d "T" -f1)
  21.     if is_older $snapdate -7 days ;then
  22.       echo "Purging snapshot $snapid in $region";
  23.       ec2-delete-snapshot --region $region $snapid
  24.     fi
  25.   done;
  26. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement