2

I have to do something like this :

while ( condition)

do
 wait 

I have to do this is in linux. While the state of snapshot is pending it should wait.

Content of ec2-describe snapshot is :

SNAPSHOT    snap-c7f3   vol-f6a0    completed   2013-12-04T09:24:50+0000    100%    109030037527    10  2013-12-04: Daily Backup for SaMachine (VolID:vol-f09a0 InstID:i-2604)
SNAPSHOT    snap-c7df9  vol-3f6b    completed   2013-12-04T09:24:54+0000    100%    109030037527    10  2013-12-04: Daily Backup for sa_test_VPC (VolID:vol-3InstID:i-e1c46)

How to do this? How should I use grep and all that ?

#!/bin/bash

# Setting the environmental variables

export EC2_HOME=/opt/ec2/tools
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export SOURCE_REGION="us-west-2"
export DESTINATION_REGION="us-east-1" 


# Set the variable today-date to current date
today_date=`date +%Y-%m-%d`
echo "$today_date"


# Set the variable date_dir to yesterday's date
date_dir=$(date +%Y-%m-%d -d '-1 days')
echo "$date_dir"


#First delete all snapshots older than one day 
#Create a file with all scheduled snapshots

echo "First delete all snapshots older than one day"
echo "Create a file with all scheduled snapshots"
read -rsp $'Press enter to continue...\n'
ec2-describe-snapshots | grep -i "$date_dir">"$EC2_HOME/SnapshotsDOW_$today_date"


#Delete snapshots for older backups

echo "Delete snapshots for older backups"
read -rsp $'Press enter to continue...\n'
for snapshot in $(cat "$EC2_HOME/SnapshotsDOW_$today_date" | awk '{print $2}')
  do
      ec2-delete-snapshot $snapshot
done


#Now create a snapshot for every attached volume to every instance
#Create a file with all attached volumes

echo "Create a file with all attached volumes"
read -rsp $'Press enter to continue...\n'
#ec2-describe-volumes   | grep -i "attached" >"$EC2_HOME/ActiveVolumes_$today_date"


#Create a file with all instances

echo "Create a file with all instances"
read -rsp $'Press enter to continue...\n'
ec2-describe-instances | grep -i "tag" | grep -i "name" >"$EC2_HOME/Instances_$today_date"


#Create snapshots of all attached volumes

echo "Create snapshots of all attached volumes"
read -rsp $'Press enter to continue...\n'
awk '{print $2, $3}' "$EC2_HOME/ActiveVolumes_$today_date" | while read vol_id inst_id; do
    awk '{print $3, $5}' "$EC2_HOME/Instances_$today_date" | while read inst_id2 name; do
        if test "$inst_id" = "$inst_id2"; then
            echo ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
             ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
        fi
    done
done



#Create a file with all latest snapshots

echo "Create a file with all latest snapshots"
read -rsp $'Press enter to continue...\n'
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -rk 5 | awk '{print substr($5, 1, 10); exit}')
ec2-describe-snapshots | grep "^SNAPSHOT.*$latestdate" > "$EC2_HOME/SnapshotsLatest_$today_date"


#Copy the snapshot across multiple regions.

echo "Copy the snapshot across multiple regions."
read -rsp $'Press enter to continue...\n'
for snapshot in $(cat "$EC2_HOME/SnapshotsLatest_$today_date" | awk '{print $2}') 
do 
  ec2-copy-snapshot -r $SOURCE_REGION -s $snapshot -region $DESTINATION_REGION
done


#Send the latest snapshot details to user using mail command.
echo "Send the latest snapshot details to user using mail command"
read -rsp $'Press enter to continue...\n'
mail -s 'VM Snapshot $today_date' jp.com <  "$EC2_HOME/SnapshotsLatest_$today_date"

read -rsp $'Press enter to exit...\n'
6
  • What command do you use to get that output?
    – Smurker
    Commented Dec 11, 2013 at 9:59
  • ec2-describe-snapshots ( an ec2-command line utility ) Commented Dec 11, 2013 at 10:02
  • Do you need all snapshots to be completed or are you just looking for a specific snapshot?
    – Smurker
    Commented Dec 11, 2013 at 10:03
  • no all the snapshots should be completed. I am trying something like this but not working : while(ec2-describe-snapshots | grep -i "status" !="completed") do echo "sleep" sleep 5 done Commented Dec 11, 2013 at 10:07
  • Hang on, i'll write something up for you.
    – Smurker
    Commented Dec 11, 2013 at 10:11

3 Answers 3

2

Writing that while loop is as simple as this:

while ec2-describe-snapshots | grep -q '^SNAPSHOT.*pending'
do
    echo "There are pending SNAPSHOTs, waiting..."
    sleep 10
done
echo "No pending SNAPSHOTs."
3
  • Hi janos, i am again writing shell script and need your help. I have posted a new quesion kindly check. Thanks Commented Mar 26, 2014 at 3:03
  • please have a look at my questions. Im stuck only you can help me :(\ Commented Mar 26, 2014 at 6:04
  • @jaypal there is more than capable to help you. But you have to be more clear. A LOT more clear. Please read this article. And appreciate it. And follow it. It's crucial. You will struggle getting answers here until you fully appreciate that article. There tend to be 10+ comments on your questions/answers, that's not normal here, and a waste of time for those answering you. You also need better understanding of the shell. Take the time and learn the basics. I have to go now, good luck.
    – janos
    Commented Mar 26, 2014 at 6:34
2

If you can put it in a shellscript, here's how i'd do it.

Edit

So after reading your current code this is what i would do. You'll have to test it yourself though.

# This is where you're creating your snapshots...

# Setup the command.
command=`ec2-describe-snapshots | grep pending | wc -l`

# Check if we have any pending snapshots at all.
if [ $command == "0" ]
then
        echo "No snapshots are pending."
        ec2-describe-snapshots
else
        # Wait for the snapshot to finish.
        while [ $command != "0" ]
        do
                # Communicate that we're waiting.
                echo "There are $command snapshots waiting for completion."
                sleep 5

                # Re run the command.
                command=`ec2-describe-snapshots | grep pending | wc -l`
        done

        # Snapshot has finished.
        echo "Snapshots are finished."
fi       

# This is where you're writing snapshots to file...

Old script kept for reference

#!/bin/sh

waitForSnapshot() {
        # Make sure we actually passed a snapshot to grep for.
        if [ -n "$1" ]
        then
                # Setup the command.
                command=`ec2-describe-snapshots | grep $1 | awk '{print $4}'`

                # Wait for the snapshot to finish.
                while [ $command != "completed" ]
                do
                        sleep 1

                        # Re run the command.
                        command=`ec2-describe-snapshots | grep $1 | awk '{print $4}'`
                done

                # Snapshot has finished.
                echo "Snapshot '$1' has finished."
        else
                echo "No snapshot was passed to us."
        fi

}

waitForSnapshot "snap-c7f3"
29
  • it will work for a single snapshot only or all the snapshots returned by the command? Commented Dec 11, 2013 at 10:22
  • and why you have made it in a function. please dont make it as a function. there is no function in my script Commented Dec 11, 2013 at 10:23
  • This works for a single snapshot. The reason i made it into a function was so you could reuse the same code to wait for any number of snapshots of your choice. You'd just call waitForSnapshot "snapshotName" as many times as you'd like.
    – Smurker
    Commented Dec 11, 2013 at 10:25
  • but i have to test this for multiple snapshot. i cant pass the value manually as parameter. please suggest something else Commented Dec 11, 2013 at 10:26
  • Sure, i'd love to help you. So you say you need multiple snapshots, but you don't want to input them manually. How do you know what snapshots to test? Is it all the snapshots from the output of the command ec2-describe-snapshots? Or do you get them from another command?
    – Smurker
    Commented Dec 11, 2013 at 10:33
1
until ec2-describe-snapshots | grep -q ' completed '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."

If you need all the snapshots to be completed, you could use instead:

until ! ec2-describe-snapshots | grep -q ' pending '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."

UPDATE

And, to handle also no output case:

until [ -n "`ec2-describe-snapshots`" ]
do
    echo "snapshot not yet started, still waiting..."
    sleep 10
done
until ! ec2-describe-snapshots | grep -q ' pending '
do
    echo "snapshot is pending, still waiting..."
    sleep 10
done
echo "snapshot completed."
7
  • @macros : error : cat: ec2-describe-snapshots: No such file or directory snapshot is pending, waiting... Commented Dec 11, 2013 at 10:14
  • Sorry, don't use 'cat '... I forgot to remove it after my test with command output you gave... ;-) I did already corrected answer...
    – MarcoS
    Commented Dec 11, 2013 at 10:17
  • no not working the loop is going on even if the snapshot is completed. and when there is no snapshot in repository even then its working Commented Dec 11, 2013 at 10:20
  • Strange, if the command "ec2-describe-snapshots" outputs the text you gave, it works, for me... And, sorry, I don't really understand what you mean by "when there is no snapshot in repository even then its working"... Probably you should specify the output of the command when there is no snapshot in repository...
    – MarcoS
    Commented Dec 11, 2013 at 10:36
  • 1
    Please check my last update... If it doesn't work for you it would be useful to know some more details about the error...
    – MarcoS
    Commented Dec 11, 2013 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.