【问题标题】:List all EC2 EBS snapshots taken on a particular date列出在特定日期拍摄的所有 EC2 EBS 快照
【发布时间】:2018-12-06 20:54:15
【问题描述】:

我正在尝试列出在特定日期拍摄的所有 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`]

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

【问题讨论】:

    标签: amazon-web-services amazon-ec2 boto3 aws-cli amazon-ebs


    【解决方案1】:

    鉴于您需要某种编程方式来计算“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'])
    

    【讨论】:

    • 一如既往地感谢约翰。但我不需要删除 30 天前的列表,我需要在特定日期(当前日期后 2 天)创建的快照列表,以便我可以将它们复制到另一个区域。这就是为什么我正在寻找 jmespath 开关或任何排序方式。
    【解决方案2】:

    我想出了来自 doc right here. 的 JMESpath 开关 因此,为了搜索特定日期,我应用了在两个日期之间搜索的开关。例如:-

    '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
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2016-12-26
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多