Python备份Mysql脚本
时间:2024-01-16 05:19:28
#!/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'
标签:python,mysql备份
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python PaddleSpeech实现婴儿啼哭识别
2023-08-22 22:25:31
09七夕节各大搜索引擎LOGO欣赏
2009-08-27 15:34:00
![](https://img.aspxhome.com/file/UploadPic/20098/27/09-tanabata-festival-search-logo-44s.jpg)
利用Echarts如何实现多段圆环图
2024-04-28 09:36:22
![](https://img.aspxhome.com/file/2023/1/132881_0s.png)
在Pycharm terminal中字体大小设置的方法
2023-11-12 07:12:56
Python实现的knn算法示例
2022-09-21 14:35:09
![](https://img.aspxhome.com/file/2023/0/114790_0s.png)
python用opencv 图像傅里叶变换
2021-03-02 02:04:35
![](https://img.aspxhome.com/file/2023/1/92471_0s.png)
Python如何实现强制数据类型转换
2022-10-18 10:08:02
Vue路由的模块自动化与统一加载实现
2024-04-27 16:17:15
![](https://img.aspxhome.com/file/2023/9/132929_0s.png)
python3实现多线程聊天室
2021-09-11 07:35:47
![](https://img.aspxhome.com/file/2023/9/75249_0s.png)
oracle跨库查询的方法
2023-07-18 03:15:35
一些sql语句
2009-04-10 18:36:00
一步步解析Python斗牛游戏的概率
2021-05-03 05:17:27
解决Django部署设置Debug=False时xadmin后台管理系统样式丢失
2022-05-09 00:33:57
![](https://img.aspxhome.com/file/2023/5/133225_0s.jpg)
Python中装饰器高级用法详解
2023-09-30 09:46:42
NodeJs Express路由使用流程解析
2024-05-11 10:16:56
Python实现求解括号匹配问题的方法
2023-08-02 08:05:47
![](https://img.aspxhome.com/file/2023/5/61465_0s.png)
python 实现手机自动拨打电话的方法(通话压力测试)
2021-03-19 08:10:34
可以自动轮换的页签 tabs
2008-02-21 12:25:00
Python四大金刚之字典详解
2022-02-22 23:11:20
![](https://img.aspxhome.com/file/2023/8/127878_0s.jpg)
用Python一键搭建Http服务器的方法
2021-08-09 19:36:32
![](https://img.aspxhome.com/file/2023/8/121058_0s.jpg)