import logging
import os,sys
import tarfile,bz2
import time
import shutil
import subprocess
from ftplib import FTP
#
# 必要条件
# 1.安装Python
# 2.对GnuPG要求
# A.安装GnuPG且执行目录配在PATH内
# B.导入加密用户的公钥
# C:\..\> gpg --import svnbackup-public-key.txt
# C.对加密用户绝对信任
# C:\..\> gpg --edit-key svnbackup
# Command> trust
# ....
# ....
# 请选择 5
# ....
# Command> quit
# 3.启动"定时作业"程序
# 4.设置"定时作业",由于都为全量备份,最好不要设置每星期都执行,可以一星期执行一次
# C:\..\> at 05:00 /every:M,T,W,Th,F,S,Su f:\svndepository\backupsvn.bat
# C:\..\> at 23:30 /every:F f:\svndepository\backupsvn.bat
#
# 5.生成backupsvn.bat文件
# 内容如下:
# set USERPROFILE=C:\Documents and Settings\Administrator
# c:\Python24\python f:\svndepository\svnbackup.py
#
# 其中环境变量%USERPROFILE%是gpg所需要的
#
global_log='F:\\svnbackup\\svnbackup.log' #日志文件所在位置
global_crypt_person='svnbackup' #gpg public key所属的人
global_ftp_server='192.168.0.84' #ftp服务器的ip或名字
global_ftp_user='svnbackup' #ftp帐号
global_ftp_password='svnbackup' #ftp帐号对应的密码
global_destdir="F:\svnbackup\\" #备份所用的临时目录:最后的两个\\一定不能少
global_dbs=['F:\\svndepository\\svnroot\\huangshangzhong',
'F:\\svndepository\\svnroot\\ante.develop',
'F:\\svndepository\\svnroot\\ante.online',
'F:\\svndepository\\svnroot\\infoservice.develop',
'F:\\svndepository\\svnroot\\infoservice.online',
'F:\\svndepository\\svnroot\\szhelper']
logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s] [%(levelname)s] %(message)s',filename=global_log,filemode='w')
def compressBz2(srcDir,dstDir,format):
logging.debug("进入压缩目录函数:compressBz2")
if os.path.isdir(srcDir):
logging.info("准备备份:"+srcDir)
else:
logging.info("备份的源目录:"+srcDir+"不存在")
return '.'
if os.path.isdir(dstDir):
logging.info("将压缩到:"+dstDir)
#dstFileName=dstDir+time.strftime(format)+".tar.bz2"
#out=tarfile.TarFile.open(dstFileName,'w:bz2')
dstFileName=dstDir+time.strftime(format)+".tar"
out=tarfile.TarFile.open(dstFileName,'w')
try:
out.add(srcDir,srcDir)
out.close()
logging.info('成功压缩数据到:'+dstFileName)
return dstFileName
except:
out.close()
logging.error("压缩数据失败!")
return '.'
else:
logging.info("备份的目标目录:"+dstDir+"不存在")
return '.'
def ftpData(filename,server,user,password):
logging.debug("进入上传数据函数:ftpData")
ftp=FTP()
ftp.set_debuglevel(0)
ftp.connect(server,21)
ftp.login(user,password)
ftp.set_pasv(1)
logging.info("登陆ftp的反馈信息:"+ftp.getwelcome())
filehandle=open(filename,'rb')
fileshortname=os.path.basename(filename)
try:
ftp.storbinary('STOR '+fileshortname,filehandle,1024)
ftp.set_debuglevel(1)
filehandle.close()
ftp.quit()
ftp.close()
logging.info("上传数据:"+fileshortname+"成功!")
os.remove(filename)
return 0
except:
ftp.set_debuglevel(0)
filehandle.close()
ftp.quit()
ftp.close()
logging.error("上传数据:"+filename+"失败!")
return -1
def svnHotCopy(dbname,dstDir,format):
logging.debug("进入svn库备份函数:svnHotCopy")
if os.path.isdir(dbname):
logging.info("准备备份:"+dbname)
else:
logging.info("备份的源目录:"+dbname+"不存在")
return '-1'
if os.path.isdir(dstDir):
tmpDir=os.path.dirname(dstDir)+"\\"
dbshortname=os.path.basename(dbname)
dstFileDir=tmpDir+dbshortname+time.strftime(format)
logging.info("正在备份版本库:"+dbname)
else:
logging.info("备份的目标目录:"+dstDir+"不存在")
return '-2'
cmd=['svnadmin','hotcopy',dbname,dstFileDir]
rst=subprocess.call(cmd)
if rst==0:
logging.info("版本库:"+dbname+"成功备份到了:"+dstDir+"!")
return dstFileDir
else:
logging.info("备份版本库:"+dbname+"到:"+dstDir+"失败!")
return '-3'
def cryptFile(filename,ext):
logging.debug("进入加密文件函数:cryptFile")
destfile=filename+ext
cmd=['gpg','-z','0','-o',destfile,'-er',global_crypt_person,filename]
rst=subprocess.call(cmd)
if rst==0:
logging.info("加密文件:"+filename+"成功")
os.system("del "+filename)
return destfile
else:
logging.info("加密文件:"+filename+"失败")
return '.'
defdoSvnBackup():
logging.info("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
logging.debug("进入主函数:SvnBackup")
for x in global_dbs:
try:
logging.info("-=-=-=-=--=-=--")
y=svnHotCopy(x,global_destdir,'-%Y%m%d%H%M%S')
logging.debug("生成目录:"+y)
bz2f=compressBz2(y,y,'')
logging.debug("生成压缩文件:"+bz2f)
cryf=cryptFile(bz2f,'.gpg')
logging.debug("生成加密文件:"+cryf)
rs=ftpData(cryf,global_ftp_server,global_ftp_user,global_ftp_password)
if rs==0:
os.system("attrib -h -r "+y+"\\*.* /s")
shutil.rmtree(y,1)
logging.info("备份:"+x+"成功!")
else:
logging.error("备份:"+x+"失败!")
except:
logging.error("主函数执行过程中有异常发生")
if __name__ == "__main__":
doSvnBackup()
logging.shutdown()
#以下是定时任务
rem ==================================================
rem at 23:30 /every:M,T,W,Th,F,S,Su f:\svndepository\backupsvn.bat
rem at 23:30 /every:T,F f:\svndepository\backupsvn.bat
rem ==================================================
rem == Set GnuPG USERPROFILE
set USERPROFILE=C:\Documents and Settings\Administrator
c:\Python24\python f:\svndepository\svnbackup.py
<!-- CoreMail Version 3.1_dev Copyright (c) 2002-2007 www.mailtech.cn -->