[英]Delete older than month AWS EC2 snapshots
下面给出的命令是否可以删除早于月份的 AWS EC2 快照。
aws 描述快照 | grep -v(日期+%Y-%m-)| grep 快照- | awk '{打印 $2}' | xargs -n 1 -t aws 删除快照
您的命令将无法正常工作,主要是因为输入错误: 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
可以表明请求中没有错误。
编辑:
第一个命令有两点需要注意:
--owner-ids
- 您帐户的唯一 ID。 可以很容易地在 AWS 控制台的右上角手动找到: Support->Support Center->Account Number xxxxxxxxxxxx
--query
- JMESPath 查询仅获取晚于指定日期创建的快照(例如:2017-02-15): Snapshots[?StartTime>=`2017-02-15`].SnapshotId
+1 @roman-zhuzha 让我接近。 当$snapshots_to_delete
没有解析成一长串由空格分隔的快照时,我确实遇到了麻烦。
下面的这个脚本确实将它们解析为一长串快照 ID,在我的 Ubuntu (trusty) 14.04 上用 awscli 1.16 在 bash 中用空格分隔:
#!/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 \
以下是您可以在哪里找到该号码的示例:
如果在 cron 中运行它,您只想看到错误和警告。 一个常见的警告是有快照正在使用中。 两个示例快照ID(snap-a1234xyz、snap-c1234abc)被忽略,因为它们会打印如下内容:
调用DeleteSnapshot操作时出现错误(InvalidSnapshot.InUse):快照snap-a1234xyz当前正在被ami-zyx4321ab使用
有关如何处理此输出,请参阅“snap-a1234xyx”示例快照 ID 附近的注释。
您可以在“--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;
日期条件必须在括号 () 内
aws ec2 describe-snapshots \
--owner-ids 012345678910 \
--query "Snapshots[?(StartTime<='2020-03-31')].[SnapshotId]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.