2
2
3
3
import boto3
4
4
from datetime import datetime
5
- import sys
5
+ import sys , traceback
6
6
import logging
7
7
import json
8
8
import pytz
@@ -43,6 +43,9 @@ def list_snapshots_for_resource(self, resource):
43
43
def resolve_backupable_id (self , resource ):
44
44
pass
45
45
46
+ def resolve_snapshot_name (self , resource ):
47
+ pass
48
+
46
49
def resolve_snapshot_time (self , resource ):
47
50
return resource ['StartTime' ]
48
51
@@ -86,9 +89,12 @@ def process_backup(self):
86
89
self .snapshot_resource (resource = backup_item , description = description , tags = tags_volume )
87
90
self .message += ' New Snapshot created with description: %s and tags: %s\n ' % (description , str (tags_volume ))
88
91
total_creates += 1
89
- except Exception , e :
92
+ except Exception as e :
90
93
print ("Unexpected error:" , sys .exc_info ()[0 ])
91
94
print (e )
95
+ exc_type , exc_value , exc_traceback = sys .exc_info ()
96
+ traceback .print_exception (exc_type , exc_value , exc_traceback ,
97
+ limit = 2 , file = sys .stdout )
92
98
pass
93
99
94
100
snapshots = self .list_snapshots_for_resource (resource = backup_item )
@@ -104,11 +110,11 @@ def process_backup(self):
104
110
else :
105
111
print (' Skipping other backup schedule: ' + sndesc )
106
112
107
- self .message += "\n Current backups in rotation (keeping {})\n " .format (self .keep_count )
113
+ self .message += "\n Current backups in rotation (keeping {0 })\n " .format (self .keep_count )
108
114
self .message += " ---------------------------\n "
109
115
110
116
for snap in deletelist :
111
- self .message += " {} - {}\n " .format (self .resolve_snapshot_name (snap ),
117
+ self .message += " {0 } - {1 }\n " .format (self .resolve_snapshot_name (snap ),
112
118
self .resolve_snapshot_time (snap ))
113
119
self .message += " ---------------------------\n "
114
120
@@ -123,6 +129,9 @@ def process_backup(self):
123
129
except Exception as ex :
124
130
print ("Unexpected error:" , sys .exc_info ()[0 ])
125
131
print (ex )
132
+ exc_type , exc_value , exc_traceback = sys .exc_info ()
133
+ traceback .print_exception (exc_type , exc_value , exc_traceback ,
134
+ limit = 2 , file = sys .stdout )
126
135
logging .error ('Error in processing volume with id: ' + backup_id )
127
136
self .errmsg += 'Error in processing volume with id: ' + backup_id
128
137
count_errors += 1
@@ -140,6 +149,13 @@ def process_backup(self):
140
149
self .message += "\n Total snapshots errors: " + str (count_errors )
141
150
self .message += "\n Total snapshots deleted: " + str (total_deletes ) + "\n "
142
151
152
+ return {
153
+ "total_resources" : count_total ,
154
+ "total_creates" : total_creates ,
155
+ "total_errors" : count_errors ,
156
+ "total_deletes" : total_deletes ,
157
+ }
158
+
143
159
def delete_snapshot (self , snapshot ):
144
160
pass
145
161
@@ -360,7 +376,7 @@ def build_arn_for_id(self, instance_id):
360
376
region = self .conn .meta .region_name
361
377
account_number = self .resolve_account_number ()
362
378
363
- return "arn:aws:rds:{}:{}:db:{}" .format (region , account_number , instance_id )
379
+ return "arn:aws:rds:{0 }:{1 }:db:{2 }" .format (region , account_number , instance_id )
364
380
365
381
366
382
def lambda_handler (event , context = {}):
@@ -402,13 +418,6 @@ def lambda_handler(event, context={}):
402
418
403
419
date_suffix = datetime .today ().strftime (period_format )
404
420
405
- sns_boto = None
406
-
407
- # Connect to SNS
408
- if sns_arn or error_sns_arn :
409
- print ('Connecting to SNS' )
410
- sns_boto = boto3 .client ('sns' , region_name = ec2_region_name )
411
-
412
421
result = event
413
422
if ec2_region_name :
414
423
backup_mgr = EC2BackupManager (ec2_region_name = ec2_region_name ,
@@ -418,16 +427,24 @@ def lambda_handler(event, context={}):
418
427
date_suffix = date_suffix ,
419
428
keep_count = keep_count )
420
429
421
- backup_mgr .process_backup ()
430
+ metrics = backup_mgr .process_backup ()
422
431
432
+ result ["metrics" ] = metrics
423
433
result ["ec2_backup_result" ] = backup_mgr .message
424
434
print ('\n ' + backup_mgr .message + '\n ' )
425
435
436
+ sns_boto = None
437
+
438
+ # Connect to SNS
439
+ if sns_arn or error_sns_arn :
440
+ print ('Connecting to SNS' )
441
+ sns_boto = boto3 .client ('sns' , region_name = ec2_region_name )
442
+
426
443
if error_sns_arn and backup_mgr .errmsg :
427
444
sns_boto .publish (error_sns_arn , 'Error in processing volumes: ' + backup_mgr .errmsg , 'Error with AWS Snapshot' )
428
445
429
446
if sns_arn :
430
- sns_boto .publish (sns_arn , backup_mgr .message , 'Finished AWS EC2 snapshotting' )
447
+ sns_boto .publish (TopicArn = sns_arn , Message = backup_mgr .message , Subject = 'Finished AWS EC2 snapshotting' )
431
448
432
449
if rds_region_name :
433
450
backup_mgr = RDSBackupManager (rds_region_name = rds_region_name ,
@@ -437,15 +454,23 @@ def lambda_handler(event, context={}):
437
454
date_suffix = date_suffix ,
438
455
keep_count = keep_count )
439
456
440
- backup_mgr .process_backup ()
457
+ metrics = backup_mgr .process_backup ()
441
458
459
+ result ["metrics" ] = metrics
442
460
result ["rds_backup_result" ] = backup_mgr .message
443
461
print ('\n ' + backup_mgr .message + '\n ' )
444
462
463
+ sns_boto = None
464
+
465
+ # Connect to SNS
466
+ if sns_arn or error_sns_arn :
467
+ print ('Connecting to SNS' )
468
+ sns_boto = boto3 .client ('sns' , region_name = rds_region_name )
469
+
445
470
if error_sns_arn and backup_mgr .errmsg :
446
471
sns_boto .publish (error_sns_arn , 'Error in processing RDS: ' + backup_mgr .errmsg , 'Error with AWS Snapshot' )
447
472
448
473
if sns_arn :
449
- sns_boto .publish (sns_arn , backup_mgr .message , 'Finished AWS RDS snapshotting' )
474
+ sns_boto .publish (TopicArn = sns_arn , Message = backup_mgr .message , Subject = 'Finished AWS RDS snapshotting' )
450
475
451
476
return json .dumps (result , indent = 2 )
0 commit comments