#!/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- ####################################
# discrition: jenkins git php scrpit
# example: sudo python shop-h5.py
# write by saneri at //
#################################### 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) == :
print "Successful backup to ", tagz
else:
print "Backup FAILED!!" time.sleep()
def rsync_php(): print("开始代码同步中...") remote_ssh = remote_ip + ":" + packge_dir time.sleep() 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) == :
print("同步代码成功...") else:
print("同步代码失败,check...") #os.chown(source_dir,,)
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- ####################################
# discrition: jenkins git php scrpit
# example: sudo python prd-api.py
# write by saneri at //
#################################### 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) == :
print "Successful backup to ", tagz
else:
print "Backup FAILED!!" time.sleep()
def rsync_php(): print("开始代码同步中...") remote_ssh = remote_ip + ":" + packge_dir
#print remote_ssh time.sleep() 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) == :
print("同步代码成功...") else:
print("同步代码失败,check...") mattr="chown -R phpci:phpci %s" %(source_dir)
subprocess.call(mattr,shell=True) if __name__ == "__main__":
backup_php()
rsync_php()

增加备份排除目录功能

pyhont备份php代码脚本的更多相关文章

  1. Centos使用crontab自动定时备份mysql的脚本

    在我们网站上线之后免不了需要备份数据库,为什么要备份呢?我给大家列出了3个理由. 1.防止数据丢失 2.防止数据改错了,可以用来恢复 3.方便给客户数据 以 上几点告诉我们要经常备份,当然我今天给大家 ...

  2. salt上编写了备份日志的脚本

    我在salt上编写了备份日志的脚本,在/opt/CardServer下的主程序目录只保留当天的日志,/opt/log_del目录会保存7天的日志.salt * state.sls script.log ...

  3. SQL备份数据库代码

    SQL备份数据库代码 #region 服务每天备份一次数据库 /// <summary> /// 服务每天备份一次数据库 /// </summary> public void ...

  4. php 备份数据库代码(生成word,excel,json,xml,sql)

    单表备份代码: 复制代码代码如下: <?php class Db { var $conn; function Db($host="localhost",$user=" ...

  5. 实现对MySQL数据库进行分库/分表备份(shell脚本)

    工作中,往往数据库备份是件非常重要的事情,毕竟数据就是金钱,就是生命!废话不多,下面介绍一下:如何实现对MySQL数据库进行分库备份(shell脚本) Mysq数据库dump备份/还原语法: mysq ...

  6. 数据库备份及SQL脚本导入

    数据库备份及SQL脚本导入 数据导出 su - oracle exp 数据库用户名/数据库密码@ORCL file=20190905.dmp full=y SQL脚本导入 首先导入前查看Oracle用 ...

  7. [SQLServer]NetCore中将SQLServer数据库备份为Sql脚本

    NetCore中将SQLServer数据库备份为Sql脚本 描述: 最近写项目收到了一个需求, 就是将SQL Server数据库备份为Sql脚本, 如果是My Sql之类的还好说, 但是在网上搜了一大 ...

  8. Linux oracle数据库自动备份自动压缩脚本代码

    Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...

  9. Linux自动备份MySQL数据库脚本代码

    下面这段Linux的Shell脚本用于每日自动备份MySQL数据库,可通过Linux的crontab每天定时执行 在脚本中可设置需要备份的数据库表清单,并且会将备份文件通过gzip压缩.需要注意的是, ...

随机推荐

  1. Linux环境系搭建Git服务器过程全纪录

    Last :: from 139.199.180.186 [root@VM_219_131_centos ~]# yum install curl-devel expat-devel gettext- ...

  2. VMware的快照和克隆总结

    原文:https://www.cnblogs.com/zxz1987/p/6480833.html 多重快照功能简介:  快照的含义:对某一个特定文件系统在某一个特定时间内的一个具有只读属性的镜像.当 ...

  3. idea 实现热部署

    Idea实现热部署需要以下几步 1.open module setting,设置path,使output path指向存放实时编译class的文件夹 2.设置Artifacts,选择exploded选 ...

  4. ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN

    问题描述:FatalListenerStartupException: Authentication failure 问题原因:连接RabbitMQ服务器异常,要么是用户名和密码错误,要么是使用的用户 ...

  5. Excel数据批量导入到SqlServer的方法

    1,以Excel为数据源建立连接导入. 关键点在于Excel的数据要有表头,表头要和数据库表的列名一样.连接字符串中HDR=YES不能省略,也就是第一行是表头的意思.IMEX=1;是把数据都当作字符串 ...

  6. [leetcode]Minimum Depth of Binary Tree--二叉树层序遍历的应用

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  7. genymotion 前端调试

    1. genymotion的安装见这里 注: a. 要先装VirtualBox, 再装genymotion b. VirtualBox不能太高,我装的是VirtualBox-4.1.40-101594 ...

  8. Web前端开发测试题阅读笔记

    引自: http://www.w3cplus.com/css/front-end-web-development-quiz.html Q7:下面代码弹出值是什么? x = 1; function ba ...

  9. c:url标签

    <c:url>标签 <c:url>标签的主要作用是按照特定的重写规则重新构造URL,它的基本语法为: <c:url   value="原始URL"   ...

  10. 你应该知道的CSS2.0中最常用的18条技巧

    一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.  具体内容请浏览:CSS常用缩写语法 二.明确定义单位,除非值为0. 忘记定义尺寸的单位是CSS新手普遍的错误.在HTML中 ...