#!/usr/bin/env python # encoding: utf-8 import time import os import sys import logging #create logger logger = logging.getLogger("phpfile") logger.setLevel(logging.DEBUG) #create console handler and set level to error ch = logging.StreamHandler() ch.setLevel(logging.ERROR) #create file handler and set level to debug fh = logging.FileHandler("phpfile.log") fh.setLevel(logging.DEBUG) #create formatter formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #add formatter to ch and fh ch.setFormatter(formatter) fh.setFormatter(formatter) #add ch and fh to logger logger.addHandler(ch) logger.addHandler(fh) def backup(): source = [‘/apps/emc_nas/cms/activity‘,‘/apps/emc_nas/cms/phpcms‘] #source = [‘/apps/emc_nas/cms/activity‘] target_dir = ‘/apps/emc_nas/bak/cms_backup/‘ today = target_dir + time.strftime(‘%Y%m%d‘) now = "phpcms_" + time.strftime(‘%Y%m%d‘) if not os.path.exists(today): os.mkdir(today) print ‘Successfully created directory‘, today logger.info(today) target = today + os.sep + now + ‘.tar.gz‘ logger.debug(target) tar_backup = "tar -zcvf %s %s" % (target,‘ ‘.join(source)) logger.info(tar_backup) if os.system(tar_backup) == 0: print "Successful backup to ", target else: print "Backup FAILED!!" logger.debug(tar_backup) if __name__ == "__main__": backup()
时间: 2024-10-07 18:10:37