pyhont备份php代码脚本

! usr bin env python encoding: utf-8import time import os import sys import l
#!/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()

 Jenkins发布PHP代码脚本:

#!/usr/bin/env python
#coding:utf-8

####################################
# discrition: jenkins git php scrpit
# example:  sudo python php-shop.py
# write by saneri at 2017/08/14
####################################

import os
import time
import subprocess

packge_dir = "/root/.jenkins/workspace/shop-H5/"
remote_ip = "59.110.164.99"
remote_port = "'-e ssh -p 6168'"
backup_dir = '/data/backup/' + time.strftime('%Y%m%d')
source_dir = "shop-H5"

def backup_php():

    tgz = backup_dir + os.sep + "shop-H5_" + time.strftime('%Y%m%d%H') + '.tgz'
    backup_tgz = "tar -zcvf %s %s" % (tgz,source_dir)

    #print(time.strftime('%Y-%m-%d-%H:%M'))
    #print(backup_dir)
    if not os.path.exists(backup_dir):
        os.mkdir(backup_dir)
        print("Sucessfully created directory",backup_dir)

    print("开始备份代码中...")
    os.chdir('/opt')
    if subprocess.call(backup_tgz,shell=True) == 0:
        print("Successful backup to ", tgz)
    else:
        print("Backup FAILED!!")

    time.sleep(3)

def rsync_php():

    print("开始代码同步中...")
    remote_ssh = remote_ip + ":" + packge_dir
    rsyn = "/usr/bin/sudo rsync -avH --progress %s %s %s" %(remote_port,remote_ssh,source_dir)

    if subprocess.call(rsyn,shell=True) == 0:
        print("同步代码成功...")
    else:
        print("同步代码失败,check...")
    os.chown(source_dir,504,504)

if __name__ == "__main__":
    backup_php()
    rsync_php()
#!/usr/bin/env python
#coding:utf-8

####################################
# discrition: jenkins git php scrpit
# example:  sudo python shop-h5.py
# write by saneri at 2017/08/14
####################################

import os
import time
import subprocess

packge_dir = "/root/.jenkins/workspace/prd-php-p/"
remote_ip = "59.110.164.99"
remote_port = "'-e ssh -p 6168'"
backup_dir= '/data/backup/' + time.strftime('%Y%m%d')
source_dir="shop-H5"
source_URL="/data/web/xd"

def backup_php():
  tagz = backup_dir + os.sep + source_dir + "_" + time.strftime('%Y%m%d%H') + '.tgz'
  print "tagz=",tagz
  backup_tgz = "tar -zcvf %s %s" %(tagz,source_dir)

  if not os.path.exists(backup_dir):
    os.mkdir(backup_dir)
    print("Sucessfully created directory",backup_dir)

  print("开始备份代码中...")
  os.chdir(source_URL)
  #os.system(backup_tgz)
  #subprocess.call(backup_tgz,shell=True)

  if subprocess.call(backup_tgz,shell=True) == 0:
    print  "Successful backup to ", tagz
  else:
    print "Backup FAILED!!"

  time.sleep(3)
def rsync_php():

  print("开始代码同步中...")

  remote_ssh = remote_ip + ":" + packge_dir

  time.sleep(3)

  rsyn="/usr/bin/sudo rsync -avH --progress --exclude=.git %s %s %s" %(remote_port,remote_ssh,source_dir)
  #print rsyn

  if subprocess.call(rsyn,shell=True) == 0:
    print("同步代码成功...")

  else:
    print("同步代码失败,check...")

  #os.chown(source_dir,504,504)
  mattr="chown -R phpci:phpci %s" %(source_dir)
  subprocess.call(mattr,shell=True)

if __name__ == "__main__":
  backup_php()
  rsync_php()
生产代码发布脚本
#!/usr/bin/env python
#coding:utf-8

####################################
# discrition: jenkins git php scrpit
# example:  sudo python prd-api.py
# write by saneri at 2017/08/14
####################################

import os
import time
import subprocess

packge_dir = "/root/.jenkins/workspace/prd-php-api/"
remote_ip = "59.110.164.99"
remote_port = "'-e ssh -p 6168'"
backup_dir= '/data/backup/' + time.strftime('%Y%m%d')
source_dir="shop-api"
source_URL="/data/www"
excl="--exclude=shop-api/appApi/frame/log"

def backup_php():
  tagz = backup_dir + os.sep + source_dir + "_" + time.strftime('%Y%m%d%H') + '.tgz'
  print "tagz=",tagz
  backup_tgz = "tar -zcvf %s %s %s" %(tagz,excl,source_dir)
  if not os.path.exists(backup_dir):
    os.mkdir(backup_dir)
    print("Sucessfully created directory",backup_dir)

  print("开始备份代码中...")
  os.chdir(source_URL)
  if subprocess.call(backup_tgz,shell=True) == 0:
    print  "Successful backup to ", tagz
  else:
    print "Backup FAILED!!"

  time.sleep(3)
def rsync_php():

  print("开始代码同步中...")

  remote_ssh = remote_ip + ":" + packge_dir
  #print remote_ssh

  time.sleep(3)

  rsyn="/usr/bin/sudo rsync -avH --progress --exclude=.git %s %s %s" %(remote_port,remote_ssh,source_dir)
  #print rsyn

  if subprocess.call(rsyn,shell=True) == 0:
    print("同步代码成功...")

  else:
    print("同步代码失败,check...")

  mattr="chown -R phpci:phpci %s" %(source_dir)
  subprocess.call(mattr,shell=True)

if __name__ == "__main__":
  backup_php()
  rsync_php()
增加备份排除目录功能