Take EBS Snapshot with 3 days of rentention

NOTE: This script was created for Chef build instances, so the log(/opt/log.txt) which takes the stdout output is the source where the instance id is fetched from. Also a 50GB EBS is also attached on the fly by CHEF

#!/bin/bash

C=`date +%Y-%m-%d –date=’3 days ago’`

###### To obtain instance ID #########

inst_id=`cat /opt/log.txt | grep Instance | awk {‘print $3’}`

####### To obtain volume ID #######

ec2-describe-volumes -K /opt/EC2_API_Certs/pk-4GV.pem -C /opt/EC2_API_Certs/cert-47OH3YSO.pem –region us-east-1 | grep $inst_id | grep /dev/sdi | awk ‘{print $2}’ > /opt/volID.txt

id=`cat /opt/volID.txt`

##### To create snapshots and log into snapshot_log.txt ######

ec2-create-snapshot $id -K /opt/EC2_API_Certs/pk-47OH.pem -C /opt/EC2_API_Certs/cert-47OH3Y.pem –region us-east-1 >> /opt/scripts/snapshot_log.txt

###### To put up list of volumes ######

ec2-describe-snapshots -K /opt/EC2_API_Certs/pk-47OHGV.pem -C /opt/EC2_API_Certs/cert-47OH3YSOZR.pem –region us-east-1 | grep $id >> /opt/scripts/snaplist

####### To sieve the snapids to be deleted into a file and delete them one by one ############

cat /opt/scripts/snaplist | grep $C | awk {‘print $2’} | sort -u > /opt/scripts/delsnaps

for i in `cat /opt/scripts/delsnaps`; do ec2-delete-snapshot $i -K /opt/EC2_API_Certs/pk-47OH3Y.pem -C /opt/EC2_API_Certs/cert-47.pem –region us-east-1; done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s