欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python備份Mysql腳本

 更新時間:2008年08月11日 21:31:12   作者:  
特點(diǎn)是多平臺,一個腳本內(nèi)可以備份多個數(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" #錯誤日志名 
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ù),可能就不用針對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'

相關(guān)文章

  • Python基礎(chǔ)之模塊詳解

    Python基礎(chǔ)之模塊詳解

    本文詳細(xì)講解了Python基礎(chǔ)之模塊,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Pycharm簡單使用教程(入門小結(jié))

    Pycharm簡單使用教程(入門小結(jié))

    這篇文章主要介紹了Pycharm簡單使用教程(入門小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-07-07
  • Python實(shí)現(xiàn)執(zhí)行Shell命令并獲取輸出

    Python實(shí)現(xiàn)執(zhí)行Shell命令并獲取輸出

    這篇文章主要介紹了如何借助?os.system()?從?Python?腳本執(zhí)行?cmd?命令,以及如何借助?Python?中的?subprocess?模塊以更簡單的方式從腳本執(zhí)行?cmd?命令,感興趣的小伙伴可以了解下
    2023-10-10
  • python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例詳解

    python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例詳解

    數(shù)據(jù)清洗是指發(fā)現(xiàn)并糾正數(shù)據(jù)文件中可識別的錯誤的最后一道程序,包括檢查數(shù)據(jù)一致性,處理無效值和缺失值等,數(shù)據(jù)清洗與處理的目的是提高數(shù)據(jù)的質(zhì)量,提高實(shí)驗(yàn)結(jié)果的可靠度,本文給大家介紹了python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例,需要的朋友可以參考下
    2024-08-08
  • 詳解如何利用Pytest?Cache?Fixture實(shí)現(xiàn)測試結(jié)果緩存

    詳解如何利用Pytest?Cache?Fixture實(shí)現(xiàn)測試結(jié)果緩存

    這篇文章主要為大家詳細(xì)介紹了如何利用Pytest?Cache?Fixture實(shí)現(xiàn)測試結(jié)果緩存,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-09-09
  • 使用OpenCV實(shí)現(xiàn)讀取和顯示圖像與視頻

    使用OpenCV實(shí)現(xiàn)讀取和顯示圖像與視頻

    OpenCV 是一個強(qiáng)大的計算機(jī)視覺庫,廣泛應(yīng)用于圖像處理和視頻處理等領(lǐng)域,本文將詳細(xì)介紹如何使用 OpenCV 在 Python 中讀取和顯示圖像以及視頻,希望對大家有所幫助
    2024-11-11
  • 使用jupyter notebook直接打開.md格式的文件

    使用jupyter notebook直接打開.md格式的文件

    這篇文章主要介紹了使用jupyter notebook直接打開.md格式的文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 淺談pytorch grad_fn以及權(quán)重梯度不更新的問題

    淺談pytorch grad_fn以及權(quán)重梯度不更新的問題

    今天小編就為大家分享一篇淺談pytorch grad_fn以及權(quán)重梯度不更新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python使用matplotlib 畫矩形的三種方式分析

    Python使用matplotlib 畫矩形的三種方式分析

    這篇文章主要介紹了Python使用matplotlib 畫矩形的三種方式,結(jié)合實(shí)例形式分析了Python基于matplotlib繪制矩形的具體實(shí)現(xiàn)方法與相關(guān)操作注意事項,需要的朋友可以參考下
    2019-10-10
  • 基于OpenCV4.2實(shí)現(xiàn)單目標(biāo)跟蹤

    基于OpenCV4.2實(shí)現(xiàn)單目標(biāo)跟蹤

    這篇文章主要介紹了如何和何時使用OpenCV 4.2中可用的8種不同的跟蹤器- BOOSTING, MIL, KCF, TLD, MEDIANFLOW, GOTURN, MOSSE和CSRT,并用他們實(shí)現(xiàn)單目標(biāo)跟蹤,需要的可以參考一下
    2022-03-03

最新評論