Coder Social home page Coder Social logo

binlogdecode's Introduction

binlogdecode

基于row模式的binlog,生成binlog的正向解析内容和回滚内容

1.binlog_rollback
2.binlog_explain

1.binlog_rollback.pl

基于row模式的binlog,生成DML(insert/update/delete)的rollback语句
通过mysqlbinlog -v 解析binlog生成可读的sql文件
提取需要处理的有效sql

  • "### "开头的行.如果输入的start-position位于某个event group中间,则会导致"无法识别event"错误

*将INSERT/UPDATE/DELETE 的sql反转,并且1个完整sql只能占1行
* INSERT: INSERT INTO => DELETE FROM, SET => WHERE
* UPDATE: WHERE => SET, SET => WHERE
* DELETE: DELETE FROM => INSERT INTO, WHERE => SET
* 用列名替换位置@{1,2,3}
* 通过desc table获得列顺序及对应的列名
* 特殊列类型value做特别处理
* 逆序

注意:

  • 表结构与现在的表结构必须相同[谨记]

* 由于row模式是幂等的,并且恢复是一次性,所以只提取sql,不提取BEGIN/COMMIT
* 只能对INSERT/UPDATE/DELETE进行处理
  使用方法
  
  Command line options :
    --help                   # OUT : print help info   
    -f, --srcfile            # IN  : binlog file. [required]
    -o, --outfile            # OUT : output sql file. [required]
    -h, --host               # IN  : host. default '127.0.0.1'
    -u, --user               # IN  : user. [required]
    -p, --password           # IN  : password. [required] 
    -P, --port               # IN  : port. default '3306'
    --start-datetime         # IN  : start datetime
    --stop-datetime          # IN  : stop datetime
    --start-position         # IN  : start position
    --stop-position          # IN  : stop position
    -d, --database           # IN  : database, split comma
    -T, --table              # IN  : table, split comma. [required] set -d
    -i, --ignore             # IN  : ignore binlog check contain DDL(CREATE|ALTER|DROP|RENAME)
    --debug                  # IN  : print debug information

Sample :
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' 
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' -i
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --debug
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -h '192.168.1.2' -u 'user' -p 'pwd' -P 3307
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107 --stop-position=10000
   shell> perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' -d 'db1,db2'
   shell> perl binlog-rollback.pl -f 'mysql-bin.0000*' -o '/tmp/t.sql' -u 'user' -p 'pwd' -d 'db1,db2' -T 'tb1,tb2'
   

2.binlog_explain.pl

基于row模式的binlog,生成DML(insert/update/delete)的正向语句
通过mysqlbinlog -v 解析binlog生成可读的sql文件
提取需要处理的有效sql

  • "### "开头的行.如果输入的start-position位于某个event group中间,则会导致"无法识别event"错误

将INSERT/UPDATE/DELETE 的sql反转,并且1个完整sql只能占1行
* INSERT: INSERT INTO => INSERT INTO, SET => SET
* UPDATE: WHERE => WHERE, SET => SET,需要将 WHERE 部分追加到最后
* DELETE: DELETE FROM => DELETE FROM, WHERE => WHERE
* 用列名替换位置@{1,2,3}/
* 通过desc table获得列顺序及对应的列名
* 特殊列类型value做特别处理


注意:

  • 表结构与现在的表结构必须相同[谨记]
    

  • 由于row模式是幂等的,并且恢复是一次性,所以只提取sql,不提取BEGIN/COMMIT
    

  • 只能对INSERT/UPDATE/DELETE进行处理
    

     
     Command line options :
    --help                   # OUT : print help info   
    -f, --srcfile            # IN  : binlog file. [required]
    -o, --outfile            # OUT : output sql file. [required]
    -h, --host               # IN  : host. default '127.0.0.1'
    -u, --user               # IN  : user. [required]
    -p, --password           # IN  : password. [required] 
    -P, --port               # IN  : port. default '3306'
    --start-datetime         # IN  : start datetime
    --stop-datetime          # IN  : stop datetime
    --start-position         # IN  : start position
    --stop-position          # IN  : stop position
    -d, --database           # IN  : database, split comma
    -T, --table              # IN  : table, split comma. [required] set -d
    -i, --ignore             # IN  : ignore binlog check contain DDL(CREATE|ALTER|DROP|RENAME)
    --debug                  # IN  : print debug information

Sample :
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' 
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' -i
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --debug
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -h '192.168.1.2' -u 'user' -p 'pwd' -P 3307
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107 --stop-position=10000
   shell> perl binlog-explain.pl -f 'mysql-bin.000001' -o '/tmp/t.sql' -u 'user' -p 'pwd' -d 'db1,db2'
   shell> perl binlog-explain.pl -f 'mysql-bin.0000*' -o '/tmp/t.sql' -u 'user' -p 'pwd' -d 'db1,db2' -T 'tb1,tb2'
   
   

binlogdecode's People

Contributors

acmcee avatar

Stargazers

Xiaotao avatar  avatar  avatar lizhi avatar

Watchers

 avatar

binlogdecode's Issues

关于INSERT 语句类型问题

你好,binlog_explain.pl解析正向INSERT 语句的类型是INSERT INRO SET,能不能换成INSERT INTO tbl_name () VALUES(); 这种类型的呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.