Python backup Mysql script

Source: Internet
Author: User

Copy codeThe Code is as follows:
#! /Usr/bin/python

Import OS
Import time
Import ftplib
Import traceback

# Config vars
Systempathchr = "/" # path delimiter, * nix "/" win32 "\\"

Dbuser = "root" # database username
Dbpwd = "dbpwd" # Database Password
Dbnamelist = ["dbone", "dbtwo", "dbthree"] # databases to be backed up

Workdir = "/path/to/backup/" # local backup folder
Errlogfile = "databack. log" # Error log name
Ftp_addr = "192.168.0.2" # ftp address
Ftp_port = "2102" # ftp port
Ftp_user = "databack" # ftp user name
Ftp_pwd = "backpwd" # ftp Password
Ftp_path = "/" # store it in the ftp path

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 t:
Path = OS. path. join (workdir, errlogfile)
Traceback. print_exc (file = open (path, ""))



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

I did not take a closer look, but in the following two sentences, we recommend that you look at the functions in the OS. path module. You may not need to set different separators for linux and Windows.
# Config vars
Systempathchr = "/" # path delimiter, * nix "/" win32 "\\"
We can see that the code is used to get the file name. You can try OS. path. basename to live OS. path. split.
 Copy codeThe Code is 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.