svn版本备份
备份机制
1, 每10分钟检查一次svn(时间可以在配置文件中配置)。
2,检查到有更新时备份最新版本,记录更新内容。
3,删除30天之前的备份(天数可以配置)。
配置文件
# vi ../conf/backup_server.conf [common]# How many hours between scanscan_interval = 10log_level = debugbackup_day = 20backup_dir = /opt/backupname = Android:C++:PHP:IPHONEdir_name = /data/soft/rl/mobile_develop/moxian/code/android/MoXian:/data/soft/rl/c++/cframework/src:/data/soft/rl/moxian_system:/data/soft/rl/mobile_develop/moxian/code/iphone/MoXian/MoXian_developer
备份脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-'''2013/04/06 Rongzhong Xu'''import osimport ConfigParserimport timeimport randomimport subprocessimport loggingimport logging.handlersimport redebug = Falsesvn_user = 'test'svn_pwd = '***'svn_dir = "svn://192.168.0.7/rl/"svn_quiet = " --non-interactive --trust-server-cert"svn_auth = "{0} --username {1} --password {2}".format(svn_quiet, svn_user, svn_pwd)# Read configuratons of logger and add loggertry: parser = ConfigParser.ConfigParser() parser.read("../conf/backup_server.conf") log_level = parser.get("common", "log_level") scan_interval = int(parser.get('common', 'scan_interval')) log_num = getattr(logging, log_level.upper(), None) backup_dir = parser.get("common", "backup_dir") back_logger = logging.getLogger('MyLogger') back_logger.setLevel(log_num) fh = logging.handlers.RotatingFileHandler('../logs/backup_server.log', maxBytes=10000000, backupCount=5, ) fh.setLevel(log_num) formatter = logging.Formatter(u'%(asctime)s [%(levelname)s] %(message)s') fh.setFormatter(formatter) back_logger.addHandler(fh) back_logger.info("Application starting! \n")except Exception as e: print "Read log configurations Failed!" print e for item in sys.exc_info(): print item sys.exit(str(e))while True: time.sleep(60 * scan_interval) # Get files from SVN try: svn_cmd_get = 'cd /data/soft && svn co {0} {1}'.format( svn_dir, svn_auth) #back_logger.info({0}! \n".format(svn_cmd_get)) result = subprocess.check_output(svn_cmd_get, shell=True) #back_logger.info("update info: {0} ! \n".format(result)) if debug: print result version = re.search('\s+(\d{3,8})',result).group(1) # back_logger.info("version: {0} \n".format(version)) directory = '{0}/{1}/'.format(backup_dir, version) if os.path.exists(directory): back_logger.info("version {0} has backuped before! \n".format( version)) continue else: back_logger.info("updating:\n{0}! \n".format( result)) except Exception as e: back_logger.critical("Get files from SVN failed! \n") back_logger.critical(str(e), exc_info=True) continue # Read common configurations try: back_logger.info("Beginning backup! \n") scan_interval = int(parser.get('common', 'scan_interval')) backup_day = parser.get("common", "backup_day") names = parser.get("common", "name") dirs = parser.get("common", "dir_name") name_list = names.split(":") dir_list = dirs.split(":") number = len(name_list) if len(name_list) != len(dir_list): back_logger.critical("backup name and dir number not equal! \n") except Exception as e: back_logger.critical("Read common configurations failed! \n") back_logger.critical(str(e), exc_info=True) continue for seq in range(number): try: pack_name = name_list[seq] pack_dir = dir_list[seq] subprocess.check_output('mkdir -p {0}'.format(directory), shell=True) now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) subprocess.call('cd {0}'.format(pack_dir), shell=True) file_name = "{0}/{1}-{2}.tar.gz".format(directory,pack_name, now) dir_name = os.path.dirname(pack_dir.rstrip('/')) base_name = os.path.basename(pack_dir.rstrip('/')) cmd = 'cd {0} && tar czf {1} {2}'.format(dir_name, file_name, base_name) print cmd logging.info(cmd) subprocess.call(cmd, shell=True) except Exception as e: back_logger.critical(str(e), exc_info=True) continue if random.randint(1, 100) % 99 == 0: try: back_logger.info("Delele old file before {0} days\n".format( backup_day)) subprocess.call( 'find {0} -mtime +{1} -name "*.tar.gz" | xargs rm -f'.format( backup_dir, backup_day), shell=True) except Exception as e: back_logger.error("Delele old file before {0} days Failed\n".format( backup_day)) back_logger.critical(str(e), exc_info=True) continue
启动脚本:
#!/bin/shAPP_MAIN=backup_serverAPP_LOG=logsPID=0getPID(){ pythonps=`ps aux | grep $APP_MAIN | grep -v grep` if [ -n "$pythonps" ]; then PID=`echo $pythonps | awk '{print $2}'` else PID=0 fi}startup(){ getPID echo "================================================================================================================" if [ $PID -ne 0 ]; then echo "$APP_MAIN already started(PID=$PID)" echo "================================================================================================================" else echo -n "Starting $APP_MAIN" if [ ! -d "../$APP_LOG" ]; then mkdir "../$APP_LOG" fi nohup python $APP_MAIN > ../$APP_LOG/nohup.log 2>&1 & sleep 2 getPID if [ $PID -ne 0 ]; then echo "(PID=$PID)...[Success]" echo "================================================================================================================" else echo "[Failed]" echo "================================================================================================================" fi fi}startup
停止脚本:
#!/bin/shAPP_MAIN=backup_serverPID=0getPID(){ pythonps=`ps aux | grep $APP_MAIN | grep -v grep` if [ -n "$pythonps" ]; then PID=`echo $pythonps | awk '{print $2}'` else PID=0 fi}shutdown(){ getPID echo "================================================================================================================" if [ $PID -ne 0 ]; then echo -n "Stopping $APP_MAIN(PID=$PID)..." kill -9 $PID if [ $? -eq 0 ]; then echo "[Success]" echo "================================================================================================================" else echo "[Failed]" echo "================================================================================================================" fi getPID if [ $PID -ne 0 ]; then shutdown fi else echo "$APP_MAIN is not running" echo "================================================================================================================" fi}shutdownexit 0
查看服务状态
#!/bin/shAPP_MAIN=backup_serverPID=0getPID(){ pythonps=`ps aux | grep $APP_MAIN | grep -v grep` if [ -n "$pythonps" ]; then PID=`echo $pythonps | awk '{print $2}'` else PID=0 fi}getServerStatus(){ getPID echo "================================================================================================================" if [ $PID -ne 0 ]; then echo "$APP_MAIN is running(PID=$PID)" echo "================================================================================================================" else echo "$APP_MAIN is not running" echo "================================================================================================================" fi}getServerStatus
本文地址
本站地址:python自动化测试 python开发自动化测试群113938272和开发测试群6089740 微博