-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbackuplambda.py
executable file
·508 lines (388 loc) · 18.9 KB
/
backuplambda.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from __future__ import print_function
import boto3
from datetime import datetime
import sys, traceback
import logging
import json
import pytz
class BaseBackupManager(object):
def __init__(self, period, tag_name, tag_value, date_suffix, keep_count):
# Message to return result
self.message = ""
self.errmsg = ""
self.period = period
self.tag_name = tag_name
self.tag_value = tag_value
self.date_suffix = date_suffix
self.keep_count = keep_count
def lookup_period_prefix(self):
return self.period
def get_resource_tags(self, resource_id):
pass
def set_resource_tags(self, resource, tags):
pass
def get_backable_resources(self):
pass
def snapshot_resource(self, resource, description, tags):
pass
def list_snapshots_for_resource(self, resource):
pass
def resolve_backupable_id(self, resource):
pass
def resolve_snapshot_name(self, resource):
pass
def resolve_snapshot_time(self, resource):
return resource['StartTime']
def process_backup(self):
# Setup logging
start_message = 'Started taking %(period)s snapshots at %(date)s' % {
'period': self.period,
'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S')
}
self.message = start_message + "\n\n"
print(start_message)
# Counters
total_creates = 0
total_deletes = 0
count_errors = 0
# Number of snapshots to keep
count_success = 0
count_total = 0
backupables = self.get_backable_resources()
for backup_item in backupables:
count_total += 1
backup_id = self.resolve_backupable_id(backup_item)
self.message += 'Processing backup item %(id)s\n' % {
'id': backup_id
}
try:
tags_volume = self.get_resource_tags(backup_id)
description = '%(period)s_snapshot %(item_id)s_%(period)s_%(date_suffix)s by snapshot script at %(date)s' % {
'period': self.period,
'item_id': backup_id,
'date_suffix': self.date_suffix,
'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S')
}
try:
self.snapshot_resource(resource=backup_item, description=description, tags=tags_volume)
self.message += ' New Snapshot created with description: %s and tags: %s\n' % (description, str(tags_volume))
total_creates += 1
except Exception as e:
print ("Unexpected error:", sys.exc_info()[0])
print (e)
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback,
limit=2, file=sys.stdout)
pass
snapshots = self.list_snapshots_for_resource(resource=backup_item)
deletelist = []
# Sort the list based on the dates of the objects
snapshots.sort(self.date_compare)
for snap in snapshots:
sndesc = self.resolve_snapshot_name(snap)
if sndesc.startswith(self.lookup_period_prefix()):
deletelist.append(snap)
else:
print(' Skipping other backup schedule: ' + sndesc)
self.message += "\n Current backups in rotation (keeping {0})\n".format(self.keep_count)
self.message += " ---------------------------\n"
for snap in deletelist:
self.message += " {0} - {1}\n".format(self.resolve_snapshot_name(snap),
self.resolve_snapshot_time(snap))
self.message += " ---------------------------\n"
deletelist.sort(self.date_compare)
delta = len(deletelist) - self.keep_count
for i in range(delta):
self.message += ' Deleting snapshot ' + self.resolve_snapshot_name(deletelist[i]) + '\n'
self.delete_snapshot(resource=backup_item,
snapshot=deletelist[i])
total_deletes += 1
# time.sleep(3)
except Exception as ex:
print("Unexpected error:", sys.exc_info()[0])
print(ex)
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback,
limit=2, file=sys.stdout)
logging.error('Error in processing volume with id: ' + backup_id)
self.errmsg += 'Error in processing volume with id: ' + backup_id
count_errors += 1
else:
count_success += 1
result = '\nFinished making snapshots at %(date)s with %(count_success)s snapshots of %(count_total)s possible.\n\n' % {
'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S'),
'count_success': count_success,
'count_total': count_total
}
self.message += result
self.message += "\nTotal snapshots created: " + str(total_creates)
self.message += "\nTotal snapshots errors: " + str(count_errors)
self.message += "\nTotal snapshots deleted: " + str(total_deletes) + "\n"
return {
"total_resources": count_total,
"total_creates": total_creates,
"total_errors": count_errors,
"total_deletes": total_deletes,
}
def delete_snapshot(self, resource, snapshot):
pass
class EC2BackupManager(BaseBackupManager):
def __init__(self, ec2_region_name, period, tag_name, tag_value, date_suffix, keep_count):
super(EC2BackupManager, self).__init__(period=period,
tag_name=tag_name,
tag_value=tag_value,
date_suffix=date_suffix,
keep_count=keep_count)
# Connect to AWS using the credentials provided above or in Environment vars or using IAM role.
print('Connecting to AWS')
self.conn = boto3.client('ec2', region_name=ec2_region_name)
@staticmethod
def date_compare(snap1, snap2):
if snap1['StartTime'] < snap2['StartTime']:
return -1
elif snap1['StartTime'] == snap2['StartTime']:
return 0
return 1
def lookup_period_prefix(self):
return self.period + "_snapshot"
def get_resource_tags(self, resource_id):
resource_tags = {}
if resource_id:
tags = self.conn.describe_tags(Filters=[{"Name": "resource-id",
"Values": [resource_id]}])
for tag in tags["Tags"]:
# Tags starting with 'aws:' are reserved for internal use
if not tag['Key'].startswith('aws:'):
resource_tags[tag['Key']] = tag['Value']
return resource_tags
def set_resource_tags(self, resource, tags):
resource_id = resource['SnapshotId']
for tag_key, tag_value in tags.iteritems():
print('Tagging %(resource_id)s with [%(tag_key)s: %(tag_value)s]' % {
'resource_id': resource_id,
'tag_key': tag_key,
'tag_value': tag_value
})
self.conn.create_tags(Resources=[resource_id],
Tags=[{"Key": tag_key, "Value": tag_value}])
def get_backable_resources(self):
# Get all the volumes that match the tag criteria
print('Finding volumes that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % {
'tag_name': self.tag_name,
'tag_value': self.tag_value
})
volumes = self.conn.describe_volumes(Filters=[{"Name": 'tag:' + self.tag_name,
"Values": [self.tag_value]}])["Volumes"]
print('Found %(count)s volumes to manage' % { 'count': len(volumes) })
return volumes
def snapshot_resource(self, resource, description, tags):
current_snap = self.conn.create_snapshot(VolumeId=self.resolve_backupable_id(resource),
Description=description)
self.set_resource_tags(current_snap, tags)
def list_snapshots_for_resource(self, resource):
snapshots = self.conn.describe_snapshots(Filters=[
{"Name": "volume-id",
"Values": [self.resolve_backupable_id(resource)]
}])
return snapshots['Snapshots']
def resolve_backupable_id(self, resource):
return resource["VolumeId"]
def resolve_snapshot_name(self, resource):
return resource['Description']
def resolve_snapshot_time(self, resource):
return resource['StartTime']
def delete_snapshot(self, resource, snapshot):
self.conn.delete_snapshot(SnapshotId=snapshot["SnapshotId"])
class RDSBackupManager(BaseBackupManager):
account_number = None
def __init__(self, rds_region_name, period, tag_name, tag_value, date_suffix, keep_count):
super(RDSBackupManager, self).__init__(period=period,
tag_name=tag_name,
tag_value=tag_value,
date_suffix=date_suffix,
keep_count=keep_count)
# Connect to AWS using the credentials provided above or in Environment vars or using IAM role.
print('Connecting to AWS')
self.conn = boto3.client('rds', region_name=rds_region_name)
@staticmethod
def date_compare(snap1, snap2):
utc = pytz.UTC
now = datetime.utcnow().replace(tzinfo=utc)
if snap1.get('SnapshotCreateTime', now) < snap2.get('SnapshotCreateTime', now):
return -1
elif snap1.get('SnapshotCreateTime', now) == snap2.get('SnapshotCreateTime', now):
return 0
return 1
def lookup_period_prefix(self):
return self.period
def get_resource_tags(self, resource_id):
resource_tags = {}
if resource_id:
arn = self.build_arn_for_id(resource_id)
tags = self.conn.list_tags_for_resource(ResourceName=arn)['TagList']
for tag in tags:
# Tags starting with 'aws:' are reserved for internal use
if not tag['Key'].startswith('aws:'):
resource_tags[tag['Key']] = tag['Value']
return resource_tags
def set_resource_tags(self, resource, tags):
resource_id = resource['SnapshotId']
for tag_key, tag_value in tags.iteritems():
print('Tagging %(resource_id)s with [%(tag_key)s: %(tag_value)s]' % {
'resource_id': resource_id,
'tag_key': tag_key,
'tag_value': tag_value
})
self.conn.create_tags(Resources=[resource_id],
Tags=[{"Key": tag_key, "Value": tag_value}])
def get_backable_resources(self):
# Get all the volumes that match the tag criteria
print('Finding databases that match the requested tag ({ "tag:%(tag_name)s": "%(tag_value)s" })' % {
'tag_name': self.tag_name,
'tag_value': self.tag_value
})
all_instances = self.conn.describe_db_instances()['DBInstances']
found = []
for db_instance in all_instances:
if self.db_has_tag(db_instance):
found.append(db_instance)
print('Found %(count)s databases to manage' % { 'count': len(found) })
return found
def snapshot_resource(self, resource, description, tags):
aws_tagset = []
for k in tags:
aws_tagset.append({"Key": k, "Value": tags[k]})
date = datetime.today().strftime('%d-%m-%Y-%H-%M-%S')
snapshot_id = self.period+'-'+self.resolve_backupable_id(resource)+"-"+date+"-"+self.date_suffix
if self.is_cluster(resource):
current_snap = self.conn.create_db_cluster_snapshot(
DBClusterIdentifier=self.resolve_backupable_id(resource),
DBClusterSnapshotIdentifier=snapshot_id,
Tags=aws_tagset)
else:
current_snap = self.conn.create_db_snapshot(
DBInstanceIdentifier=self.resolve_backupable_id(resource),
DBSnapshotIdentifier=snapshot_id,
Tags=aws_tagset)
def is_cluster(self, resource):
return 'DBClusterIdentifier' in resource or 'DBClusterSnapshotIdentifier' in resource
def list_snapshots_for_resource(self, resource):
if self.is_cluster(resource):
snapshots = self.conn.describe_db_cluster_snapshots(DBClusterIdentifier=self.resolve_backupable_id(resource),
SnapshotType='manual')
else:
snapshots = self.conn.describe_db_snapshots(DBInstanceIdentifier=self.resolve_backupable_id(resource),
SnapshotType='manual')
return snapshots['DBSnapshots']
def resolve_backupable_id(self, resource):
if self.is_cluster(resource):
return resource["DBClusterIdentifier"]
return resource["DBInstanceIdentifier"]
def resolve_snapshot_name(self, resource):
if self.is_cluster(resource):
return resource['DBClusterSnapshotIdentifier']
return resource['DBSnapshotIdentifier']
def resolve_snapshot_time(self, resource):
now = datetime.utcnow()
return resource.get('SnapshotCreateTime', now)
def delete_snapshot(self, resource, snapshot):
if self.is_cluster(resource):
self.conn.delete_db_cluster_snapshot(
DBClusterSnapshotIdentifier=snapshot['DBClusterSnapshotIdentifier']
)
else:
self.conn.delete_db_snapshot(
DBSnapshotIdentifier=snapshot["DBSnapshotIdentifier"]
)
def db_has_tag(self, db_instance):
arn = self.build_arn(db_instance)
tags = self.conn.list_tags_for_resource(ResourceName=arn)['TagList']
for tag in tags:
if tag['Key'] == self.tag_name and tag['Value'] == self.tag_value:
return True
return False
def resolve_account_number(self):
if self.account_number is None:
groups = self.conn.describe_db_security_groups()['DBSecurityGroups']
if groups is None or len(groups) == 0:
self.account_number = 0
else:
self.account_number = groups[0]['OwnerId']
return self.account_number
def build_arn(self, instance):
return self.build_arn_for_id(instance['DBInstanceIdentifier'])
def build_arn_for_id(self, instance_id):
# "arn:aws:rds:<region>:<account number>:<resourcetype>:<name>"
region = self.conn.meta.region_name
account_number = self.resolve_account_number()
return "arn:aws:rds:{0}:{1}:db:{2}".format(region, account_number, instance_id)
def lambda_handler(event, context={}):
"""
Example content
{
"period_label": "day",
"period_format": "%a%H",
"ec2_region_name": "ap-southeast-2",
"rds_region_name": "ap-southeast-2",
"tag_name": "MakeSnapshot",
"tag_value": "True",
"arn": "blart",
"keep_count": 12
}
:param event:
:param context:
:return:
"""
print("Received event: " + json.dumps(event, indent=2))
period = event["period_label"]
period_format = event["period_format"]
tag_name = event['tag_name']
tag_value = event['tag_value']
ec2_region_name = event.get('ec2_region_name', None)
rds_region_name = event.get('rds_region_name', None)
sns_arn = event.get('arn')
error_sns_arn = event.get('error_arn')
keep_count = event['keep_count']
date_suffix = datetime.today().strftime(period_format)
result = event
if ec2_region_name:
backup_mgr = EC2BackupManager(ec2_region_name=ec2_region_name,
period=period,
tag_name=tag_name,
tag_value=tag_value,
date_suffix=date_suffix,
keep_count=keep_count)
metrics = backup_mgr.process_backup()
result["metrics"] = metrics
result["ec2_backup_result"] = backup_mgr.message
print('\n' + backup_mgr.message + '\n')
sns_boto = None
# Connect to SNS
if sns_arn or error_sns_arn:
print('Connecting to SNS')
sns_boto = boto3.client('sns', region_name=ec2_region_name)
if error_sns_arn and backup_mgr.errmsg:
sns_boto.publish(error_sns_arn, 'Error in processing volumes: ' + backup_mgr.errmsg, 'Error with AWS Snapshot')
if sns_arn:
sns_boto.publish(TopicArn=sns_arn, Message=backup_mgr.message, Subject='Finished AWS EC2 snapshotting')
if rds_region_name:
backup_mgr = RDSBackupManager(rds_region_name=rds_region_name,
period=period,
tag_name=tag_name,
tag_value=tag_value,
date_suffix=date_suffix,
keep_count=keep_count)
metrics = backup_mgr.process_backup()
result["metrics"] = metrics
result["rds_backup_result"] = backup_mgr.message
print('\n' + backup_mgr.message + '\n')
sns_boto = None
# Connect to SNS
if sns_arn or error_sns_arn:
print('Connecting to SNS')
sns_boto = boto3.client('sns', region_name=rds_region_name)
if error_sns_arn and backup_mgr.errmsg:
sns_boto.publish(error_sns_arn, 'Error in processing RDS: ' + backup_mgr.errmsg, 'Error with AWS Snapshot')
if sns_arn:
sns_boto.publish(TopicArn=sns_arn, Message=backup_mgr.message, Subject='Finished AWS RDS snapshotting')
return json.dumps(result, indent=2)