Python備份Mysql腳本
更新時(shí)間:2008年08月11日 21:31:12 作者:
特點(diǎn)是多平臺(tái),一個(gè)腳本內(nèi)可以備份多個(gè)數(shù)據(jù)庫(kù),并分別打包上傳到ftp進(jìn)行備份。調(diào)用了mysqldump及tar來(lái)進(jìn)行數(shù)據(jù)庫(kù)dump及打包。
具體參數(shù)說(shuō)明參見源文件
復(fù)制代碼 代碼如下:
#!/usr/bin/python
import os
import time
import ftplib
import traceback
#config vars
systempathchr="/" #路徑分割符,*nix用"/" win32用"\\"
dbuser="root" #數(shù)據(jù)庫(kù)用戶名
dbpwd="dbpwd" #數(shù)據(jù)庫(kù)密碼
dbnamelist=["dbone","dbtwo","dbthree"] #需要備份那些數(shù)據(jù)庫(kù)
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()
沒(méi)有仔細(xì)看,不過(guò)下面這兩句,推薦看看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'
相關(guān)文章
Pycharm簡(jiǎn)單使用教程(入門小結(jié))
這篇文章主要介紹了Pycharm簡(jiǎn)單使用教程(入門小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Python實(shí)現(xiàn)執(zhí)行Shell命令并獲取輸出
這篇文章主要介紹了如何借助?os.system()?從?Python?腳本執(zhí)行?cmd?命令,以及如何借助?Python?中的?subprocess?模塊以更簡(jiǎn)單的方式從腳本執(zhí)行?cmd?命令,感興趣的小伙伴可以了解下2023-10-10python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例詳解
數(shù)據(jù)清洗是指發(fā)現(xiàn)并糾正數(shù)據(jù)文件中可識(shí)別的錯(cuò)誤的最后一道程序,包括檢查數(shù)據(jù)一致性,處理無(wú)效值和缺失值等,數(shù)據(jù)清洗與處理的目的是提高數(shù)據(jù)的質(zhì)量,提高實(shí)驗(yàn)結(jié)果的可靠度,本文給大家介紹了python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例,需要的朋友可以參考下2024-08-08詳解如何利用Pytest?Cache?Fixture實(shí)現(xiàn)測(cè)試結(jié)果緩存
這篇文章主要為大家詳細(xì)介紹了如何利用Pytest?Cache?Fixture實(shí)現(xiàn)測(cè)試結(jié)果緩存,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-09-09使用OpenCV實(shí)現(xiàn)讀取和顯示圖像與視頻
OpenCV 是一個(gè)強(qiáng)大的計(jì)算機(jī)視覺庫(kù),廣泛應(yīng)用于圖像處理和視頻處理等領(lǐng)域,本文將詳細(xì)介紹如何使用 OpenCV 在 Python 中讀取和顯示圖像以及視頻,希望對(duì)大家有所幫助2024-11-11使用jupyter notebook直接打開.md格式的文件
這篇文章主要介紹了使用jupyter notebook直接打開.md格式的文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04淺談pytorch grad_fn以及權(quán)重梯度不更新的問(wèn)題
今天小編就為大家分享一篇淺談pytorch grad_fn以及權(quán)重梯度不更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08基于OpenCV4.2實(shí)現(xiàn)單目標(biāo)跟蹤
這篇文章主要介紹了如何和何時(shí)使用OpenCV 4.2中可用的8種不同的跟蹤器- BOOSTING, MIL, KCF, TLD, MEDIANFLOW, GOTURN, MOSSE和CSRT,并用他們實(shí)現(xiàn)單目標(biāo)跟蹤,需要的可以參考一下2022-03-03