我必须做这样的事情:
while ( condition)
do
wait
我必须在linux中这样做。虽然快照状态正在等待,但它应该等待。
ec2-describe快照的内容是:
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)
怎么做?我应该如何使用grep以及所有这些?
#!/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'
答案 0 :(得分:2)
写while
循环就像这样简单:
while ec2-describe-snapshots | grep -q '^SNAPSHOT.*pending'
do
echo "There are pending SNAPSHOTs, waiting..."
sleep 10
done
echo "No pending SNAPSHOTs."
答案 1 :(得分:1)
until ec2-describe-snapshots | grep -q ' completed '
do
echo "snapshot is pending, still waiting..."
sleep 10
done
echo "snapshot completed."
如果您需要完成所有快照,则可以改为使用:
until ! ec2-describe-snapshots | grep -q ' pending '
do
echo "snapshot is pending, still waiting..."
sleep 10
done
echo "snapshot completed."
<强>更新强>
并且,处理也没有输出案例:
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."
答案 2 :(得分:1)
如果你可以把它放在一个shellcript中,我就是这样做的。
修改强>
所以读完你当前的代码后,这就是我要做的。你必须自己测试一下。
# 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...
旧脚本留作参考
#!/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"