Backup script of the master database when mysql4.0 is used as the master database

Source: Internet
Author: User


Mysql4.0 is the backup script for the master database when the master database is started. mysql4.0 is an old version, but some enterprises that are used earlier are still using it. When creating a Master/Slave instance, especially when an online server creates a Master/Slave instance, ensuring data consistency is a big problem: for example, repeated Data Execution occurs when the slave database is created for synchronization (although the number of data entries is consistent, the data may be inconsistent. In mysql5.0 and later versions, the backup master database only needs to add the-F, master-data = 2, single-transaction parameters to mysqldump to import the backup data during Database Synchronization, you can synchronize the bin-log and pos locations starting with the backup file without repeated Data Execution. This ensures consistency during synchronization. The tragedy is that my database has not been upgraded. It is in MySQL 4.0. After testing, I wrote a script dedicated to the backup of the master database during master-slave synchronization of MySQL 4.0, the principle is to simulate a backup process of more than 5.0. It can also be used for Versions later than 5.0, but it is not necessary for Versions later than 5.0. For more information, see. #! /Usr/bin/env python #-*-coding: UTF-8-*-www.2cto.com import OS, sys, time, MySQLdb import subprocess, threading class mysql_dump (): def _ init _ (self): self. dumpname = "/root/alldata‑s.bz2" % time. strftime ("% Y % m % d % H % M") self. STAT_IP = "192.168.0.39" self. logfile = "/root/mysql_dump.log" self. user = "root" self. passwd = "1q2w3e4r" def log_w (self, text): now = time. strftime ("% Y-% m-% d % H: % M: % S") tt = str (now) + "\ t" + text + "\ n" f = open (self. logfile, 'a + ') f. write (tt) f. close () def dump (self ): cmd = "/usr/local/mysql/bin/mysqldump-A-Q-e -- add-drop-table -- add-locks -- extended-insert -- quick -- no-autocommit -- single-transaction-u % s-p % s | bzip2-2> % s "% (self. user, self. passwd, self. dumpname) print time. strftime ("% Y-% m-% d % H: % M: % S") text = "Start mysqldump, Please wait... "print text www.2cto.com self. log_w (text) a = subprocess. popen (cmd, shell = True) while 1: B = subprocess. popen. poll (a) if B = 0: text = "Mysqldump complete" print text self. log_w (text) break elif B is None: print 'mysqldump running' time. sleep (30) else: print. pid, 'condition' break self. rsync () def rsync (self): cmd = "rsync-az % s: asktao_db/db_back/" % (self. dumpname, self. STAT_IP) text = "Start rsync to server (% s), Please wait... "% self. STAT_IP print text self. log_w (text) a = subprocess. popen (cmd, shell = True) while 1: B = subprocess. popen. poll (a) if B = 0: text = "Rsync complete" print text www.2cto.com self. log_w (text) break elif B is None: print 'rsync running' time. sleep (30) else: print. pid, 'condition' break def lock (self): try: conn = MySQLdb. connect (host = '2017. 0.0.1 ', user = 'root', passwd = '1q2w3e4r', charset = 'utf8', connect_timeout = 5) cursor = conn. cursor () text = "flush tables with read lock" print text www.2cto.com self. log_w (text) cursor.exe cute ("flush tables with read lock") text = "flush logs" print text self. log_w (text) cursor.exe cute ("flush logs") d = threading. thread (target = self. dump, args = () d. start () while 1: if OS. path. isfile (self. dumpname) and OS. path. getsize (self. dumpname)> 0: text = "unlock tables" print text self. log_w (text) cursor.exe cute ("unlock tables") break cannot be MySQLdb. error, e: text = e. args print text self. log_w (text) def work (self): t = threading. thread (target = self. lock, args = () t. start () if _ name _ = "_ main _": boss = mysql_dump () boss. work ()

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.