LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2013, 07:28 AM   #1
h.dilshad
LQ Newbie
 
Registered: Jan 2013
Posts: 17

Rep: Reputation: Disabled
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
 
Old 01-31-2013, 10:59 AM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,163
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
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!
 
Old 02-01-2013, 02:29 AM   #3
h.dilshad
LQ Newbie
 
Registered: Jan 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
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 View Post
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!
 
Old 02-01-2013, 09:11 AM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,163
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by h.dilshad View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to create Slackware AMI image for amazon AWS? kite Slackware 4 02-19-2013 03:19 PM
[SOLVED] create my own snapshots/backups kb2tfa Linux - Software 3 04-17-2012 02:42 PM
cron job to create snapshots not working spoovy Solaris / OpenSolaris 4 11-15-2011 05:24 AM
[SOLVED] can't create more tan 16 snapshots andres-m77 Slackware 5 10-20-2010 03:13 PM
LXer: How to create, bundle, upload and access a custom Debian AMI using ubuntu LXer Syndicated Linux News 0 10-15-2009 12:51 AM


All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration