2017-03-08 110 views

回答

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])