【发布时间】:2015-05-05 01:13:12
【问题描述】:
我是 Python 的初学者,我编写了一个 Python 脚本,它拍摄指定卷的快照,然后只保留为该卷请求的快照数量。
#Built with Python 3.3.2
import boto.ec2
from boto.ec2.connection import EC2Connection
from boto.ec2.regioninfo import RegionInfo
from boto.ec2.snapshot import Snapshot
from datetime import datetime
from functools import cmp_to_key
import sys
aws_access_key = str(input("AWS Access Key: "))
aws_secret_key = str(input("AWS Secret Key: "))
regionname = str(input("AWS Region Name: "))
regionendpoint = str(input("AWS Region Endpoint: "))
region = RegionInfo(name=regionname, endpoint=regionendpoint)
conn = EC2Connection(aws_access_key_id = aws_access_key, aws_secret_access_key = aws_secret_key, region = region)
print (conn)
volumes = conn.get_all_volumes()
print ("%s" % repr(volumes))
vol_id = str(input("Enter Volume ID to snapshot: "))
keep = int(input("Enter number of snapshots to keep: "))
volume = volumes[0]
description = str(input("Enter volume snapshot description: "))
if volume.create_snapshot(description):
print ('Snapshot created with description: %s' % description)
snapshots = volume.snapshots()
print (snapshots)
def date_compare(snap1, snap2):
if snap1.start_time < snap2.start_time:
return -1
elif snap1.start_time == snap2.start_time:
return 0
return 1
snapshots.sort(key=cmp_to_key(date_compare))
delta = len(snapshots) - keep
for i in range(delta):
print ('Deleting snapshot %s' % snapshots[i].description)
snapshots[i].delete()
我现在要做的不是使用要保留的快照数量,而是要将其更改为指定要保留的快照的日期范围。例如,删除任何早于特定日期和时间的内容。我有点知道从哪里开始,根据上面的脚本,我有按日期排序的快照列表。我想要做的是提示用户指定删除快照的日期和时间,例如 2015-3-4 14:00:00 任何比这更早的东西都将被删除。希望有人可以让我从这里开始
谢谢!!
【问题讨论】:
标签: python datetime amazon-web-services amazon-ec2 snapshot