Python備份Mysql指令碼_python

來源:互聯網
上載者:User

複製代碼 代碼如下:

#!/usr/bin/python 

import os 
import time 
import ftplib 
import traceback 

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

dbuser="root" #資料庫使用者名稱 
dbpwd="dbpwd" #資料庫密碼 
dbnamelist=["dbone","dbtwo","dbthree"] #需要備份那些資料庫 

workdir="/path/to/backup/" #本地備份檔案夾 
errlogfile="databack.log" #錯誤記錄檔名 
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()

沒有仔細看,不過下面這兩句,推薦看看os.path模組裡面的函數,可能就不用針對linux和win分別設定不同的分隔字元了 引用  
#config vars
systempathchr="/" #路徑分割符,*nix用"/" win32用"\\"  
看到代碼裡面是用在得到檔案名稱的,可以試試os.path.basename活著os.path.split了 
 
複製代碼 代碼如下:

 >>> 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'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.