Skip to content

Instantly share code, notes, and snippets.

@tongpu
Created January 28, 2014 23:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tongpu/8678808 to your computer and use it in GitHub Desktop.
Save tongpu/8678808 to your computer and use it in GitHub Desktop.
A simple script to send an email with important information about a dirvish backup run via email. Also sends an extra email on error. Add it to post-server in your dirvish.conf. Based losely on http://wiki.dirvish.org/FuhHoe
#!/usr/bin/env python
from email.mime.text import MIMEText
import os
import re
import smtplib
mailfrom = 'from@example.com'
mailto = 'to@example.com'
mailerr = 'err@example.com'
dv_dir = os.path.dirname(os.getenv('DIRVISH_DEST'))
dv_summary = os.path.join(dv_dir, 'summary')
dv_log = os.path.join(dv_dir, 'log')
dv_rsyncerror = os.path.join(dv_dir, 'rsyncerror')
dv_client = os.getenv('DIRVISH_CLIENT')
dv_src = os.getenv('DIRVISH_SRC')
dv_status = os.getenv('DIRVISH_STATUS')
separator = '#' * 60 + '\n'
email_text = separator
email_text += 'Status: %(status)s\n' % {'status': dv_status}
email_text += separator
email_text += 'Directory: %(dir)s\n' % {'dir': dv_dir}
email_text += 'Summary: %(summary)s\n' % {'summary': dv_summary}
email_text += separator
if os.path.isfile(dv_summary):
with open(dv_summary) as fid_summary:
email_text += fid_summary.read()
email_text += separator
if os.path.isfile(dv_rsyncerror):
with open(dv_rsyncerror) as fid_rsyncerror:
email_text += fid_rsyncerror.read()
email_text += separator
if os.path.isfile(dv_log):
with open(dv_log) as fid_log:
transfered_file_size = re.findall(r'^Total transferred file size: (\d+) bytes$', fid_log.read(), flags=re.MULTILINE)[0]
email_text += 'Total transferred file size: %(ttfs).2f MB\n' % {'ttfs': float(transfered_file_size)/1024**2}
email_text += separator
msg = MIMEText(email_text)
msg['Subject'] = 'Dirvish %(client)s:%(src)s (europa) %(status)s' % {'client': dv_client, 'src': dv_src, 'status': dv_status}
msg['From'] = mailfrom
msg['To'] = mailto
s = smtplib.SMTP('localhost')
if(dv_status != 'success'):
msg['To'] = mailerr
s.sendmail(mailfrom, mailerr, msg.as_string())
s.sendmail(mailfrom, mailto, msg.as_string())
s.quit()
@Huaba
Copy link

Huaba commented Dec 3, 2014

Thx, works with Ubuntu Server 14.04.1 LTS, dirvish and postfix :)

@Huaba
Copy link

Huaba commented Dec 29, 2014

One problem: i've two vaults to safe, the /home/ directory (huabanet-homes) and the /var/data directory (huabanet-data). The notification for huabanet-homes works always, but the notification for huabanet-data works only sometimes...

If an error occurred this is the error message: 
Traceback (most recent call last):
  File "/etc/dirvish/dirvish-mail", line 41, in <module>
    transfered_file_size = re.findall(r'^Total transferred file size: (\d+) bytes$', fid_log.read(), flags=re.MULTILINE)[0]
IndexError: list index out of range
huabanet-data:default post-server failed (1)

Did you know what to do?

Thx for help,
Huaba

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment