删除较旧的孤立快照-脚本

时间:2019-05-30 15:50:55

标签: python amazon-web-services boto3 snapshot

我有以下脚本来删除12个月大的快照。但这会删除所有12个月以上的快照。我必须确定12个月的孤立快照,并仅删除孤立快照而不删除AMI快照。

此脚本会删除所有12个月前的快照。

  1. 我想识别和删除12个月大的孤立快照,但是此脚本删除了所有12个月以上的快照。

import boto 
import datetime
import dateutil
from dateutil import parser
from boto import ec2
 
connection=ec2.connect_to_region("REGION-NAME")
 
ebsAllSnapshots=connection.get_all_snapshots(owner='16-DIGIT-AWS-ACCOUNT-NUMBER')
 
#Get the 30 days old date
timeLimit=datetime.datetime.now() - datetime.timedelta(days=365)  
 
for snapshot in ebsAllSnapshots:
     
    if parser.parse(snapshot.start_time).date() <= timeLimit.date():
        print " Deleting Snapshot %s  %s "  %(snapshot.id,snapshot.tags)
        connection.delete_snapshot(snapshot.id) 
    else:
        # this section will have all snapshots which is created before 30 days
        print "Only Deleting Snapshots which is 365 days old"

0 个答案:

没有答案