Source

Simple_BackUP / backup.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##########################################################################
#BACKUP FILES company "AlieParusa"                                       #
##########################################################################
import xml.parsers.expat
import logging
import tarfile
import os
import modules
from datetime import datetime

#List of path backup from XML file with name is "path.xml"
def Path():
   paths={}
   paths_list = []
   def XMLlist(name, attrs):
       paths.update(attrs)
   filepaths=open("/usr/NAS/scripts/python/path.xml", "r")
   parse=xml.parsers.expat.ParserCreate()
   parse.StartElementHandler = XMLlist
   parse.ParseFile(filepaths)
   for i in paths.keys():
       paths_list.append(paths[i])	
   return paths
#Create tar archives
def Archive(paths):
    archivepach="/usr/NAS/backups/"
    date=datetime.now().strftime('%Y'"."'%m'"."'%d')
    logging.basicConfig(filename=archivepach+'logs'+'_'+date+'.log',format='%(asctime)s %(message)s',level=logging.DEBUG)
    for i in paths:
        logging.info('Start task: '+str(i))
        tar = tarfile.open(archivepach+str(i)+'_'+date+".tar", "w")
        for root, dir, files in os.walk(str(paths[i])):
            for file in files:
                fullpath = os.path.join(root,file) 
                try:
                      tar.add(fullpath)
                except:
                      logging.warning("Error: "+fullpath)
        logging.info('Finish task: '+str(i))
        tar.close()
#Send email          
        modules.Sendemail(fro_m='service@redsails.ru', to='backup@redsails.ru', body='Job '+str(i)+' is done.' , subject='BackUP')
#Run processing
Archive(Path())