删除早于月份的AWS EC2快照

时间:2017-03-16 13:20:45

标签: amazon-web-services amazon-ec2

以下给定的命令是否可以删除早于月份的AWS EC2快照。

aws describe-snapshots | grep -v(日期+%Y-%m - )| grep snap- | awk'{print $ 2}'| xargs -n 1 -t aws delete-snapshot

3 个答案:

答案 0 :(得分:7)

您的命令主要因为拼写错误而无法工作:aws describe-snapshots应为aws ec2 describe-snapshots

无论如何,除了aws之外,你可以在没有任何其他工具的情况下做到这一点:

snapshots_to_delete=($(aws ec2 describe-snapshots --owner-ids xxxxxxxxxxxx --query 'Snapshots[?StartTime>=`2017-02-15`].SnapshotId' --output text))
echo "List of snapshots to delete: $snapshots_to_delete"

# actual deletion
for snap in $snapshots_to_delete; do
  aws ec2 delete-snapshot --snapshot-id $snap
done

确保您始终知道要删除的内容。例如,echo $snap 此外,将--dry-run添加到aws ec2 delete-snapshot可以向您显示请求中没有错误。

修改

第一个命令有两点需要注意:

  1. --owner-ids - 您的帐户唯一ID。可以在AWS控制台的右上角轻松找到:Support->Support Center->Account Number xxxxxxxxxxxx

  2. --query - 仅获取在指定日期之后创建的快照的JMESPath查询(例如:2017-02-15):Snapshots[?StartTime>=`2017-02-15`].SnapshotId

答案 1 :(得分:1)

+1到@ roman-zhuzha让我靠近。当$snapshots_to_delete不能解析成由空格分隔的一长串快照时,我确实遇到了麻烦。

下面的这个脚本确实将它们解析为一串长长的快照ID,这些快照ID由awscli 1.16在bash中的我的Ubuntu(可信任)14.04上用空格分隔:

#!/usr/bin/env bash

dry_run=1
echo_progress=1

d=$(date +'%Y-%m-%d' -d '1 month ago')
if [ $echo_progress -eq 1 ]
then
  echo "Date of snapshots to delete (if older than): $d"
fi

snapshots_to_delete=$(aws ec2 describe-snapshots \
    --owner-ids xxxxxxxxxxxxx \
    --output text \
    --query "Snapshots[?StartTime<'$d'].SnapshotId" \
)
if [ $echo_progress -eq 1 ]
then
  echo "List of snapshots to delete: $snapshots_to_delete"
fi


for oldsnap in $snapshots_to_delete; do

  # some $oldsnaps will be in use, so you can't delete them
  # for "snap-a1234xyz" currently in use by "ami-zyx4321ab"
  # (and others it can't delete) add conditionals like this

  if [ "$oldsnap" = "snap-a1234xyz" ] ||
     [ "$oldsnap" = "snap-c1234abc" ]
  then
    if [ $echo_progress -eq 1 ]
    then
       echo "skipping $oldsnap known to be in use by an ami"
    fi
    continue
  fi

  if [ $echo_progress -eq 1 ]
  then
     echo "deleting $oldsnap"
  fi

  if [ $dry_run -eq 1 ]
  then
    # dryrun will not actually delete the snapshots
    aws ec2 delete-snapshot --snapshot-id $oldsnap --dry-run
  else
    aws ec2 delete-snapshot --snapshot-id $oldsnap
  fi
done

根据需要切换这些变量:

dry_run=1           # set this to 0 to actually delete
echo_progress=1     # set this to 0 to not echo stmnts

date -d字符串更改为您想要删除“早于”的天数,月数或年数的可读版本:

d=$(date +'%Y-%m-%d' -d '15 days ago')  # half a month

找到您的帐户ID,并将这些XXXX更新为该数字:

    --owner-ids xxxxxxxxxxxxx \

以下是可以找到该号码的示例:

enter image description here


如果在cron中运行此程序,则只希望看到错误和警告。经常会警告说正在使用快照。这两个示例快照 id(snap-a1234xyz,snap-c1234abc)将被忽略,因为它们否则将显示类似以下内容:

  

调用DeleteSnapshot操作时发生错误(InvalidSnapshot.InUse):ami-zyx4321ab当前正在使用快照snap-a1234xyz

有关如何处理此输出的信息,请参见“ snap-a1234xyx”示例快照ID附近的评论。


And don't forget to check on the handy examples and references in the 1.16 aws cli describe-snapshots manual

答案 2 :(得分:0)

您可以在'--owner-ids'中使用'self'并使用此单线命令删除在特定日期(例如2018-01-01)之前创建的快照:

for i in $(aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?StartTime<=`2018-01-01`].SnapshotId' --output text); do echo Deleting $i; aws ec2 delete-snapshot --snapshot-id $i; sleep 1; done;