3131
3232class S3BackupTool :
3333
34- def __init__ (self , config_file = "config.json" , log_file = "s3backup.log" ):
34+ def __init__ (self , config_file = "config.json" ):
3535 logger .info ('Initialising...' )
3636
3737 try :
@@ -52,17 +52,42 @@ def run_plans(self):
5252 logger .info ('Executing plan %d of %d' , counter , len (self .PLANS ))
5353
5454 try :
55- plan .run ()
56- self .__send_status_email (plan , True )
55+ updated , output_file = plan .run ()
56+ self .__send_success_email (plan , updated , output_file )
5757 except Exception , e :
5858 logger .error ('Failed to run plan: %s' , e )
59- self .__send_status_email (plan , False , e )
59+ self .__send_failure_email (plan , e )
6060
6161 counter += 1
6262
6363 logger .info ('Finished running backup plans' )
6464
65- def __send_status_email (self , plan , success , exception = None ):
65+ def __send_success_email (self , plan , updated , output_file ):
66+ subject = '[S3-Backup] [SUCCESS] - Plan: %s' % plan .name
67+
68+ body = 'The backup plan, %s, run at %s was SUCCESSFUL\n \n ' % (
69+ plan .name ,
70+ strftime ("%a, %d %b %Y %H:%M:%S +0000" , gmtime ()))
71+
72+ if updated :
73+ body += 'The backup set had changed, so a new backup was uploaded: %s' % output_file
74+ else :
75+ body += 'The backup set had not changed. No new backup uploaded'
76+
77+ self .__send_status_email (subject , body )
78+
79+ def __send_failure_email (self , plan , exception ):
80+ subject = '[S3-Backup] [FAILURE] - Plan: %s' % plan .name
81+
82+ body = 'The backup plan, %s, run at %s was a FAILURE\n \n ' % (
83+ plan .name ,
84+ strftime ("%a, %d %b %Y %H:%M:%S +0000" , gmtime ()))
85+
86+ body += '\n \n Detailed failure information:\n \n %s' % exception
87+
88+ self .__send_status_email (subject , body )
89+
90+ def __send_status_email (self , subject , body ):
6691 if self .CONFIGURATION ['EMAIL_FROM' ] is None or self .CONFIGURATION ['EMAIL_TO' ] is None :
6792 logger .debug ('Email not provided, so status update not sent' )
6893 return
@@ -72,25 +97,13 @@ def __send_status_email(self, plan, success, exception=None):
7297 aws_access_key_id = self .CONFIGURATION ['AWS_KEY' ],
7398 aws_secret_access_key = self .CONFIGURATION ['AWS_SECRET' ])
7499
75- result = 'SUCCESS'
76- if not success :
77- result = 'FAILURE'
78-
79- body = 'The backup plan, %s, run at %s was %s' % (
80- plan .name ,
81- strftime ("%a, %d %b %Y %H:%M:%S +0000" , gmtime ()),
82- result )
83-
84- if exception is not None :
85- body += '\n \n Detailed failure information:\n \n %s' % exception
86-
87100 try :
88101 conn .send_email (
89102 self .CONFIGURATION ['EMAIL_FROM' ],
90- '[S3-Backup] [%s] - Plan: %s' % ( result , plan . name ) ,
103+ subject ,
91104 body ,
92105 [self .CONFIGURATION ['EMAIL_TO' ]])
93106 except Exception , e :
94- logger .error ('Failed to send email to {0:s} regarding plan: {1:s}' .format (self .CONFIGURATION ['EMAIL_TO' ],
95- plan . name ),
107+ logger .error ('Failed to send email to {0:s} with subject {1:s}' .format (self .CONFIGURATION ['EMAIL_TO' ],
108+ subject ),
96109 e )
0 commit comments