我是python的新手,如果有人与我分享一个示例脚本,那将会非常有帮助 获取每个AWS卷的最新快照ID#。
我正在使用AWS api。
答案 0 :(得分:3)
这是我写的一个Python脚本,它连接到一个区域并为每个卷创建快照,然后删除除最近的N个快照之外的快照。
您将看到快照按start_time
排序,然后快照ID用于删除最早的快照:
#!/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('us-west-2')
# 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])
这是snap_sorted
分配,可让您找到最早的快照。