MySQL Backup tool mydumper Backup

Source: Internet
Author: User
Tags mysql backup

#!/usr/bin/phthonimport osimport timeimport commandsimport shutilimport threadingfrom os.path Import Join,  Getsizeimport MySQLdb as mydb# backup directory basedir = "/data2/backup/backup_data/" # ns or WX; Whether to compress after backup (Mydumper comes with compression), to compress True, otherwise FALSE.IDC = ' ns '; Iszip = true# backup failed retry, True retry, do not retry set False, Retry_sleep start retry (seconds) Is_errretrybackup = true; Retry_sleep = 300# Backup Date backup_date = Time.strftime ("%y%m%d") # backup command cmd = "/usr/local/bin/mydumper-h%s-u root-p passwor D? -P%s%s-t 5-o%s "" ' Function Description: 1. Mydumper Remote Bulk Backup, the backup list is provided by the configuration file 2. The backup can be compressed as required (Mydumper Auto-compression) 3. Backup failure allows you to try back up 4 again. Backup information written to the database "Def main (): Thread_pool = [] # Whether to enable compression zip = '-C ' if iszip = = True Else ' # read IP, name, port, and spell from config file Rounding up the BACKUP statement #f = open ('/data2/backup/cnf/other_list.cnf ', ' r ') F = open ('/data/other_list.cnf ', ' r ') for lines in F.rea Dlines (): if (not Lines.startswith (' # ') and Len (Lines.strip ()) > 0): str = lines.split () h OST, BusinessName, port, IsMaster = Str[0], str[1], STR[2], Str[3] # Business folder does not exist then create DIR = BaseDir + '/' + BusinessName;            if (not os.path.exists (dir)): Os.makedirs (dir) dir + = "/%s%s"% (BusinessName, backup_date) # Business Directory: dir, backup directory: dir/name+ backup date strcmd = cmd% (host, port, zip, dir) th = threading. Thread (target = mydumper, args = (Strcmd, dir, BusinessName, host, port, is_errretrybackup, int (ismaster))) thre        Ad_pool.append (TH) if (Thread_pool): for T in Thread_pool:t.daemon = True T.start ()    For T in Thread_pool:t.join () def mydumper (Scmd, Backupdir, BusinessName, host, Port, Is_retry, IsMaster): Master_host = ""; Backup_host = host; name = BusinessName; Port = port; Backup_type = 1;    File = ""; Start_time = ""; Stop_time = ""; ReturnCode = 0; file_size = 0; Slave_statement = "";    Std_err = ""; Start_time = Time.strftime ("%y%m%d%h%m%s") # Clears the possible legacy backup if (Os.path.exists (backupdiR): Shutil.rmtree (Backupdir) # performs backup returncode, Std_err = Execute (scmd) stop_time = Time.strftime ("%y%m%d        %h%m%s ") if (ReturnCode = = 0): # backup Std_err return is not empty also considered an error.            if (Std_err.strip () = ""): ReturnCode = 123456 Else: # Get the change master to information and verify again that the backup is valid ReturnCode, Std_err, master_host, slave_statement = statement (Backupdir, Backup_host, IsMaster) if (ret Urncode = = 0): File = Backupdir if (ReturnCode! = 0): # exception Backup tagged as: date + _err Errdir = Backup Dir + "_err" Os.rename (Backupdir, errdir) file = errdir # Gets the backup size file_size = getdirsize (file) if (l En (Std_err) > 255): Std_err = std_err[:250] + "..." My_args = [IDC, Master_host, Backup_host, name, port, BA    Ckup_type, file, Start_time, Stop_time, ReturnCode, File_size, Slave_statement, Std_err] # Write to Database Call_proc (My_args) # Does the backup fail to be re-prepared?    Re-provision is allowed one time.   if (Is_retry = = True and ReturnCode! = 0):     Time.sleep (retry_sleep) oldfile = Scmd.split ('-O ') [1] pos = Oldfile.rfind ("/") + 1 # Get backup full path, backup File tags re-prepare the typeface retry_file = oldfile[:p os] + "rebackup-" + oldfile[pos:] Retrycmd = Scmd.replace (Oldfile, Retry_    File) # Re-prepare to start Mydumper (Retrycmd, Retry_file.strip (), name, host, Port, False, IsMaster) def getdirsize (path): # get backup folder size = 0L for root, dirs, files in Os.walk (path): size + = SUM ([GetSize (Join (root, name)) for n    Ame in Files]) return (size) def statement (path, Backup_host, IsMaster): "function: Read change master to information from metadata 1. Backup process: metadata.partial, after completion metadata.partial will have the same name as: metadata and write backup completion time 2.       Metadata 3 Segment: (1) Started Dump: Backup start time. (2) Master's Log-file and Log-pos information (must have); Slave host, Log-file, and Log-pos information (standby is slave only) (3) Finished dump: Backup end time 3. Returns the wrong code, master_host and change master to information ' path + = '/metadata '; Smetadata = ""; Master_host = ""; Er_code = 654321; Er_info = "%s NOT EXISTS!!!"% (path) if (os.path.exists (path)): if (ismaster! = 1): # Standby machine is slave        num = 3 Sfinds = "SLAVE status" Else:num = 2 Sfinds = "MASTER status" f = open (path, ' r ') rows = F.readlines (); i = 100; LST =[] for s in Rows:if (S.find (sfinds) > 0): i = 1; Continue if (i <= num): Lst.append (S.split (': ') [1].strip ()) i + = 1 if (        IsMaster = = 1): # Standby machine is master Master_host = Backup_host log_file, log_pos = LST;        ELSE: # standby machine is slave master_host, log_file, log_pos = LST; Er_code = 0 Er_info = "" "Smetadata =" Change MASTER to master_host= '%s ', master_log_file= '%s ', master_log_pos= %s,master_user= ' Rep_user ', master_password= ' meizu.com ' "% (Master_host, log_file, Log_pos) return (Er_code, Er_info, MA Ster_host, Smetadata) def execute (cmd): ' 1. Execute Shell command 2. Return execution information (ReturnCode = 0 execution succeeded, std_err error message) ' Try:returncode, Std_err = Commands.getstatusoutput (cmd) return (ReturnCode, std_err) except Os.error, E: # exception returned 1001 error R Eturn (1001, E) def call_proc (My_args): # Backup information written to database Try:conn = mydb.connect (host = ' 127.0.0.1 ', user = ' test ' , passwd = ' zxc/213? ', db = ' meizu_item ') cur = conn.cursor () cur.callproc (' sp_backup_i ', [my_args[0], My_ar GS[1], my_args[2], my_args[3], my_args[4], my_args[5], my_args[6], my_args[7], my_args[8], my_args[9], my_args[10], My_ ARGS[11], my_args[12]]) conn.commit () except MyDB. Error, E:pass # print "Mysql Error%d:%s"% (E.args[0], e.args[1]) finally:cur.close (); Conn.close () if __name__ = = ' __main__ ': Main ()

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL Backup tool mydumper Backup

Related Article

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.