自动备份站点

PHP网站需要备份,代码如下

#!/usr/local/anaconda3/bin/python
# coding:utf-8

# ====================================================
# Author: chang - EMail:changbo@hmg100.com
# Last modified: 2018-02-22
# Filename: zbsitebak.py
# Description: version update ,base paramiko, sys, time
# blog:http://www.cnblogs.com/changbo
# ====================================================


import os
import subprocess
import time
import logging
import shutil

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='zbsitebak.log',
                    filemode='a')

nowDay = time.strftime("%Y%m%d", time.localtime())
path = os.getcwd()
blackDir = ['images', 'bdimages']
# count = 0
siteBakDir = '/home/sitebakdir/'
sites = '/home/wwwroot/default/xxx/'
siteDirs = (((subprocess.Popen("/usr/bin/ls " + sites, shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()).strip()

if not os.path.isdir(siteBakDir):
    os.mkdir(siteBakDir)

for siteDir in siteDirs.split('\n'):
    if os.path.isdir(sites + siteDir) and siteDir != 'default':
        sourceDir = sites + siteDir + '/public_html/'
        destDir = siteBakDir + siteDir + '/public_html/'
        os.system('/usr/bin/mkdir -p ' + destDir)
        files = (((subprocess.Popen("/usr/bin/ls " + sourceDir, shell=True,
                                    stdout=subprocess.PIPE)).stdout.read()).decode()).strip()
        for copyfile in files.split('\n'):
            if copyfile not in blackDir:
                os.system('/usr/bin/cp -rf ' + sourceDir + copyfile + ' ' + destDir)
        os.system('cd /home/sitebakdir && tar czf /home/allbak/sitebak/' + siteDir + nowDay + '.tar.gz ' + siteDir)
    logging.debug('copy %s files to bak dir...' % siteDir)

os.system('/usr/bin/cp -rf ' + sites + 'default ' + siteBakDir)
logging.debug('copy default to bak dir...')
time.sleep(5)
logging.debug("begin to tar site bak dir")
os.system('cd /home/sitebakdir && tar czf /home/allbak/sitebak/default' + nowDay + '.tar.gz default')
logging.debug("bak success!")
time.sleep(5)
shutil.rmtree(siteBakDir)
# for root, dirs, files in os.walk(path):
#     for each in files:
#         count += 1
#     if len(dirs) > 0:
#         print(dirs)
# print(path, count, dirs)

 

END!

 

posted @ 2018-02-23 18:25  知_行  阅读(251)  评论(0编辑  收藏  举报