自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL遭遇DELETE誤操作的回滾

數(shù)據(jù)庫 MySQL
操作數(shù)據(jù)庫時候難免會因為“大意”而誤操作,需要快速恢復(fù)的話通過備份來恢復(fù)是不太可能的,因為需要還原和binlog差來恢復(fù),等不了,很費時。這里先說明下因為Delete 操作的恢復(fù)方法:主要還是通過binlog來進行恢復(fù),前提是binlog_format必須是Row格式,否則只能通過備份來恢復(fù)數(shù)據(jù)了。

方法:

條件:開啟Binlog,F(xiàn)ormat為Row。

步驟:

1.通過MySQL自帶工具mysqlbinlog 指定導(dǎo)出操作的記錄: 

  1. mysqlbinlog  
  2. --no-defaults  
  3. --start-datetime='2012-12-25 14:56:00'  
  4. --stop-datetime='2012-12-25 14:57:00'  
  5. -vv mysql-bin.000001 > /home/zhoujy/restore/binlog.txt 

2.數(shù)據(jù)取出來之后,需要把數(shù)據(jù)解析反轉(zhuǎn),原始數(shù)據(jù):

  1. ### DELETE FROM test.me_info 
  2. ### WHERE 
  3. ###   @1=2165974 /* INT meta=0 nullable=0 is_null=0 */ 
  4. ###   @2='1984:03:17' /* DATE meta=0 nullable=1 is_null=0 */ 
  5. ###   @3=NULL /* DATE meta=765 nullable=1 is_null=1 */ 
  6. ###   @4=2012-10-25 00:00:00 /* DATETIME meta=0 nullable=0 is_null=0 */ 
  7. ###   @5='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */ 
  8. ###   @6=0 /* TINYINT meta=0 nullable=1 is_null=0 */ 
  9. ###   @7='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */ 
  10. ###   @8=-1 (4294967295) /* INT meta=0 nullable=1 is_null=0 */ 
  11. ###   @9=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */ 
  12. ###   @10=NULL /* MEDIUMINT meta=0 nullable=1 is_null=1 */ 
  13. ###   @11=2 /* TINYINT meta=0 nullable=1 is_null=0 */ 
  14. ###   @12=0 /* TINYINT meta=0 nullable=1 is_null=0 */ 
  15. ###   @13='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */ 
  16. ###   @14='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */ 
  17. ###   @15=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */ 
  18. ###   @16=320 /* INT meta=0 nullable=1 is_null=0 */ 
  19. …………………… 
  20. …………………… 
  21. …………………… 

Row格式的binlog記錄的格式如上面所示,需要做的工作就是吧Delete的操作轉(zhuǎn)換成Insert操作,發(fā)上面的都是有一定規(guī)律的,并且需要注意的是:

1、字段類型 DATETIME 日期。在日志中保存的格式為 @4=2012-10-25 00:00:00,需要將2012-10-25 00:00:00加上引號。

2、負數(shù)。在日志中保存的格式為 @1=-1 (4294967295), -2(4294967294),-3(4294967293),需要將()里面的數(shù)據(jù)去掉,只保留@1=-1。

3、轉(zhuǎn)義字符集。如:'s,\,等。

上面3點清楚之后,可以寫一個腳本(水平有限,在提升中,寫的不好看):

  1. #!/bin/env python 
  2. # -*- encoding: utf-8 -*- 
  3. #------------------------------------------------------------------------------- 
  4. Name:        restore.py 
  5. # Purpose:     通過Binlog恢復(fù)Delete誤操作數(shù)據(jù) 
  6. # Author:      zhoujy 
  7. # Created:     2012-12-25 
  8. update:      2012-12-25 
  9. # Copyright:   (c) Mablevi 2012 
  10. # Licence:     zjy 
  11. #------------------------------------------------------------------------------- 
  12. def read_binlog(file,column_num): 
  13.     f=open(file) 
  14.     num = '@'+str(column_num) 
  15.     while True
  16.         lines = f.readline() 
  17.         if lines.strip()[0:3] == '###'
  18.             lines=lines.split(' ',3) 
  19.             if lines[1] == 'DELETE' and lines[2] =='FROM':           #該部分替換DeleteInsert 
  20.                 lines[1] = "INSERT" 
  21.                 lines[2] = 'INTO' 
  22.                 lines[-1] = lines[-1].strip() 
  23.             if lines[1].strip() == 'WHERE'
  24.                 lines[1] = 'VALUES (' 
  25.             if  ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] <> num:          #num為列數(shù),要是小于***的列數(shù),后面均加, 
  26.                 lines[3] = lines[3].split('=',1)[-1].strip() 
  27.                 if lines[3].strip('\'').strip().find('\'') <> -1: 
  28.                     lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')  #這里過濾掉轉(zhuǎn)義的字符串 
  29.                     lines[3] = '\'' + lines[3] + '\',' 
  30.                 elif lines[3].find('INT meta') <> -1:                #過濾Int類型的字段為負數(shù)后帶的(),正數(shù)不受影響 
  31.                     lines[3] = lines[3].split('/*')[0].strip() 
  32.                     lines[3] = lines[3].split()[0] + ',' 
  33.                 elif lines[3].find('NULL') <> -1: 
  34.                     lines[3] = lines[3].split('/*')[0].strip() 
  35.                     lines[3] = lines[3] + ',' 
  36.                 else
  37.                     lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')  #這里過濾掉轉(zhuǎn)義的字符串 
  38.                     lines[3] = '\'' + lines[3].strip('\''' ') + '\',' 
  39.             if  ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] == num:          #num為列數(shù),要是小于***的列數(shù),后面均加); 
  40.                 lines[3] = lines[3].split('=',1)[-1].strip() 
  41.                 if lines[3].find('\'') <> -1:  
  42.                     lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')  #同上 
  43.                     lines[3] = '\'' + lines[3] + '\');' 
  44.                 elif lines[3].find('INT meta') <> -1:                #同上 
  45.                     lines[3] = lines[3].split('/*')[0].strip() 
  46.                     lines[3] = lines[3].split(' ')[0] + ');' 
  47.                 elif lines[3].find('NULL') <> -1: 
  48.                     lines[3] = lines[3].split('/*')[0].strip() 
  49.                     lines[3] = lines[3] + ');' 
  50.                 else
  51.                     lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')  #同上 
  52.                     lines[3] = '\'' + lines[3].strip('\''' ') + '\');' 
  53.             print ' '.join(lines[1:]) 
  54.         if lines == ''
  55.             break 
  56. if __name__ == '__main__'
  57.     import sys 
  58.     read_binlog(sys.argv[1],sys.argv[2]) 

執(zhí)行腳本:

python restore.py binlog.txt 36 > binlog.sql

命令行中的36 表示 需要還原的表的字段有36個,效果:

  1. INSERT INTO test.me_info 
  2. VALUES ( 
  3.   2123269, 
  4.   '1990:11:12'
  5.   NULL
  6.   2, 
  7.   ''
  8.   0, 
  9.   ''
  10.   -1, 
  11.   0, 
  12.   340800, 
  13.   1, 
  14.   0, 
  15.   ''
  16. …… 
  17. …… 
  18.   1, 
  19.   NULL 
  20. ); 

***還原:

mysql test < binlog.sql

總結(jié):

下次整理Row和STATEMENT的優(yōu)劣。

原文鏈接:http://www.cnblogs.com/zhoujinyi/archive/2012/12/25/2832543.html

【編輯推薦】

 

  1. 維基逃離MySQL 力挺開源數(shù)據(jù)庫
  2. MariaDB 2周年了
  3. MySQL的四種不同查詢的分析
  4. MySQL的四種不同查詢的分析
  5. MySQL內(nèi)存表的特性與使用介紹

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2024-02-20 09:54:20

MySQL數(shù)據(jù)庫

2018-03-26 14:05:56

MySQLbinlog2sql誤操作

2020-10-16 18:41:43

command設(shè)計模式代碼

2017-05-31 16:10:45

MySQL誤操作恢復(fù)數(shù)據(jù)

2009-02-02 10:26:39

谷歌搜索故障

2010-05-27 17:35:36

MYSQL DELET

2009-11-16 17:15:12

Oracle減少回滾段

2020-08-10 07:52:30

MySQL數(shù)據(jù)庫

2009-11-16 13:41:18

Oracle分離回滾段

2009-07-20 18:11:52

iBATIS事務(wù)Spring

2017-05-18 16:07:23

回滾數(shù)據(jù)庫代碼

2010-04-16 17:31:22

ORACLE回滾段

2021-10-12 06:56:05

MYSQLDeleteDrop

2013-11-12 14:43:43

MySQL數(shù)據(jù)庫

2018-01-18 09:34:27

LinuxCentOSYUM

2011-07-29 16:21:21

Oracle數(shù)據(jù)庫回滾段

2022-10-12 08:01:08

MySQL日志數(shù)據(jù)庫

2017-06-07 19:18:56

Oracle實例恢復(fù)前滾和回滾

2010-05-10 17:46:21

Oracle數(shù)據(jù)庫

2022-06-15 08:26:23

數(shù)據(jù)庫MySQL
點贊
收藏

51CTO技術(shù)棧公眾號