Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1366034
  • 博文数量: 243
  • 博客积分: 888
  • 博客等级: 准尉
  • 技术积分: 2955
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-05 14:33
个人简介

漫漫长路,其修远兮!

文章分类

全部博文(243)

文章存档

2017年(2)

2016年(22)

2015年(32)

2014年(57)

2013年(107)

2012年(23)

分类: Mysql/postgreSQL

2016-09-03 08:38:35

基于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进行处理

mysql> select * from yoon;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|        1 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        2 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        3 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        4 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        5 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        6 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        7 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        8 | HANK       | YOON      | 2006-02-15 04:34:33 |
|        9 | HANK       | YOON      | 2006-02-15 04:34:33 |
|       10 | HANK       | YOON      | 2006-02-15 04:34:33 |
|       11 | HANK       | YOON      | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
11 rows in set (0.00 sec)


mysql> delete from yoon;
Query OK, 11 rows affected (1.03 sec)


mysql> select * from yoon;
Empty set (0.00 sec)

命令之间的空格一定要注意,否则就会无法提取SQL语句:
[root@hank-yoon data]# perl binlog-rollback.pl -f 'mysql-bin.000001' -o '/export/data/mysql/data/yoon.sql' -u 'root' -p 'yoon'
Warning: Using a password on the command line interface can be insecure.
[root@hank-yoon data]# ls
auto.cnf            hank     ibdata2      ib_logfile1  modify.pl  mysql-bin.000001  performance_schema  test  yoon.sql
binlog-rollback.pl  ibdata1  ib_logfile0  ib_logfile2  mysql      mysql-bin.index   sakila              yoon
[root@hank-yoon data]# cat yoon.sql 
INSERT INTO `yoon`.`yoon` SET `actor_id`=11, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=10, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=9, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=8, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=7, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=6, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=5, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=4, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=3, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=2, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
INSERT INTO `yoon`.`yoon` SET `actor_id`=1, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);

mysql> INSERT INTO `yoon`.`yoon` SET `actor_id`=11, `first_name`='HANK', `last_name`='YOON', `last_update`=from_unixtime(1139949273);
Query OK, 1 row affected (0.01 sec)


mysql> select * from yoon;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|       11 | HANK       | YOON      | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+



  1. #!/usr/lib/perl -w

  2. use strict;
  3. use warnings;

  4. use Class::Struct;
  5. use Getopt::Long qw(:config no_ignore_case);                    # GetOption
  6. # register handler system signals
  7. use sigtrap 'handler', \&sig_int, 'normal-signals';

  8. # catch signal
  9. sub sig_int(){
  10.     my ($signals) = @_;
  11.     print STDERR "# Caught SIG$signals.\n";
  12.     exit 1;
  13. }

  14. my %opt;
  15. my $srcfile;
  16. my $host = '127.0.0.1';
  17. my $port = 3306;
  18. my ($user,$pwd);
  19. my ($MYSQL, $MYSQLBINLOG, $ROLLBACK_DML);
  20. my $outfile = '/dev/null';
  21. my (%do_dbs,%do_tbs);

  22. # tbname=>tbcol, tbcol: @n=>colname,type
  23. my %tbcol_pos;

  24. my $SPLITER_COL = ',';
  25. my $SQLTYPE_IST = 'INSERT';
  26. my $SQLTYPE_UPD = 'UPDATE';
  27. my $SQLTYPE_DEL = 'DELETE';
  28. my $SQLAREA_WHERE = 'WHERE';
  29. my $SQLAREA_SET = 'SET';

  30. my $PRE_FUNCT = '========================== ';

  31. =========================================================
  32. # 基于row模式的binlog,生成DML(insert/update/delete)的rollback语句
  33. # 通过mysqlbinlog -v 解析binlog生成可读的sql文件
  34. # 提取需要处理的有效sql
  35. #     "### "开头的行.如果输入的start-position位于某个event group中间,则会导致"无法识别event"错误
  36. #
  37. # 将INSERT/UPDATE/DELETE 的sql反转,并且1个完整sql只能占1行
  38. #     INSERT: INSERT INTO => DELETE FROM, SET => WHERE
  39. #     UPDATE: WHERE => SET, SET => WHERE
  40. #     DELETE: DELETE FROM => INSERT INTO, WHERE => SET
  41. # 用列名替换位置@{1,2,3}
  42. #     通过desc table获得列顺序及对应的列名
  43. #     特殊列类型value做特别处理
  44. # 逆序

  45. # 注意:
  46. #     表结构与现在的表结构必须相同[谨记]
  47. #     由于row模式是幂等的,并且恢复是一次性,所以只提取sql,不提取BEGIN/COMMIT
  48. #     只能对INSERT/UPDATE/DELETE进行处理
  49. ========================================================
  50. sub main{

  51.     # get input option
  52.     &get_options();

  53.     # 
  54.     &init_tbcol();

  55.     #
  56.     &do_binlog_rollback();
  57. }

  58. &main();


  59. ----------------------------------------------------------------------------------------
  60. # Func : get options and set option flag 
  61. ----------------------------------------------------------------------------------------
  62. sub get_options{
  63.     #Get options info
  64.     GetOptions(\%opt,
  65.         'help',                    # OUT : print help info 
  66.         'f|srcfile=s',            # IN : binlog file
  67.         'o|outfile=s',            # out : output sql file
  68.         'h|host=s',                # IN : host
  69.         'u|user=s', # IN : user
  70.         'p|password=s', # IN : password
  71.         'P|port=i',                # IN : port
  72.         'start-datetime=s',        # IN : start datetime
  73.         'stop-datetime=s',        # IN : stop datetime
  74.         'start-position=i',        # IN : start position
  75.         'stop-position=i',        # IN : stop position
  76.         'd|database=s',            # IN : database, split comma
  77.         'T|table=s',            # IN : table, split comma
  78.         'i|ignore',                # IN : ignore binlog check ddl and so on
  79.         'debug',                # IN : print debug information
  80.      ) or print_usage();

  81.     if (!scalar(%opt)) {
  82.         &print_usage();
  83.     }

  84.     # Handle for options
  85.     if ($opt{'f'}){
  86.         $srcfile = $opt{'f'};
  87.     }else{
  88.         &merror("please input binlog file");
  89.     }

  90.     $opt{'h'} and $host = $opt{'h'};
  91.     $opt{'u'} and $user = $opt{'u'};
  92.     $opt{'p'} and $pwd = $opt{'p'};
  93.     $opt{'P'} and $port = $opt{'P'};
  94.     if ($opt{'o'}) {
  95.         $outfile = $opt{'o'};
  96.         # 清空 outfile
  97.         `echo '' > $outfile`;
  98.     }

  99.     # 
  100.     $MYSQL = qq{mysql -h$host -u$user -p'$pwd' -P$port};
  101.     &mdebug("get_options::MYSQL\n\t$MYSQL");

  102.     # 提取binlog,不需要显示列定义信息,用-v,而不用-vv
  103.     $MYSQLBINLOG = qq{mysqlbinlog -v};
  104.     $MYSQLBINLOG .= " --start-position=".$opt{'start-position'} if $opt{'start-position'};
  105.     $MYSQLBINLOG .= " --stop-position=".$opt{'stop-position'} if $opt{'stop-postion'};
  106.     $MYSQLBINLOG .= " --start-datetime='".$opt{'start-datetime'}."'" if $opt{'start-datetime'};
  107.     $MYSQLBINLOG .= " --stop-datetime='$opt{'stop-datetime'}'" if $opt{'stop-datetime'};
  108.     $MYSQLBINLOG .= " $srcfile";
  109.     &mdebug("get_options::MYSQLBINLOG\n\t$MYSQLBINLOG");

  110.     # 检查binlog中是否含有 ddl sql: CREATE|ALTER|DROP|RENAME
  111.     &check_binlog() unless ($opt{'i'});

  112.     # 不使用mysqlbinlog过滤,USE dbname;方式可能会漏掉某些sql,所以不在mysqlbinlog过滤
  113.     # 指定数据库
  114.     if ($opt{'d'}){
  115.         my @dbs = split(/,/,$opt{'d'});
  116.         foreach my $db (@dbs){
  117.             $do_dbs{$db}=1;
  118.         }
  119.     }

  120.     # 指定表
  121.     if ($opt{'T'}){
  122.         my @tbs = split(/,/,$opt{'T'});
  123.         foreach my $tb (@tbs){
  124.             $do_tbs{$tb}=1;
  125.         }
  126.     }

  127.     # 提取有效DML SQL
  128.     $ROLLBACK_DML = $MYSQLBINLOG." | grep '^### '";
  129.     # 去掉注释: '### ' -> ''
  130.     # 删除首尾空格
  131.     $ROLLBACK_DML .= " | sed 's/###\\s*//g;s/\\s*\$//g'";
  132.     &mdebug("rollback dml\n\t$ROLLBACK_DML");
  133.     
  134.     # 检查内容是否为空
  135.     my $cmd = "$ROLLBACK_DML | wc -l";
  136.     &mdebug("check contain dml sql\n\t$cmd");
  137.     my $size = `$cmd`;
  138.     chomp($size);
  139.     unless ($size >0){
  140.         &merror("binlog DML is empty:$ROLLBACK_DML");
  141.     };

  142. }    


  143. ----------------------------------------------------------------------------------------
  144. # Func : check binlog contain DDL
  145. ----------------------------------------------------------------------------------------
  146. sub check_binlog{
  147.     &mdebug("$PRE_FUNCT check_binlog");
  148.     my $cmd = "$MYSQLBINLOG ";
  149.     $cmd .= " | grep -E -i '^(CREATE|ALTER|DROP|RENAME)' ";
  150.     &mdebug("check binlog has DDL cmd\n\t$cmd");
  151.     my $ddlcnt = `$cmd`;
  152.     chomp($ddlcnt);

  153.     my $ddlnum = `$cmd | wc -l`;
  154.     chomp($ddlnum);
  155.     my $res = 0;
  156.     if ($ddlnum>0){
  157.         # 在ddl sql前面加上前缀<DDL>
  158.         $ddlcnt = `echo '$ddlcnt' | sed 's/^//g'`;
  159.         &merror("binlog contain $ddlnum DDL:$MYSQLBINLOG. ddl sql:\n$ddlcnt");
  160.     }

  161.     return $res;
  162. }


  163. ----------------------------------------------------------------------------------------
  164. # Func : init all table column order
  165. #        if input --database --table params, only get set table column order
  166. ----------------------------------------------------------------------------------------
  167. sub init_tbcol{
  168.     &mdebug("$PRE_FUNCT init_tbcol");
  169.     # 提取DML语句
  170.     my $cmd .= "$ROLLBACK_DML | grep -E '^(INSERT|UPDATE|DELETE)'";
  171.     # 提取表名,并去重
  172.     #$cmd .= " | awk '{if (\$1 ~ \"^UPDATE\") {print \$2}else {print \$3}}' | uniq ";
  173.     $cmd .= " | awk '{if (\$1 ~ \"^UPDATE\") {print \$2}else {print \$3}}' | sort | uniq ";
  174.     &mdebug("get table name cmd\n\t$cmd");
  175.     open ALLTABLE, "$cmd | " or die "can't open file:$cmd\n";

  176.     while (my $tbname = <ALLTABLE>){
  177.         chomp($tbname);
  178.         #if (exists $tbcol_pos{$tbname}){
  179.         #    next;
  180.         #}
  181.         &init_one_tbcol($tbname) unless (&ignore_tb($tbname));
  182.         
  183.     }
  184.     close ALLTABLE or die "can't close file:$cmd\n";

  185.     # init tb col
  186.     foreach my $tb (keys %tbcol_pos){
  187.         &mdebug("tbname->$tb");
  188.         my %colpos = %{$tbcol_pos{$tb}};
  189.         foreach my $pos (keys %colpos){
  190.             my $col = $colpos{$pos};
  191.             my ($cname,$ctype) = split(/$SPLITER_COL/, $col);
  192.             &mdebug("\tpos->$pos,cname->$cname,ctype->$ctype");
  193.         }
  194.     }
  195. };


  196. ----------------------------------------------------------------------------------------
  197. # Func : init one table column order
  198. ----------------------------------------------------------------------------------------
  199. sub init_one_tbcol{
  200.     my $tbname = shift;
  201.     &mdebug("$PRE_FUNCT init_one_tbcol");
  202.     # 获取表结构及列顺序
  203.     my $cmd = $MYSQL." --skip-column-names --silent -e 'desc $tbname'";
  204.     # 提取列名,并拼接
  205.     $cmd .= " | awk -F\'\\t\' \'{print NR\"$SPLITER_COL`\"\$1\"`$SPLITER_COL\"\$2}'";
  206.     &mdebug("get table column infor cmd\n\t$cmd");
  207.     open TBCOL,"$cmd | " or die "can't open desc $tbname;";

  208.     my %colpos;
  209.     while (my $line = <TBCOL>){
  210.         chomp($line);
  211.         my ($pos,$col,$coltype) = split(/$SPLITER_COL/,$line);
  212.         &mdebug("linesss=$line\n\t\tpos=$pos\n\t\tcol=$col\n\t\ttype=$coltype");
  213.         $colpos{$pos} = $col.$SPLITER_COL.$coltype;
  214.     }
  215.     close TBCOL or die "can't colse desc $tbname";

  216.     $tbcol_pos{$tbname} = \%colpos;
  217. }


  218. ----------------------------------------------------------------------------------------
  219. # Func : rollback sql:    INSERT/UPDATE/DELETE
  220. ----------------------------------------------------------------------------------------
  221. sub do_binlog_rollback{
  222.     my $binlogfile = "$ROLLBACK_DML ";
  223.     &mdebug("$PRE_FUNCT do_binlog_rollback");

  224.     # INSERT|UPDATE|DELETE
  225.     my $sqltype;
  226.     # WHERE|SET
  227.     my $sqlarea;
  228.     
  229.     my ($tbname, $sqlstr) = ('', '');
  230.     my ($notignore, $isareabegin) = (0,0);

  231.     # output sql file
  232.     open SQLFILE, ">> $outfile" or die "Can't open sql file:$outfile";

  233.     # binlog file
  234.     open BINLOG, "$binlogfile |" or die "Can't open file: $binlogfile";
  235.     while (my $line = <BINLOG>){
  236.         chomp($line);
  237.         if ($line =~ /^(INSERT|UPDATE|DELETE)/){
  238.             # export sql
  239.             if ($sqlstr ne ''){
  240.                 $sqlstr .= ";\n";
  241.                 print SQLFILE $sqlstr;
  242.                 &mdebug("export sql\n\t".$sqlstr);
  243.                 $sqlstr = '';
  244.             }

  245.             if ($line =~ /^INSERT/){
  246.                 $sqltype = $SQLTYPE_IST;
  247.                 $tbname = `echo '$line' | awk '{print \$3}'`;
  248.                 chomp($tbname);
  249.                 $sqlstr = qq{DELETE FROM $tbname};
  250.             }elsif ($line =~ /^UPDATE/){
  251.                 $sqltype = $SQLTYPE_UPD;
  252.                 $tbname = `echo '$line' | awk '{print \$2}'`;
  253.                 chomp($tbname);
  254.                 $sqlstr = qq{UPDATE $tbname};
  255.             }elsif ($line =~ /^DELETE/){
  256.                 $sqltype = $SQLTYPE_DEL;    
  257.                 $tbname = `echo '$line' | awk '{print \$3}'`;
  258.                 chomp($tbname);
  259.                 $sqlstr = qq{INSERT INTO $tbname};
  260.             }

  261.             # check ignore table
  262.             if(&ignore_tb($tbname)){
  263.                 $notignore = 0;
  264.                 &mdebug("#IGNORE#:line:".$line);
  265.                 $sqlstr = '';
  266.             }else{
  267.                 $notignore = 1;
  268.                 &mdebug("#DO#:line:".$line);
  269.             }
  270.         }else {
  271.             if($notignore){
  272.                 &merror("can't get tbname") unless (defined($tbname));
  273.                 if ($line =~ /^WHERE/){
  274.                     $sqlarea = $SQLAREA_WHERE;
  275.                     $sqlstr .= qq{ SET};
  276.                     $isareabegin = 1;
  277.                 }elsif ($line =~ /^SET/){
  278.                     $sqlarea = $SQLAREA_SET;
  279.                     $sqlstr .= qq{ WHERE};
  280.                     $isareabegin = 1;
  281.                 }elsif ($line =~ /^\@/){
  282.                     $sqlstr .= &deal_col_value($tbname, $sqltype, $sqlarea, $isareabegin, $line);
  283.                     $isareabegin = 0;
  284.                 }else{
  285.                     &mdebug("::unknown sql:".$line);
  286.                 }
  287.             }
  288.         }
  289.     }
  290.     # export last sql
  291.     if ($sqlstr ne ''){
  292.         $sqlstr .= ";\n";
  293.         print SQLFILE $sqlstr;
  294.         &mdebug("export sql\n\t".$sqlstr);
  295.     }
  296.     
  297.     close BINLOG or die "Can't close binlog file: $binlogfile";

  298.     close SQLFILE or die "Can't close out sql file: $outfile";

  299.     # 逆序
  300.     # 1!G: 只有第一行不执行G, 将hold space中的内容append回到pattern space
  301.     # h: 将pattern space 拷贝到hold space
  302.     # $!d: 除最后一行都删除
  303.     my $invert = "sed -i '1!G;h;\$!d' $outfile";
  304.     my $res = `$invert`;
  305.     &mdebug("inverter order sqlfile :$invert");
  306. }

  307. ----------------------------------------------------------------------------------------
  308. # Func : transfer column pos to name
  309. #    deal column value
  310. #
  311. &deal_col_value($tbname, $sqltype, $sqlarea, $isareabegin, $line);
  312. ----------------------------------------------------------------------------------------
  313. sub deal_col_value($$$$$){
  314.     my ($tbname, $sqltype, $sqlarea, $isareabegin, $line) = @_;
  315.     &mdebug("$PRE_FUNCT deal_col_value");
  316.     &mdebug("input:tbname->$tbname,type->$sqltype,area->$sqlarea,areabegin->$isareabegin,line->$line");
  317.     my @vals = split(/=/, $line);
  318.     my $pos = substr($vals[0],1);
  319.     my $valstartpos = length($pos)+2;
  320.     my $val = substr($line,$valstartpos);
  321.     my %tbcol = %{$tbcol_pos{$tbname}};
  322.     my ($cname,$ctype) = split(/$SPLITER_COL/,$tbcol{$pos});
  323.     &merror("can't get $tbname column $cname type") unless (defined($cname) || defined($ctype));
  324.     &mdebug("column infor:cname->$cname,type->$ctype");

  325.     # join str
  326.     my $joinstr;
  327.     if ($isareabegin){
  328.         $joinstr = ' ';
  329.     }else{
  330.         # WHERE 被替换为 SET, 使用 , 连接
  331.         if ($sqlarea eq $SQLAREA_WHERE){
  332.             $joinstr = ', ';
  333.         # SET 被替换为 WHERE 使用 AND 连接
  334.         }elsif ($sqlarea eq $SQLAREA_SET){
  335.             $joinstr = ' AND ';
  336.         }else{
  337.             &merror("!!!!!!The scripts error");
  338.         }
  339.     }
  340.     
  341.     # 
  342.     my $newline = $joinstr;

  343.     # NULL value
  344.     if (($val eq 'NULL') && ($sqlarea eq $SQLAREA_SET)){
  345.         $newline .= qq{ $cname IS NULL};
  346.     }else{
  347.         # timestamp: record seconds
  348.         if ($ctype eq 'timestamp'){
  349.             $newline .= qq{$cname=from_unixtime($val)};
  350.         # datetime: @n=yyyy-mm-dd hh::ii::ss
  351.         }elsif ($ctype eq 'datetime'){
  352.             $newline .= qq{$cname='$val'};
  353.         }else{
  354.             $newline .= qq{$cname=$val};
  355.         }
  356.     }
  357.     &mdebug("\told>$line\n\tnew>$newline");
  358.     
  359.     return $newline;
  360. }

  361. ----------------------------------------------------------------------------------------
  362. # Func : check is ignore table
  363. # params: IN table full name # format:`dbname`.`tbname`
  364. # RETURN:
  365. #        0 not ignore
  366. #        1 ignore
  367. ----------------------------------------------------------------------------------------
  368. sub ignore_tb($){
  369.     my $fullname = shift;
  370.     # 删除`
  371.     $fullname =~ s/`//g;
  372.     my ($dbname,$tbname) = split(/\./,$fullname);
  373.     my $res = 0;
  374.     
  375.     # 指定了数据库
  376.     if ($opt{'d'}){
  377.         # 与指定库相同
  378.         if ($do_dbs{$dbname}){
  379.             # 指定表
  380.             if ($opt{'T'}){
  381.                 # 与指定表不同
  382.                 unless ($do_tbs{$tbname}){
  383.                     $res = 1;
  384.                 }
  385.             }
  386.         # 与指定库不同
  387.         }else{
  388.             $res = 1;
  389.         }
  390.     }
  391.     #&mdebug("Table check ignore:$fullname->$res");
  392.     return $res;
  393. }


  394. ----------------------------------------------------------------------------------------
  395. # Func : print debug msg
  396. ----------------------------------------------------------------------------------------
  397. sub mdebug{
  398.     my (@msg) = @_;
  399.     print "@msg\n" if ($opt{'debug'});
  400. }


  401. ----------------------------------------------------------------------------------------
  402. # Func : print error msg and exit
  403. ----------------------------------------------------------------------------------------
  404. sub merror{
  405.     my (@msg) = @_;
  406.     print ":@msg\n";
  407.     &print_usage();
  408.     exit(1);
  409. }

  410. ----------------------------------------------------------------------------------------
  411. # Func : print usage
  412. ----------------------------------------------------------------------------------------
  413. sub print_usage{
  414.     print <<EOF;
  415. ==========================================================================================
  416. Command line options :
  417.     --help                # OUT : print help info 
  418.     -f, --srcfile            # IN : binlog file. [required]
  419.     -o, --outfile            # OUT : output sql file. [required]
  420.     -h, --host            # IN : host. default '127.0.0.1'
  421.     -u, --user            # IN : user. [required]
  422.     -p, --password            # IN : password. [required] 
  423.     -P, --port            # IN : port. default '3306'
  424.     --start-datetime        # IN : start datetime
  425.     --stop-datetime            # IN : stop datetime
  426.     --start-position        # IN : start position
  427.     --stop-position            # IN : stop position
  428.     -d, --database            # IN : database, split comma
  429.     -T, --table            # IN : table, split comma. [required] set -d
  430.     -i, --ignore            # IN : ignore binlog check contain DDL(CREATE|ALTER|DROP|RENAME)
  431.     --debug                # IN : print debug information

  432. Sample :
  433.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' 
  434.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' -i
  435.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' --debug
  436.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -h '192.168.1.2' -u 'user' -p 'pwd' -P 3307
  437.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107
  438.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' --start-position=107 --stop-position=10000
  439.    shell> perl binlog-rollback.pl -'mysql-bin.000001' -'/tmp/t.sql' -u 'user' -p 'pwd' -'db1,db2'
  440.    shell> perl binlog-rollback.pl -'mysql-bin.0000*' -'/tmp/t.sql' -u 'user' -p 'pwd' -'db1,db2' -'tb1,tb2'
  441. ==========================================================================================
  442. EOF
  443.     exit; 
  444. }


  445. 1;
阅读(15231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~