LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shells to create and remove AMI with associated snapshots which are older then 1 day. (https://www.linuxquestions.org/questions/linux-newbie-8/shells-to-create-and-remove-ami-with-associated-snapshots-which-are-older-then-1-day-4175447977/)

h.dilshad 01-31-2013 06:28 AM

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

Habitual 01-31-2013 09:59 AM

I like finding other AWS techies...I use a cron to delete snapshots older than 15 days via script.sh:

Code:

#!/bin/bash
# JJ of cirrhus9.com
# Client: Domain.com
# Region us-west-1
# Keep 1 to 15 days snapshots
#export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0/jre
echo date
export JAVA_HOME=/usr/lib64/java
export EC2_HOME=/home/jj/Bin/ec2
/home/jj/Bin/ec2/bin/ec2-describe-snapshots -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1 | sort -r -k 5 | sed 1,15d | awk '{print "Deleting snapshot: " $2}; system("/home/jj/Bin/ec2/bin/ec2-delete-snapshot -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1  " $2)'

then I verify manually using a yad driven pop-up:
Code:

#!/bin/bash
################################################################
# Name:              AWS SnapsCount
# Version:            1310054340
# Author:            JJ of cirrhus9.com
# Date:                Thu Jul 07, 2011
# Requirements:        yad,ec2-api-tools
# Description:        Retrieves AWS Snapshots and counts them.
# ToDo:
################################################################
export EC2_HOME=/home/jj/Bin/ec2
#Local date/time stamp
var1=`date`

yad --image=/home/jj/Documents/cirrhus9/Legal/cirrhus9_logo_blue.png --no-buttons --undecorated --skip-taskbar --timeout=2
function get_snapshots()
{
var3=`echo -n "FTB Snapshots="$(/home/jj/Bin/ec2/bin/ec2-describe-snapshots -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1 | sort -r -k 5 | wc -l)`
}

touch /tmp/$$
( ( echo 1 ; while [ -f /tmp/$$ ] ; do sleep 1 ; done ; echo 100 ) | yad --progress --pulsate --auto-close --text="Counting Snapshots..." --width=150 --title="" --undecorated --no-buttons) & get_snapshots
rm /tmp/$$

#output
echo -e "$var1\n$var3"| yad --text-info --show-uri --width=300 --height=265 --center --name="EC2 Snapshots" --window-icon="/home/jj/Documents/favicon.ico" --button=Done
#EOF

YMMV.

Please use [code][/code] tags. You can edit your original and put [code][/code] markups around any code (script|snippets or output).

Thanks!

h.dilshad 02-01-2013 01:29 AM

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:

Originally Posted by Habitual (Post 4881431)
I like finding other AWS techies...I use a cron to delete snapshots older than 15 days via script.sh:

Code:

#!/bin/bash
# JJ of cirrhus9.com
# Client: Domain.com
# Region us-west-1
# Keep 1 to 15 days snapshots
#export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0/jre
echo date
export JAVA_HOME=/usr/lib64/java
export EC2_HOME=/home/jj/Bin/ec2
/home/jj/Bin/ec2/bin/ec2-describe-snapshots -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1 | sort -r -k 5 | sed 1,15d | awk '{print "Deleting snapshot: " $2}; system("/home/jj/Bin/ec2/bin/ec2-delete-snapshot -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1  " $2)'

then I verify manually using a yad driven pop-up:
Code:

#!/bin/bash
################################################################
# Name:              AWS SnapsCount
# Version:            1310054340
# Author:            JJ of cirrhus9.com
# Date:                Thu Jul 07, 2011
# Requirements:        yad,ec2-api-tools
# Description:        Retrieves AWS Snapshots and counts them.
# ToDo:
################################################################
export EC2_HOME=/home/jj/Bin/ec2
#Local date/time stamp
var1=`date`

yad --image=/home/jj/Documents/cirrhus9/Legal/cirrhus9_logo_blue.png --no-buttons --undecorated --skip-taskbar --timeout=2
function get_snapshots()
{
var3=`echo -n "FTB Snapshots="$(/home/jj/Bin/ec2/bin/ec2-describe-snapshots -K /home/jj/.ssh/Domain.com/pem/pk-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem -C /home/jj/.ssh/Domain.com/pem/cert-OOPQ5MLZTVMPLIT63VC6DXGCNX6SFPH3.pem --region us-west-1 | sort -r -k 5 | wc -l)`
}

touch /tmp/$$
( ( echo 1 ; while [ -f /tmp/$$ ] ; do sleep 1 ; done ; echo 100 ) | yad --progress --pulsate --auto-close --text="Counting Snapshots..." --width=150 --title="" --undecorated --no-buttons) & get_snapshots
rm /tmp/$$

#output
echo -e "$var1\n$var3"| yad --text-info --show-uri --width=300 --height=265 --center --name="EC2 Snapshots" --window-icon="/home/jj/Documents/favicon.ico" --button=Done
#EOF

YMMV.

Please use [code][/code] tags. You can edit your original and put [code][/code] markups around any code (script|snippets or output).

Thanks!


Habitual 02-01-2013 08:11 AM

Quote:

Originally Posted by h.dilshad (Post 4881988)
Hi Habitual,
Thanks for your code. But I love to write pure bash based scripts without using any tool.

Yeah, I did too. For years. I needed visuals as I aged, more inter-activity.

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.