Backup and File Mailing


/ Published in: Python
Save to your folder(s)

This source code uses the xml file posted here(http://snipplr.com/view/4138/backup-script2--confg-file/) to know which file should be sent and mailing configuration.


Copy this code and paste it in your HTML
  1. # BACKUP.PY
  2. # Examples:
  3. #
  4. # python backup.py
  5. #
  6. # Help:
  7. # python backup.py --help
  8. #
  9. # Custom config file:
  10. # python backup.py -c custom.xml
  11. # python backup.py --configFile=custom.xml
  12. #
  13. # Author: mjsmagalhaes
  14. # E-mail: mjsmagalhaes (\at) gmail (\dot) com
  15.  
  16. import getopt
  17. import os
  18. import sys
  19. import smtplib
  20. import tarfile
  21. import time
  22. import zipfile
  23.  
  24. import xml.dom.minidom as xml
  25.  
  26. from email import MIMEBase
  27. from email import MIMEMultipart
  28. from email import Encoders
  29.  
  30. #####################################
  31. def usage():
  32. print 'BACKUP.PY Automatic backup script'
  33. print ' Options:'
  34. print ''
  35. print ' -c, --config-file : Load a xml file with backup settings'
  36. print ' default : config.xml'
  37. print ''
  38. print ' -C, --compression : bzip2 or zip(future)'
  39. print ' default : gzip'
  40. print ''
  41. print ' -h, --help : Display program usage'
  42. print ''
  43. print ' ______________________________________________'
  44. print ' Settings Files:'
  45. print ''
  46. print ' -- BACKUP Nodes --'
  47. print ' Multiplicity: 0 or more'
  48. print ' Attributes:'
  49. print ' pathToCompress - File or directory to include in compressed file'
  50. print ' file_prefix - Compressed filename prefix'
  51. print ' file_suffix - Compressed filename suffix. Google dont like .tar.gz'
  52. print ' send - Indicates if the compressed file should be attached or not'
  53. print ''
  54. print ' Compressed file names are file_prefix + timestamp + file_suffix'
  55. print ''
  56. print ' -- MAIL Nodes --'
  57. print ' Multiplicity: 0 or more'
  58. print ' Attributes:'
  59. print ' from - email sender'
  60. print ' to - email receiver'
  61. print ' subject_prefix - messages\'subject are subject_prefix + timestamp'
  62. print ''
  63. print ' Child Nodes:'
  64. print ' -- FILE Nodes --'
  65. print ' Multiplicity: 0 or more'
  66. print ' Attributes:'
  67. print ' path - Points to a path to be attached in the message'
  68. print ''
  69. print ''
  70. print " Author: mjsmagalhaes
  71. print " E-mail: mjsmagalhaes (\\at) lps (\\dot) ufrj (\\dot) br"
  72.  
  73. #####################################
  74.  
  75. def calcDirSize(arg, dir, files):
  76. for file in files:
  77. stats = os.stat(os.path.join(dir, file))
  78. size = stats[6]
  79. arg.append(size)
  80.  
  81. def getDirSize(dir):
  82. sizes = []
  83. os.path.walk(dir, calcDirSize, sizes)
  84. total = 0
  85. for size in sizes:
  86. total = total + size
  87. return total
  88.  
  89. def forHumans(total):
  90. if total > 1073741824:
  91. return (round(total/1073741824.0, 2), 'GB')
  92. if total > 1048576:
  93. return (round(total/1048576.0, 2), 'MB')
  94. if total > 1024:
  95. return (round(total/1024.0, 2), 'KB')
  96. return (total, 'bytes')
  97.  
  98. def compress(pathToCompress, fileName, compressionMethod):
  99.  
  100. if compressionMethod in ('bz2','gz'):
  101. tar = tarfile.open(fileName, 'w:'+compressionMethod)
  102.  
  103. elif compressionMethod == 'zip':
  104. print 'Not supported yet'
  105. print 'Using gzip'
  106.  
  107. compressionMethod = 'gz'
  108. #tar = zipfile.ZipFile(fileName,'w')
  109. tar = tarfile.open(fileName, 'w:'+compressionMethod)
  110. else:
  111. compressionMethod = 'gz'
  112. tar = tarfile.open(fileName, 'w:'+compressionMethod)
  113.  
  114.  
  115. print 'Compressing ...'
  116. print pathToCompress
  117. print 'in ' + fileName + ' with ' + compressionMethod
  118.  
  119. #print 'Compression Method - ' + compressionMethod
  120.  
  121. r_rawSize = 0;
  122.  
  123. for path in pathToCompress:
  124.  
  125. if compressionMethod == 'bz2':
  126. tar.add(path)
  127.  
  128. elif compressionMethod == 'zip':
  129. tar.write(path)
  130.  
  131. else:
  132. tar.add(path)
  133. #end - else
  134.  
  135. if os.path.isdir(path):
  136. r_rawSize += getDirSize(path)
  137. else:
  138. r_rawSize += os.path.getsize(path);
  139.  
  140. # end - for
  141.  
  142. tar.close();
  143.  
  144. print r_rawSize
  145.  
  146. r_tarSize = os.stat(fileName)[6]
  147.  
  148. rawSize = forHumans(r_rawSize);
  149. tarSize = forHumans(r_tarSize);
  150.  
  151. #print 'Compressed: ' + pathToCompress + ' in ' + fileName
  152. print ' Compressed size: ' + str(tarSize[0]) + tarSize[1]
  153. print ' Original Size: ' + str(rawSize[0]) + rawSize[1]
  154. print ' Compression Rate: ' + str(100 - 100*r_tarSize/r_rawSize) + '%'
  155.  
  156. def mail(email, att):
  157. msg = MIMEMultipart.MIMEMultipart();
  158.  
  159. msg['subject']=email['subject']
  160. msg['from']=email['from']
  161. msg['to']=email['to']
  162.  
  163. for path in att:
  164. print 'Attaching ' + path
  165. fp = open(path, 'rb')
  166. att_file = MIMEBase.MIMEBase('application','octet-stream')
  167. att_file.set_payload(fp.read())
  168. fp.close()
  169. Encoders.encode_base64(att_file)
  170. att_file.add_header('Content-Disposition', 'attachment', filename=path)
  171. msg.attach(att_file)
  172.  
  173. print 'Sending mail - ' + msg['subject']
  174. print 'from: ' + msg['from']
  175. print 'to: ' + msg['to']
  176.  
  177. s = smtplib.SMTP()
  178. s.connect(email['server'], email['port'])
  179.  
  180. s.ehlo()
  181.  
  182. if not(email['tls'] in ('no', 'No', 'NO')):
  183. s.starttls()
  184. s.ehlo()
  185. s.login(email['user'],email['passwd'])
  186.  
  187. s.sendmail(msg['from'],msg['to'],msg.as_string())
  188. s.quit()
  189.  
  190. def main(argv):
  191. #####################################
  192. # Resolve config
  193.  
  194. pathToCompress = []
  195. fileToSend = []
  196.  
  197. #####################################
  198. # Defaults
  199. settingsFile = 'config.xml'
  200. compressionMethod = 'gzip'
  201.  
  202. #####################################
  203. # Parse cmd line options
  204. try:
  205. opts, args = getopt.getopt(argv, "c:hC:", ["config-file=",'help','compression='])
  206. except getopt.GetoptError:
  207. usage()
  208. sys.exit(2)
  209.  
  210. for opt, arg in opts:
  211. if opt in ("-h", "--help"):
  212. usage()
  213. sys.exit()
  214. elif opt in ("-c", "--config-file"):
  215. settingsFile = arg
  216. elif opt in ("-C", "--compression"):
  217. compressionMethod = arg
  218.  
  219. #####################################
  220. # Timestamp
  221. t = time.localtime()
  222. str_t = str(t[0]) + '_' + str(t[1]) + '_' + str(t[2])
  223.  
  224. #####################################
  225. # Parse settings
  226. doc = xml.parse(settingsFile)
  227.  
  228. #0 ->
  229. for node in doc.getElementsByTagName('BACKUP'):
  230. # transform unicode to ascii
  231. fileName = str(node.getAttribute('file_prefix') + str_t + node.getAttribute('file_suffix'))
  232.  
  233. #0 ->
  234. for fileNode in node.getElementsByTagName('COMPRESS'):
  235. pathToCompress.append(str(fileNode.getAttribute('path')))
  236.  
  237. #####################################
  238. # Compress
  239.  
  240. compress(pathToCompress, fileName, compressionMethod)
  241.  
  242. if not(node.getAttribute('send') in ('no','No','NO')):
  243. fileToSend.append(fileName);
  244.  
  245. del pathToCompress[:]
  246.  
  247. #####################################
  248. # Mail
  249.  
  250. #0 ou 1
  251. for node in doc.getElementsByTagName('MAIL'):
  252. message = {}
  253. files = []
  254.  
  255. message['from'] = str(node.getAttribute('from'))
  256. message['to'] = str(node.getAttribute('to'))
  257. message['subject'] = str(node.getAttribute('subject_prefix')) + str_t
  258.  
  259. for serverNode in node.getElementsByTagName('SERVER'):
  260. message['server'] = str(serverNode.getAttribute('addr'))
  261. message['port'] = str(serverNode.getAttribute('port'))
  262. message['tls'] = str(serverNode.getAttribute('use_tls'))
  263. message['user'] = str(serverNode.getAttribute('login'))
  264. message['passwd'] = str(serverNode.getAttribute('password'))
  265.  
  266. ## Juntar server + port
  267.  
  268. #0 ->
  269. for fileNode in node.getElementsByTagName('FILE'):
  270. files.append(str(fileNode.getAttribute('path')))
  271.  
  272. mail(message, fileToSend)
  273.  
  274. print 'Done.'
  275.  
  276.  
  277. if __name__ == "__main__":
  278. main(sys.argv[1:])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.