Welcome 微信登录

首页 / 数据库 / MySQL / MySQL4.0做主从时主库的备份脚本

mysql4.0是老版本了,但是有些早期使用的企业依然在用,在创建主从时特别是线上服务器创建主从时,保证数据的一致性是个大问题:比如创建完从库同步时出现重复数据重复执行(虽然数据条数一致,但数据有可能会不一致)等。在mysql5.0以上版本中,此时备份主库只用在mysqldump时加上-F、master-data=2,single-transaction参数,从库同步时导入备份,在取备份文件开头的bin-log和pos位置进行同步即可,不会出现数据重复执行等问题,他能确保同步时的一致性。比较悲剧的是,本人所用的数据库还没有升级,是4.0的版本,经过测试,写了一个专一用于4.0主从同步时主库备份的脚本,原理就是模拟5.0以上的备份过程来做的。也可以用于5.0以上的版本,但是5.0以上的版本没有必要这么做。大家可以参考。
  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  4. import os,sys,time,MySQLdb 
  5. import subprocess,threading 
  6.  
  7. class mysql_dump(): 
  8.  
  9.     def __init__(self): 
  10.         self.dumpname = "/root/alldata%s.bz2" % time.strftime("%Y%m%d%H%M") 
  11.         self.STAT_IP = "192.168.0.39" 
  12.         self.logfile = "/root/mysql_dump.log" 
  13.         self.user = "root" 
  14.         self.passwd = "1q2w3e4r" 
  15.  
  16.     def log_w(self,text): 
  17.         now = time.strftime("%Y-%m-%d %H:%M:%S") 
  18.         tt = str(now) + " " + text + " " 
  19.         f = open(self.logfile,"a+") 
  20.         f.write(tt) 
  21.         f.close() 
  22.  
  23.     def dump(self): 
  24.         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) 
  25.         print time.strftime("%Y-%m-%d %H:%M:%S") 
  26.         text = "Start mysqldump,Please wait ..." 
  27.         print text 
  28.         self.log_w(text) 
  29.         a = subprocess.Popen(cmd,shell=True) 
  30.         while 1: 
  31.             b = subprocess.Popen.poll(a) 
  32.             if b == 0: 
  33.                 text = "Mysqldump complete" 
  34.                 print text 
  35.                 self.log_w(text) 
  36.                 break 
  37.             elif b is None: 
  38.                 print  "Mysqldump running" 
  39.                 time.sleep(30) 
  40.             else: 
  41.                 print a.pid,"term" 
  42.                 break 
  43.         self.rsync() 
  44.  
  45.     def rsync(self): 
  46.         cmd = "rsync -az %s %s::asktao_db/db_back/" % (self.dumpname,self.STAT_IP) 
  47.         text = "Start rsync to server(%s) ,Please wait ..." % self.STAT_IP 
  48.         print text 
  49.         self.log_w(text) 
  50.         a = subprocess.Popen(cmd,shell=True) 
  51.         while 1: 
  52.             b = subprocess.Popen.poll(a) 
  53.             if b == 0: 
  54.                 text = "Rsync complete" 
  55.                 print text 
  56.                 self.log_w(text) 
  57.                 break 
  58.             elif b is None: 
  59.                 print  "Rsync running" 
  60.                 time.sleep(30) 
  61.             else: 
  62.                 print a.pid,"term" 
  63.                 break 
  64.  
  65.     def lock(self): 
  66.         try: 
  67.             conn = MySQLdb.connect(host = "127.0.0.1",user = "root",passwd = "1q2w3e4r", charset="utf8", connect_timeout=5) 
  68.             cursor = conn.cursor() 
  69.             text = "flush tables with read lock" 
  70.             print text 
  71.             self.log_w(text) 
  72.             cursor.execute("flush tables with read lock") 
  73.             text = "flush logs" 
  74.             print text 
  75.             self.log_w(text) 
  76.             cursor.execute("flush logs") 
  77.             d = threading.Thread(target=self.dump, args=()) 
  78.             d.start() 
  79.             while 1: 
  80.                 if os.path.isfile(self.dumpname) and os.path.getsize(self.dumpname) > 0: 
  81.                     text = "UNLOCK TABLES" 
  82.                     print text 
  83.                     self.log_w(text) 
  84.                     cursor.execute("UNLOCK TABLES") 
  85.                     break 
  86.         except MySQLdb.Error,e: 
  87.             text = e.args 
  88.             print text 
  89.             self.log_w(text) 
  90.  
  91.     def work(self): 
  92.         t = threading.Thread(target=self.lock, args=()) 
  93.         t.start() 
  94.  
  95.  
  96. if __name__ == "__main__": 
  97.     boss = mysql_dump() 
  98.     boss.work() 
SecureCRT连接Linux显示MySQL记录中文乱码SQL server 警报类型相关资讯      MySQL主从同步 
  • Ubuntu下MySQL主从同步配置  (今 14:59)
  • MySQL主从同步校验与重新同步  (09月01日)
  • 监控MySQL主从同步延时  (08月03日)
  • MySQL生产库主从重新同步操作注意  (09月01日)
  • Linux下MySQL主从同步配置  (08月23日)
  • Linux下MySQL数据库主从同步配置  (03月11日)
本文评论 查看全部评论 (0)
表情: 姓名: 字数


评论声明
    版权所有©石家庄振强科技有限公司2024 冀ICP备08103738号-5 网站地图