Attachment 'pyrsync.py'

Download

   1 #!/usr/bin/env python
   2 
   3 import os, syslog, rfc822, smtplib
   4 from datetime import datetime
   5 from StringIO import StringIO
   6 from subprocess import Popen, PIPE, CalledProcessError
   7 
   8 
   9 class RsyncDirectoryTree(object):
  10     __DEFAULT_ARGS = "-a --delete"
  11     __RSYNC_PATH = "/usr/bin"
  12 
  13     def __init__(self, rsyncPath =__RSYNC_PATH, mailHost=None, fromAddr=None,
  14                  mailto=[], subject=""):
  15         """
  16         RsyncDirectoryTree constructor.
  17 
  18         @keyword rsyncPath: The path to rsync (default = /usr/bin).
  19         @keyword mailHost: The host name of the mailserver to use.
  20         @keyword fromAddr: 
  21         @keyword mailto: Email addresses to send results to.
  22         """
  23         self.__rsyncPath = rsyncPath
  24         self.__mailHost = mailHost
  25         self.__fromAddr = fromAddr
  26         self.__mailto = mailto
  27         self.__subject = subject
  28         self.__buff = StringIO()
  29 
  30     def rsync(self, src, dst, args=__DEFAULT_ARGS, copyDir=False):
  31         """
  32         Wrapper for the rsync command. Synchronize the source directory to the
  33         destination directory.
  34 
  35         @param src: The source directory.
  36         @param dst: The destination directory.
  37         @keyword args: The rsync command line arguments.
  38         @keyword copyDir: C{True} copies the last directory in the C{src} path
  39                           and C{False} copies only the files in the last
  40                           directory.
  41         """
  42         src = src.strip()
  43 
  44         if copyDir:
  45             src = src.rstrip(os.sep)
  46         else:
  47             src = src = src[-1] != os.sep and src + os.sep or src
  48 
  49         cmd = "%s/rsync %s %s %s" % (self.__rsyncPath, args, src, dst)
  50 
  51         try:
  52             process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE,
  53                             env={"PATH": "%s" % self.__RSYNC_PATH})
  54             stdoutdata, stderrdata = process.communicate()
  55         except (OSError, ValueError, CalledProcessError), e:
  56             msg = "Could not synchronize directory tree %s to %s."
  57             syslog.syslog(msg % (src, dst))
  58 
  59         if stderrdata:
  60             msg = "RsyncDirectoryTree: %s" % stderrdata
  61             syslog.syslog(msg)
  62             self.__buff.write(msg)
  63             self.sendmail()
  64 
  65         self.__buff.write('\n\nSource: %s\nDestination: %s\n\n' % (src, dst))
  66         self.__buff.write(stdoutdata)
  67 
  68     def sendmail(self):
  69         mailto = ', '.join(self.__mailto)
  70         now = datetime.now()
  71         subject = "%s %s" % (self.__subject, now)
  72         header = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
  73                  (self.__fromAddr, mailto, subject)
  74         self.__buff.flush()
  75         message = "Ran on: %s\n" % now + self.__buff.getvalue()
  76         self.__buff.close()
  77 
  78         try:
  79             mta = smtplib.SMTP(self.__mailHost)
  80             ##mta.set_debuglevel(1)
  81             sm_err = mta.sendmail(self.__fromAddr, mailto, header + message)
  82             mta.quit()
  83         except smtplib.SMTPException, e:
  84             msg = "Could not send email from %s to %s"
  85             syslog.syslog(msg % (self.__fromAddr, mailto))
  86 
  87 
  88 if __name__ == "__main__":
  89     import os, sys
  90 
  91     mailHost = 'smtp.knology.net'
  92     fromAddr = 'cnobile@foundation.TetraSys.org'
  93     mailto = ('carl.nobile@gmail.com',)
  94     subject = "Message from pyrsync on:"
  95     rdt = RsyncDirectoryTree(mailHost=mailHost, fromAddr=fromAddr,
  96                              mailto=mailto, subject=subject)
  97     rsyncHost = 'tetrasys-design.net'
  98     logFile = "/var/log/pyrsync/pyrsync.log"
  99     path, filename = os.path.split(logFile)
 100     if not os.path.isdir(path): os.mkdir(path)
 101     rsyncArgs = "--archive --progress --human-readable --verbose --stats " + \
 102                 "--delete --force --log-file=%s -e 'ssh -l cnobile'" % logFile
 103     src = "tetrasys-design.net:'%s'"
 104     dst = "/dat0/rsync/tetrasys-design.net"
 105     if not os.path.isdir(dst): os.mkdir(dst)
 106 
 107     # /var/www
 108     rdt.rsync(src % '/var/www', "'%s'" % dst, args=rsyncArgs, copyDir=True)
 109 
 110     # /etc/apache2
 111     rdt.rsync(src % '/etc/apache2', "'%s'" % dst, args=rsyncArgs, copyDir=True)
 112 
 113     # /usr/local/share/xymon
 114     rdt.rsync(src % '/usr/local/share/xymon', "'%s'" % dst, args=rsyncArgs,
 115               copyDir=True)
 116 
 117     # /opt/moin
 118     rdt.rsync(src % '/opt/moin', "'%s'" % dst, args=rsyncArgs, copyDir=True)
 119 
 120     rdt.sendmail()
 121     sys.exit(0)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.