前言:
目前兄弟们对“linux定时备份mysql数据库”大体比较关注,大家都想要知道一些“linux定时备份mysql数据库”的相关文章。那么小编同时在网络上收集了一些对于“linux定时备份mysql数据库””的相关内容,希望看官们能喜欢,兄弟们快快来学习一下吧!/bin/python #-*- coding:UTF-8 -*-import os import time import ftplib import traceback #config vars systempathchr="/" #路径分割符,*nix用"/" win32用"\\" dbuser="root" #数据库用户名 dbpwd="123." #数据库密码 dbnamelist=["bbs","dwz","typecho","36ynet","phpwind9","seavbbs"] #需要备份那些数据库 workdir="/home/wwwroot/m.ay.lc/back/" #本地备份文件夹 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: for filepath in ftpqueue: #open file for input as binary f=open(filepath,"rb") #store file as binary print getfilename(filepath) ("STOR "+getfilename(filepath),f,bufsize) f.close() 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()
标签: #linux定时备份mysql数据库