#!/usr/bin/env python
# -*- coding: cp936 -*-
'''
Created on 2011-10-2
@author: caojj
'''
import subprocess
import os
import sys
import codecs
import traceback
import shutil
import string
import time
import datetime
DEBUG_FLAG=True
file_new = None
def getFileName():
fileList = __file__.split(os.path.sep)
strRtn = unicode(fileList[len(fileList) - 1])
return strRtn
'''
@param deps: 日志深度
@return: 无返回值
'''
def printLog(strlog, deps, linenum, strPre=" " , isPrint = True):
listStr = ""
strlog = unicode(strlog)
if isinstance(strlog,list):
for one in strlog:
if isinstance(one,int):
listStr = listStr +unicode(one)
elif isinstance(one ,str):
listStr = listStr + one
strlog = unicode(listStr)
strPre = strPre*(deps + 1)
if DEBUG_FLAG :
strlog = u"DEBUG|" + backupConfig.getCurrentDateTime() + "|" + getFileName() + u":" + unicode(linenum) + u"|" + strlog
else:
strlog = u"RELEASE|" + backupConfig.getCurrentDateTime() + "|" + getFileName() + u":" + unicode(linenum) + u"|" + strlog
if isPrint:
print strPre + strlog
time.sleep(1)
if file_new != None:
file_new.write(strPre + strlog + "\n")
'''
@author: caojiaju
@note: 备份配置项
'''
class backupConfig:
def __init__(self,startindex):
#self.backlog_file=""
self.verboseSize = 5
self.back_root_dir="/root/backup" + os.path.sep
self.outdatefile=[]
self.startIndex = startindex + 1
self.configList = [u'/svnroot/config/authz.conf']
'''
@note: 获取当前日期,
@return: 返回格式为YYYY-MM-DD
'''
@staticmethod
def getCurrentDate():
strCurrentDate = time.strftime(u"%Y-%m-%d",time.localtime())
return unicode(strCurrentDate)
'''
@note: 获取当前日期,
@return: 返回格式为YYYY-MM-DD HH-MM-SS
'''
@staticmethod
def getCurrentDateTime():
strCurrentDate = time.strftime(u"%Y-%m-%d %X",time.localtime())
return unicode(strCurrentDate)
'''
@note: 获取过期的备份文件
@return: 列表【file1,file2】--字符串列表
'''
def fetchOutdateFile(self):
#self.outdatefile.clear()
self.outdatefile[:] = []
printLog(u"Begin Fetch Outdate file" , self.startIndex,traceback.extract_stack()[-2][1])
if os.path.isdir(self.back_root_dir) == False:
printLog (u"",self.startIndex,traceback.extract_stack()[-2][1])
for root, subdirs, files in os.walk(self.back_root_dir):
for name in files:
filename= os.path.join(root, name)
mtimeC = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
nowTime = datetime.datetime.now()
nowTime = nowTime - datetime.timedelta(hours=24*self.verboseSize)
if mtimeC < nowTime :
self.outdatefile.append(filename)
return
'''
@note: 删除过期备份文件
@return: 无返回值
'''
def removeOutdateFile(self):
#初始化过期列表
self.fetchOutdateFile()
if len(self.outdatefile) > 0:
printLog(u"Begin Delete All outdate file" , self.startIndex,traceback.extract_stack()[-2][1])
for filepath in self.outdatefile:
self.removeOneFile(filepath)
printLog(u"End Delete outdate file" , self.startIndex,traceback.extract_stack()[-2][1])
else:
printLog(u"Cannot Find Outdate file." , self.startIndex,traceback.extract_stack()[-2][1])
return
'''
@note: 文件拷贝
@return: 无返回值
'''
def copyFileto(self,sourceDir, targetDir):
shutil.copy(sourceDir, targetDir)
'''
@note: 删除单个过期备份文件
@return: 无返回值
'''
def removeOneFile(self,filepath):
printLog(u"Remove file :" + filepath , self.startIndex + 1,traceback.extract_stack()[-2][1])
os.remove(filepath)
#t = time.ctime(os.path.getmtime(filepath))
#printLog(u"Time is:" + t.__str__() ,self.startIndex +2)
# if oneStr == None or oneStr == "":
# printLog(u"File Delete Failed!" + unicode(oneStr) , self.startIndex + 1)
# else:
printLog(u"Succeed" , self.startIndex + 1,traceback.extract_stack()[-2][1])
return
'''
@note: 单个文件备份
'''
def backOneConfig(self,filename):
printLog(u"Back file :" + filename , self.startIndex + 1,traceback.extract_stack()[-2][1])
descFile = self.back_root_dir + u"Config"
if os.path.isdir(descFile) != True:
os.makedirs(descFile)
if os.path.exists(filename) != True:
printLog(u"File : " +descFile + " Is Not Exist,Backup Skipped!" , self.startIndex + 1,traceback.extract_stack()[-2][1])
return
listStr = filename.split(os.path.sep)
descFile = descFile + os.path.sep + listStr[len(listStr) - 1] + u"." + backupConfig.getCurrentDate()
shutil.copy(filename, descFile)
printLog(u"Succeed" , self.startIndex + 1,traceback.extract_stack()[-2][1])
return
'''
@note: 备份配置文件
'''
def backConfigFile(self):
printLog(u"Begin Backup Config file" , self.startIndex,traceback.extract_stack()[-2][1])
if len(self.configList) == 0 :
printLog(u"No need of backup files",self.startIndex + 1,traceback.extract_stack()[-2][1])
return
for cfile in self.configList:
self.backOneConfig(cfile)
printLog(u"End Backup Config file" , self.startIndex,traceback.extract_stack()[-2][1])
return
'''
@author: caojiaju
@note:配置库备份
'''
class backSvn:
def __init__(self,startindex):
self.sourceList=['/rootsvn/aaa']
self.config=backupConfig(startindex)
self.startIndex = startindex + 1
'''
@note: 单个svn配置库备份
@return: 无返回值
'''
def backOne(self,urladdress):
printLog(u"Begin Back " + urladdress , self.startIndex + 1,traceback.extract_stack()[-2][1])
#/usr/local/subversion/bin/svnadmin
strSplit = urladdress.split(os.path.sep)
descFile = self.config.back_root_dir + u"SVN" + os.path.sep + strSplit[len(strSplit) - 1]
if os.path.isdir(descFile) != True:
os.makedirs(descFile)
descFile = descFile + os.path.sep + strSplit[len(strSplit) - 1] + u"." + backupConfig.getCurrentDate()
strCommand = u"/usr/local/subversion/bin/svnadmin dump " + urladdress + u" > " + descFile
output=subprocess.Popen(strCommand,shell = True,stdout = subprocess.PIPE)
printLog(output.communicate()[0] , self.startIndex + 1,traceback.extract_stack()[-2][1],u" ",False)
printLog(u"Backup Finished",self.startIndex + 1,traceback.extract_stack()[-2][1])
return
'''
@note: 所有SVN备份
@return: 无返回值
'''
def backAll(self):
printLog(u"Begin Back All SVN Depsitory" , self.startIndex,traceback.extract_stack()[-2][1])
if len(self.sourceList) == 0 :
printLog(u"No need of backup svn",self.startIndex + 1,traceback.extract_stack()[-2][1])
return
for svnaddress in self.sourceList:
self.backOne(svnaddress)
printLog(u"End Back All SVN Depsitory" , self.startIndex,traceback.extract_stack()[-2][1])
'''
@author: caojiaju
@note: 数据库备份
'''
class backDatabase:
def __init__(self,startindex):
self.sourceList=[['test','vision','secret','Test database'],['test2','ttt','ttt','ttt','ttt']]
self.config = backupConfig(startindex)
self.startIndex = startindex + 1
'''
@note: 单个数据库备份
@return: 无返回值
'''
def backOne(self,dbinfo):
if len(dbinfo) < 4 :
printLog(u"Db info Not enough : " + dbinfo, self.startIndex + 1,traceback.extract_stack()[-2][1])
return
dbsource=dbinfo[0]
username=dbinfo[1]
password=dbinfo[2]
remark=dbinfo[3]
# exp sys/password@xxxx file=xxx user
descFile = self.config.back_root_dir + u"Database" + os.path.sep + dbsource
if os.path.isdir(descFile) != True:
os.makedirs(descFile)
printLog(u"Begin Back " + dbsource , self.startIndex + 1,traceback.extract_stack()[-2][1])
descFile = descFile + os.path.sep + dbsource + u".dump." + backupConfig.getCurrentDate()
strCommand = u"exp " + username + u"/" + password + u"@" + dbsource + u" file=" + descFile
output=subprocess.Popen(strCommand,shell = True,stdout = subprocess.PIPE)
printLog(output.communicate()[0] , self.startIndex + 1,traceback.extract_stack()[-2][1]," " ,False)
printLog(u"Backup Successed",self.startIndex + 1,traceback.extract_stack()[-2][1])
return
'''
@note: 所有数据库备份
@return: 无返回值
'''
def backAll(self):
printLog(u"Begin Back All Database" , self.startIndex,traceback.extract_stack()[-2][1])
if len(self.sourceList) == 0 :
printLog(u"No need of backup database",self.startIndex + 1,traceback.extract_stack()[-2][1])
return
for dbinfo in self.sourceList:
self.backOne(dbinfo)
printLog(u"End Back All Databasey" , self.startIndex,traceback.extract_stack()[-2][1])
log_deps_index = 0
def dealBack():
global log_deps_index
#检查磁盘
log_deps_index = log_deps_index + 1
print u"\n"
printLog(u"Begin check disk" , log_deps_index,traceback.extract_stack()[-2][1])
config = backupConfig(log_deps_index)
config.removeOutdateFile()
printLog(u"End check disk" , log_deps_index,traceback.extract_stack()[-2][1])
#return
print u"\n"
printLog(u"Begin Backup config file" , log_deps_index,traceback.extract_stack()[-2][1])
config = backupConfig(log_deps_index)
config.backConfigFile()
printLog(u"End Backup config file" , log_deps_index,traceback.extract_stack()[-2][1])
print u"\n"
#备份svn配置库
printLog(u"Begin Backup SVN" , log_deps_index,traceback.extract_stack()[-2][1])
svnBack = backSvn(log_deps_index)
svnBack.backAll()
printLog(u"End Backup SVN" , log_deps_index,traceback.extract_stack()[-2][1])
print u"\n"
#备份数据库
printLog(u"Begin Backup Database" , log_deps_index,traceback.extract_stack()[-2][1])
dbBack=backDatabase(log_deps_index)
dbBack.backAll()
printLog(u"End Backup Database" , log_deps_index,traceback.extract_stack()[-2][1])
log_deps_index = log_deps_index - 1
def main():
global log_deps_index
global file_new
conf = backupConfig(log_deps_index + 1)
try:
file_new = codecs.open(conf.back_root_dir + u"runlog.log",'a','utf-8')
except:
file_new.close()
file_new = None
printLog("Configure Load Failed",log_deps_index , traceback.extract_stack()[-2][1],"***")
log_deps_index = log_deps_index + 1
printLog(u"Start Back" , log_deps_index,traceback.extract_stack()[-2][1],"***")
dealBack()
#辽东、巴蜀
print u"\n"
printLog(u"End Backup" , log_deps_index , traceback.extract_stack()[-2][1],"***")
file_new.close()
if __name__=='__main__':
main()