[英]how to get the latest aws volume snapshot id# using python/boto?
I am new to python , it will be very help if some one share me a sample script to 我是python的新手,如果有人分享给我一个示例脚本,将会非常有帮助
get the latest snapshot ID# of each AWS volumes. 获取每个AWS卷的最新快照ID#。
I am using AWS api. 我正在使用AWS api。
Here's a Python script I wrote that connects to a region and makes a snapshot of each volume, then deletes snapshots except for the most recent N snapshots. 这是我编写的Python脚本,它连接到区域并为每个卷制作快照,然后删除快照(最近的N个快照除外)。
You'll see that the snapshots are sorted by start_time
and the snapshot ID is then used to delete the oldest snapshots: 您将看到快照按
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])
It's the snap_sorted
assignment that lets you find the oldest snapshots. 通过
snap_sorted
分配,您可以找到最早的快照。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.