国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python備份Mysql腳本

2020-01-04 18:06:45
字體:
供稿:網(wǎng)友
特點(diǎn)是多平臺(tái),一個(gè)腳本內(nèi)可以備份多個(gè)數(shù)據(jù)庫,并分別打包上傳到ftp進(jìn)行備份。調(diào)用了mysqldump及tar來進(jìn)行數(shù)據(jù)庫dump及打包。 具體參數(shù)說明參見源文件
 

 

復(fù)制代碼代碼如下:

#!/usr/bin/python  

import os  
import time  
import ftplib  
import traceback  

#config vars  
systempathchr="/" #路徑分割符,*nix用"/" win32用"//"  

dbuser="root" #數(shù)據(jù)庫用戶名  
dbpwd="dbpwd" #數(shù)據(jù)庫密碼  
dbnamelist=["dbone","dbtwo","dbthree"] #需要備份那些數(shù)據(jù)庫  

workdir="/path/to/backup/" #本地備份文件夾  
errlogfile="databack.log" #錯(cuò)誤日志名  
ftp_addr="192.168.0.2" #ftp地址  
ftp_port="2102" #ftp端口  
ftp_user="databack" #ftp用戶名  
ftp_pwd="backpwd" #ftp密碼  
ftp_path="/" #存放到ftp路徑  

ftpqueue=[]  


def ftpstor():  
    #login  
    bufsize=1024  
    ftp=ftplib.FTP()  
    try:  
        ftp.connect(ftp_addr,ftp_port)  
        ftp.login(ftp_user,ftp_pwd)  
        ftp.cwd(ftp_path)  
        for filepath in ftpqueue:  

            #open file for input as binary  
            f=open(filepath,"rb")  
            #store file as binary  
            print getfilename(filepath)  
            ftp.storbinary("STOR "+getfilename(filepath),f,bufsize)  
            f.close()  
        ftp.quit()  
    except:  
        path=os.path.join(workdir,errlogfile)  
        traceback.print_exc(file=open(path,"a"))  

      

def dumpdb(dbname):  
    global ftpqueue  
    timeformat="%Y%m%d"  
    sqlvalformat="mysqldump -u%s -p/"%s/" /"%s/" >/"%s/""  
    tarvalformat="tar --directory=/"%s/" -zcf /"%s/" /"%s/""  
    nowdate=time.strftime(timeformat)  
    dumpfile=os.path.join(workdir,dbname+".dump")  
    zipfile=os.path.join(workdir,dbname+nowdate+".tar.gz")  
    sqlval=sqlvalformat % (dbuser,dbpwd,dbname,dumpfile)  

    result=os.system(sqlval)  
    tarval=tarvalformat % (workdir,zipfile,dbname+".dump")  

    result=os.system(tarval)  
    os.remove(dumpfile)  
    ftpqueue.append(zipfile)  

def getfilename(path):  

    pt=path.rfind(systempathchr)  
    return path[pt+1:]  

def main():  
    for dbname in dbnamelist:  
        dumpdb(dbname)  

    ftpstor()  

main()

沒有仔細(xì)看,不過下面這兩句,推薦看看os.path模塊里面的函數(shù),可能就不用針對(duì)linux和win分別設(shè)定不同的分隔符了 引用   
#config vars 
systempathchr="/" #路徑分割符,*nix用"/" win32用"//"   
看到代碼里面是用在得到文件名的,可以試試os.path.basename活著os.path.split了 
 
復(fù)制代碼代碼如下:

 >>> import os.path  
  >>> os.path.basename("c://test//aa.txt")  
  'aa.txt'  
  >>> os.path.split("c://test//aa.txt")  
  ('c://test', 'aa.txt')  
  >>> os.path.split("c://test//aa.txt")[-1]  
  'aa.txt'  
  >>> os.path.basename("/home/test/aa.txt")  
  'aa.txt'  
  >>> os.path.split("/home/test/aa.txt")  
  ('/home/test', 'aa.txt')  
  >>> os.path.basename("/home/test/aa.txt")  
  'aa.txt'

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝清县| 扶绥县| 保德县| 泾川县| 海安县| 合作市| 宿松县| 修文县| 安国市| 新兴县| 库车县| 景泰县| 榆中县| 峨边| 兖州市| 临沧市| 楚雄市| 名山县| 颍上县| 河津市| 新邵县| 丽江市| 高邑县| 军事| 晴隆县| 灌南县| 祁门县| 合肥市| 凌云县| 长葛市| 雅安市| 诏安县| 辽源市| 成武县| 崇州市| 化德县| 开江县| 缙云县| 吉水县| 红桥区| 化州市|