#-*- coding:gbk-*- #code by HAWK.Li #version 1.0 #2009-02-01 #想抄代码的同学可以抄,不过记得留个版权信息,也让我可以满足一把
import os import time, sys import traceback import ftplib reload(sys)
sys.setdefaultencoding('utf-8') IFS="\\" #路径分割符,linux用"/" win32用"\\" nowdate=time.strftime("%Y%m%d%H%M") #当前日期 homedir = os.getcwd() #当前路径 workdir="C:\\test\\" #本地备份文件夹 errlogfile="C:\\test\\PROGerror"+nowdate+".log" #错误日志名 dbuser="test/test" #数据库用户名 dblog="c:\\test\\EXP"+nowdate+".log" #DB备份过程中的exp日志 dbconst="FULL=Y" #DB的其他参数字串 dbnamelist=["test"] #需要备份那些数据库 expfile= "C:\\oracle\\BIN\\exp" #oracle的exp文件所在目录 bufsize = 1024 #FTP的缓存,不要改 ftp_addr="127.0.0.1" #ftp地址 ftp_port="21" #ftp端口 ftp_user="test" #ftp用户名 ftp_pwd="test" #ftp密码 ftp_path="/" #远端ftp路径
def dumpdb(dbname): filename=dbname+"_"+nowdate #备份文件名 sqlvalformat ="%s userid=%s file=%s log=%s %s" dumpfile=os.path.join(workdir,filename+".dmp") #DUMP文件名 sqlval=sqlvalformat % (expfile,dbuser,dumpfile,dblog,dbconst) result=os.system(sqlval) #print sqlval
def getfilename(path): pt=path.rfind(IFS) return path[pt+1:]
def rarfile(destfile,sourcefile): rarprogram=os.path.join(homedir,"RAR.exe") #rar for dos放在本地目录 rarvalformat = "%s a \"%s\" \"%s\" -r" #rar 的语法格式 rarfilename=os.path.join(workdir,destfile+"_"+nowdate+".rar") #压缩文件名 sourcefilename=os.path.join(workdir,sourcefile+"_"+nowdate+".dmp") #备份源文件 rarval= rarvalformat % (rarprogram,rarfilename,sourcefilename) result=os.system(rarval) #print rarval
def ftpstor(ftpfile):
ftp=ftplib.FTP() try: ftp.connect(ftp_addr,ftp_port) ftp.set_pasv(True) ftp.login(ftp_user,ftp_pwd) print ftp.getwelcome() ftp.cwd(ftp_path) f = open(ftpfile,'rb') print getfilename(ftpfile)," uploading..." ftp.storbinary("STOR %s "% getfilename(f.name) ,f,bufsize) #上传文件 print getfilename(ftpfile)," upload completed." ftp.close() f.close() #关闭文件 ftp.quit() except: info=sys.exc_info() path=os.path.join(workdir,errlogfile) traceback.print_exc(file=open(path,"a"))
def main(): for dbname in dbnamelist: print time.strftime('%Y-%m-%d:%H:%M:%S') print "-----------------------------------------------" print dbname[:], "DB is backup now..." dumpdb(dbname) print dbname[:], "DB backup completed." print "-----------------------------------------------" print "dmpfile compress now..." rarfile(dbname,dbname) print "dmpfile compress completed." print "-----------------------------------------------" ftpfilename=os.path.join(workdir,dbname+"_"+nowdate+".rar") print ftpfilename," FTP session starting..." ftpstor(ftpfilename) print ftpfilename," FTP session completed." print "-----------------------------------------------" dumpfile=os.path.join(workdir,dbname+"_"+nowdate+".dmp") os.remove(dumpfile) print dumpfile," remove completed." print "-----------------------------------------------" main()
|