AWS:如何安排1周后删除EBS快照?

问题描述 投票:0回答:2

我计划使用的CloudWatch自动创建快照EBS 如何安排自动删除旧的快照?

amazon-web-services snapshot cloudwatch
2个回答
1
投票

这可能会有帮助。这是一个Python程序我写了通吃卷的快照,并保持最后2个快照。

你可以在一个EC2实例上运行这样的程序,或将其转换为预定AWS lambda函数运行。

#!/usr/bin/env python

import boto.ec2, os

MAX_SNAPSHOTS = 2   # Number of snapshots to keep

# Connect to EC2 in this region
connection = boto.ec2.connect_to_region('<insert region here>')

# Get a list of all volumes
volumes = connection.get_all_volumes()

# Create a snapshot of each volume
for v in volumes:
  connection.create_snapshot(v.id)

  # Too many snapshots?
  snapshots = v.snapshots()
  if len(snapshots) > MAX_SNAPSHOTS:

    # Delete oldest snapshots, but keep MAX_SNAPSHOTS available
    snap_sorted = sorted([(s.id, s.start_time) for s in snapshots], key=lambda k: k[1])
    for s in snap_sorted[:-MAX_SNAPSHOTS]:
      print "Deleting snapshot", s[0]
      connection.delete_snapshot(s[0])

0
投票

您可以拍摄快照,并把标签,如“DELETEON:”这些快照。

写另一个lambda其在此标记的基础上读快照并删除它在特定的日期。还有就是在botocore文件在此详述:https://botocore.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

© www.soinside.com 2019 - 2024. All rights reserved.