Python backup MySQL script _python

Source: Internet
Author: User

Copy Code code as follows:

#!/usr/bin/python

Import OS
Import time
Import Ftplib
Import Traceback

#config VARs
Systempathchr= "/" #路径分割符, *nix with "/" Win32 with "\"

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 ()

Do not look carefully, but the following two sentences, recommended to look at the Os.path module inside the function, may not be for Linux and win separately set different separator reference
#config VARs
Systempathchr= "/" #路径分割符, *nix with "/" Win32 with "\"
See the code inside is used to get the filename, you can try Os.path.basename alive Os.path.split
Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.