-
Notifications
You must be signed in to change notification settings - Fork 47
/
create_snapshots.py
93 lines (82 loc) · 3.35 KB
/
create_snapshots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import boto3
import collections
import datetime
#Please mention your region name
#below line code is call cross region
ec = boto3.client('ec2', region_name='ap-southeast-1')
#begins lambda function
def lambda_handler(event, context):
# mention your tag values below example "Backup-snap"
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['Backup', 'Yes']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Number of the Instances : %d" % len(instances)
to_tag = collections.defaultdict(list)
for instance in instances:
try:
retention_days = [
int(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Retention'][0]
except IndexError:
# Please give your retention period day
retention_days = 7
for dev in instance['BlockDeviceMappings']:
if dev.get('Ebs', None) is None:
continue
vol_id = dev['Ebs']['VolumeId']
for name in instance['Tags']:
# To store the instance tag value
Instancename= name['Value']
# To store the instance key value
key= name['Key']
# Below the code is create Snapshot name as instance Name
if key == 'Name' :
ins_name = Instancename
print "Found EBS volume %s on instance %s" % (
vol_id, instance['InstanceId'])
#To get all the instance tags deatils
for name in instance['Tags']:
# To store the instance tag value
Instancename= name['Value']
# To store the instance key value
key= name['Key']
# Below the code is create Snapshot name as instance Name
if key == 'Name' :
snap = ec.create_snapshot(
VolumeId=vol_id,
Description=Instancename,
)
print "snap %s" %snap
to_tag[retention_days].append(snap['SnapshotId'])
print "Retaining snapshot %s of volume %s from instance %s for %d days" % (
snap['SnapshotId'],
vol_id,
instance['InstanceId'],
retention_days,
)
for retention_days in to_tag.keys():
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
snap = snap['Description'] + str('_')
# Here to get current date
snapshot = snap + str(datetime.date.today())
# to mention the current date formet
delete_fmt = delete_date.strftime('%Y-%m-%d')
print "Will delete %d snapshots on %s" % (len(to_tag[retention_days]), delete_fmt)
# below code is create the name and current date as instance name
ec.create_tags(
Resources=to_tag[retention_days],
Tags=[
{'Key': 'DeleteOn', 'Value': delete_fmt},
{'Key': 'Name', 'Value': snapshot },
]
)
to_tag.clear()