删除早于一个月的AWS EC2快照 [英] Delete older than month AWS EC2 snapshots

查看:42
本文介绍了删除早于一个月的AWS EC2快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的命令是否可以删除早于一个月的AWS EC2快照?

Is this below given command will work or not to delete older than month AWS EC2 Snapshot.

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

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

推荐答案

您的命令大部分不会因为输入错误而起作用:aws describe-snapshots应该为aws ec2 describe-snapshots.

Your command won't work mostly because of a typo: aws describe-snapshots should be aws ec2 describe-snapshots.

无论如何,您都可以使用aws以外的任何其他工具来做到这一点:

Anyway, you can do this without any other tools than 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可以向您显示请求中没有错误.

Make sure you always know what are you deleting. By echo $snap, for example.
Also, adding --dry-run to aws ec2 delete-snapshot can show you that there are no errors in request.

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

There are two things to pay attention at in the first command:

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

  1. --owner-ids - you account unique id. Could easily be found manually in top right corner of AWS Console: Support->Support Center->Account Number xxxxxxxxxxxx

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

--query - JMESPath query which gets only snapshots created later than specified date (e.g.: 2017-02-15): Snapshots[?StartTime>=`2017-02-15`].SnapshotId

这篇关于删除早于一个月的AWS EC2快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆