使用 shell 脚本创建和删除 Amazon EC2 卷

使用 shell 脚本创建和删除 Amazon EC2 卷

我正在使用以下脚本定期备份 Amazon AWS 云服务上的卷。它会创建卷的新快照,并应删除两天前的快照。它可以很好地创建新快照,但在删除旧快照时,它做得不对。

#!/bin/bash
#
# File: vol-snapshot.sh
TODAY=`date +%m-%d-%Y`
echo "================================================"
echo "Starting SNAPSHOT creation and deletion process for $TODAY"
echo ""
echo "The script will create a snapshot of every single volume"
echo "It will delete snapshots older than two days"
echo ""
echo "Setting ENVIRONMENTAL VARIABLES FOR ec2"

##
#
export EC2_HOME='/usr/local/ec2'  # Make sure you use the API tools, not the     AMI tools
export EC2_BIN=$EC2_HOME/bin
export EC2_PRIVATE_KEY=$EC2_HOME/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
export EC2_CERT=$EC2_HOME/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
export REGION=us-east-1d
export PATH=$PATH:$EC2_BIN
export OLD=`date +%m-%d-%Y --date '2 days ago'`
##
#

#
# To find the current location of JAVA_HOME, try env | grep JAVA_HOME
# It's necessary to put this environment variable in here because
# cron will not have access to your standard environment variables.
export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
##

## Get Volumes
VOLUMES=`ec2-describe-volumes | grep VOLUME | cut -f 2`
echo "The volumes are: $VOLUMES"
echo ""

echo "===================================="
echo "Creating snapshots of volumes: $VOLUMES"
echo ""
for volume in $VOLUMES
do 
  ec2-create-snapshot -C $EC2_CERT -K $EC2_PRIVATE_KEY -d "Creating     Snapshots for $TODAY" $volume
done

echo""
echo "====================================="
echo "Deleting snapshots older than two days for $VOLUMES"
echo ""
for volume in $VOLUMES
do
OLDEST=`ec2-describe-snapshots -C $EC2_CERT -K $EC2_PRIVATE_KEY | grep     $volume | grep $OLD | sed -e 's/.*snap/snap/' | sed -e 's/\t.*//'`
    if [ "x$OLDEST" != "x" ]; then
        ec2-delete-snapshot -C $EC2_CERT -K $EC2_PRIVATE_KEY $OLDEST
        else
        echo "No other snapshots to delete using this script."
    fi
done
echo "The end of script."

下面的行只是提供了仅两天前的快照,但不提供超过两天的快照。

OLDEST=`ec2-describe-snapshots -C $EC2_CERT -K $EC2_PRIVATE_KEY | grep $volume | grep $OLD | sed -e 's/.*snap/snap/' | sed -e 's/\t.*//'

另外,这应该放在 if 之前的 for 循环下吗?

如果有人能在这里帮助我,我将非常高兴。

答案1

我只是算出要保留多少个最近的快照(本例中为 28 个,包括我刚刚创建的仍处于“待处理”状态的快照),然后删除任何比该快照更旧的快照:

for SNAPSHOT in `ec2-describe-snapshots | grep $THIS_EBS_VOLUME | grep -v pending | head -n -27 | awk '{print $2}'` ; do
  ec2-delete-snapshot $SNAPSHOT
done

答案2

时间戳的构造使得字符串按时间顺序排序。所以你可以用$TIMESTAMP<找到较旧的$CUTOFF

  • 用于ec2-describe-snapshots -C $EC2_CERT -K $EC2_PRIVATE_KEY | grep $volume获取快照列表。
  • Foreach快照:
    • 提取时间戳
    • 如果时间戳<$OLD删除它

答案3

我结合了来自不同地点的一些想法,所以我想我会分享我用来删除旧快照的内容,以防它可以帮助其他人。

    ec2-describe-snapshots | awk '{print $5 "\t" $2}' > /backups/tmp/snapshot_info_start.txt
    grep -v '^[A-Za-z]' /backups/tmp/snapshot_info_start.txt > /backups/tmp/snapshot_info.txt

    cat /backups/tmp/snapshot_info.txt | while read obj0

    do
            Snapshot_Name=`cat /backups/tmp/snapshot_info.txt | grep "$obj0" | awk '{print $2}'`
            Snapshot_Old=`cat /backups/tmp/snapshot_info.txt | grep "$Snapshot_Name" | awk '{print $1}'`
            Snapshot_Old_s=`date "--date=$Snapshot_Old" +%s`

            if (($Snapshot_Old_s <= $SnapCheck_21_Day_s)) ;
            then
                    echo "Deleting Snapshot $Snapshot_Name ... "
                    ec2-delete-snapshot $Snapshot_Name

            else
                    echo "Not deleting snapshot $Snapshot_Name ...  "

            fi

相关内容