Shells to create and remove AMI with associated snapshots which are older then 1 day.
Hi All,
I just make a shell to remove AMI with associated snapshots and I want to share this. I hope it will be useful for others............. TO CREATE AMI FROM AN EXISTING INSTANCE......... #!/bin/bash #Define EC2 home export EC2_HOME=/opt/aws/ec2_api_tools/ #Define JAVA home export JAVA_HOME=/usr/ #Collect the information of all Instances and store in /tmp/instancedetails file. $EC2_HOME/bin/ec2-describe-instances --aws-access-key=$access_key --aws-secret-key=$secret_key >/tmp/instancedetails #Grep the Instance ID echo "These are the instace IDs in your AWS A/C" grep INSTANCE /tmp/instancedetails|cut -f2 #Ask for Instance ID echo "Please give the Instance ID from which you want to create AMI." #Store user input in OPT read OPT #Ask for AMI name echo "Please give the AMI Name" #Store user input in AMI read AMI $EC2_HOME/bin/ec2-create-image -n "$AMI.`date +%m-%d`" $OPT --aws-access-key=$access_key --aws-secret-key=$secret_key >/tmp/amidetails #Collect Information of all AMI and store in /tmp/all-ami file $EC2_HOME/bin/ec2-describe-images -o self --aws-access-key=$access_key --aws-secret-key=$secret_key >/tmp/all-ami #Show the details of newly created AMI echo "This is the details of new AMI" grep "$AMI" /tmp/all-ami SHELL TO REMOVE AMI WITH THEIR ASSOCIATED SNAPSHOT WHICH ARE OLDER THEN 1 DAY #!/bin/bash #Define EC2 home export EC2_HOME=/opt/aws/ec2_api_tools/ #Define JAVA home export JAVA_HOME=/usr/ #Collect Information of all AMI and store in /tmp/all-ami file $EC2_HOME/bin/ec2-describe-images -o self --aws-access-key=$access_key --aws-secret-key=$Secret_Key >/tmp/all-ami #Collect Information about all SNAPSHOTS and store in /tmp/snaps file $EC2_HOME/bin/ec2-describe-snapshots --aws-access-key=$access_key --aws-secret-key=$Secret_Key >/tmp/snaps #get the date of 1 day ago and store in date_check_1d date_check_1d=`date +%m-%d --date '1 day ago'` #Get AMI ID of that instances, which are older then 1 day grep $date_check_1d /tmp/all-ami |cut -f 2 >/tmp/ami-to-delete #Display the AMI ID which are going to delete. echo "These AMIs with their associated Snapshots will be deleted" cat /tmp/ami-to-delete #Store the AMI ID in ATD(AMI TO DELETE) for ATD in $(cut -f 1 /tmp/ami-to-delete) do #First De-Register the AMI. $EC2_HOME/bin/ec2-deregister $ATD --aws-access-key=$access_key --aws-secret-key=$Secret_Key #Get the SNAP ID of that snapshot which was associated. SNAP_TO_DELETE=`grep "$ATD" /tmp/snaps | cut -f 2` #Now delete the snapshot $EC2_HOME/bin/ec2-delete-snapshot --aws-access-key --aws-secret-key $access_key $Secret_Key --region us-east-1 $SNAP_TO_DELETE echo "$SNAP_TO_DELETE" >> deleted-snap-list.txt done |
I like finding other AWS techies...I use a cron to delete snapshots older than 15 days via script.sh:
Code:
#!/bin/bash Code:
#!/bin/bash Please use [code][/code] tags. You can edit your original and put [code][/code] markups around any code (script|snippets or output). Thanks! |
Hi Habitual,
Thanks for your code. But I love to write pure bash based scripts without using any tool. I ve also created to remove snapshots older then 7 days. My privious script are made for Pugre AMIs with Snapshot. This is the script to remove snapshots older then 7 days. #!/bin/bash JAVA_HOME=/usr/ export JAVA_HOME EC2_HOME=/opt/aws export EC2_HOME #Get all snapshots with details, of current owner $EC2_HOME/bin/ec2-describe-snapshots --aws-access-key $access_key --aws-secret-key $secret_key --region us-east-1 >> /var/log/snapshots.log 2>&1 # Store 7 day ago date in "datecheck_7d" datecheck_7d=`date +%Y-%m-%d --date '7 days ago'` #Convert 7 day ago date in seconds in "datecheck_s_7d" datecheck_s_7d=`date --date="$datecheck_7d" +%s` # Loop to remove any snapshots older than 7 days #First cut the second column of snapshots.log....Which is SNAPSHOT-ID for SNAP_TO_DELETE in $(cut -f 2 /var/log/snapshots.log); do #Now Grep the date of SNAPSHT-ID and store in "datecheck_old" datecheck_old=`cat /var/log/snapshots.log | grep "$SNAP_TO_DELETE" | awk '{print $5}' | awk -F "T" '{printf "%s\n", $1}'` #Convert the date of SNAPSHOT-ID in to seconds. See %s in the manpage of date datecheck_s_old=`date --date="$datecheck_old" +%s` #check the value of snapshot date is less then or equal to 7 day ago from current date if (( $datecheck_s_old <= $datecheck_s_7d )); #if Yes then echo "deleting snapshot $SNAP_TO_DELETE ..." $EC2_HOME/bin/ec2-delete-snapshot -C $my_cert -K $my_key $SNAP_TO_DELETE echo $SNAP_TO_DELETE >> /var/log/deleted-snap-list.txt #if no else echo "not deleting snapshot $SNAP_TO_DELETE ..." echo $SNAP_TO_DELETE >> /var/log/pending-snap-list.txt fi done Quote:
|
Quote:
bash is what it's all about! "pure bash" I am not too sure what that means exactly. Interesting concept for me to pursue, I guess. :) |
All times are GMT -5. The time now is 08:37 PM. |