列出在特定日期拍摄的所有 EC2 EBS 快照

List all EC2 EBS snapshots taken on a particular date

我正在尝试列出在特定日期拍摄的所有 EBS 卷快照,以便我可以通过跨区域的 bash 脚本自动执行副本,以实现更好的灾难恢复 我有另一个 bash 脚本,它创建所有正在使用的 EBS 卷的快照并删除所有超过 30 天的卷。我需要复制之前在另一个地区拍摄的所有照片。

我尝试了很多 jmespath 开关(没有给出任何输出)其中一些是:-

$aws ec2 describe-snapshots --query 'Sanpshots[?StartTime >= `2018-06-25`]|[?StartTime <= `2018-06-27`]'
$aws ec2 describe-snapshots --query 'Sanpshots[?StartTime == `2018-06-25`]

我看了很多页,但找不到特定日期的列表。 请建议一些开关、排序方法、链接或任何东西。 谢谢。

鉴于您需要一些计算“30 天前”的编程方法,您最好使用编程语言来执行此操作,例如:

import boto3
import pytz
from datetime import datetime, timedelta

# Get my AWS Account ID
myAccount = boto3.client('sts').get_caller_identity()['Account']

# Connect to EC2
client = boto3.client('ec2', region_name = 'ap-southeast-2')

# Get a list of snapshots for my AWS account (not all public ones)
snapshots = client.describe_snapshots(OwnerIds=[myAccount])['Snapshots']

# Find snapshots more than 30 days old
oldest_date = datetime.now(pytz.utc) - timedelta(days=30)
old_snapshots = [s for s in snapshots if s['StartTime'] < oldest_date]

# Delete the old snapshots
for s in old_snapshots:
  client.delete_snapshot(SnapshotId = s['SnapshotId'])

我从正确的文档中找到了 JMESpath 开关 here. 因此,为了搜索特定日期,我应用了一个在两个日期之间搜索的开关。例如:-

'Snapshots[?(StartTime >= `2018-06-27`) && (StartTime <= `2018-06-28`)]

“==”在用于完全匹配字符串的开关中不起作用的原因。

所以完整的字符串是:-

aws ec2 describe-snapshots --query 'Snapshots[?(StartTime >= `2018-06-27`) && (StartTime <= `2018-06-28`)].{ID:SnapshotId,ST:StartTime}' --output text --region $regionname