1. 程式人生 > >pyhont備份php代碼腳本

pyhont備份php代碼腳本

targe enc failed 備份 php logging for sep today

#!/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()

  

pyhont備份php代碼腳本