pythonスクリプトでsvnライブラリをバックアップ、圧縮、暗号化、ftpアップロード
import loggingimport os,sysimport tarfile,bz2import timeimport shutilimport subprocessfrom 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