Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2353490
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: Mysql/postgreSQL

2012-10-08 17:08:01

原理:解析row格式的binlog日志,把delete语句转化成replace语句。
这个工具也可以把update,insert语句转化为replace语句。

  

点击(此处)折叠或打开

  1. #! /usr/bin/perl -w
  2. #
  3. # parse INSERT, UPDATE, DELETE event from row-formated binlog
  4. # usage: mysqlbinlog --no-defaults -vvv <binlog> | parse_binlog > out.sql
  5. # use it at your own risk.
  6. #
  7. my $state="init";
  8. my $i=0;
  9. my $col=0;
  10. my $tabname;

  11. my $sql;

  12. my $var;
  13. my $type;

  14. while (<>) {
  15.     chomp;
  16.     if ($state eq "init") {
  17.         if ($_ =~ m/^### UPDATE ([a-zA-Z._0-9]+)/) {
  18.              $tabname=$1;
  19.              print "--table: $1\n";
  20.              $state="upd";
  21.              $type = "UPDATE";
  22.         }
  23.         elsif ($_ =~ m/^### INSERT INTO ([a-zA-Z._0-9]+)/) {
  24.              $tabname=$1;
  25.              print "--table: $1\n";
  26.              $state="ins";
  27.              $type = "INSERT";
  28.         } elsif ($_ =~ m/^### DELETE FROM ([a-zA-Z._0-9]+)/) {
  29.              $tabname=$1;
  30.              print "--table: $1\n";
  31.              $state="del";
  32.              $type = "DELETE";
  33.         }
  34.     }
  35.     if ($state eq "upd") {
  36.         if ($_ =~ m/^### SET/) {
  37.             $state = "coll";
  38.         }
  39.         $col = 0;
  40.     }
  41.     if ($state eq "ins") {
  42.         if ($_ =~ m/^### SET/) {
  43.             $state = "coll";
  44.         }
  45.         $col = 0;
  46.     }
  47.     if ($state eq "del") {
  48.         if ($_ =~ m/^### WHERE/) {
  49.             $state = "coll";
  50.         }
  51.         $col = 0;
  52.     }
  53.     if ($state eq "coll") {
  54.        if ($_ =~ m/^### @([0-9]?[0-9])=(.*) +\/[*].*[*]\/$/ ) {
  55.            #print "$1, $2\n";
  56.            # 2012-04-21 12:43:22
  57.            $var = $2;
  58.            if ($var =~ m/([0-9][0-9][01][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9])/) {
  59.                $var = "'$1'";
  60.            }
  61.            if ($var =~ m/(-[0-9]+) \([0-9]+\)/) {
  62.                $var = $1;
  63.            }
  64.            if ($col == 0) {
  65.               $sql = "replace into $tabname values ( $var ";
  66.            } else {
  67.               $sql = $sql . ", $var";
  68.            }
  69.            $col++;
  70.        } elsif ($_ =~ m/^# at [0-9]+$/) {
  71.            $state="init";
  72.            $sql = $sql . ");";
  73.            print "--DML type: $type, num of cols: $col\n$sql\n";
  74.        } elsif ($_ =~ m/^### UPDATE ([a-zA-Z._0-9]+)/) {
  75.            $sql = $sql . ");";
  76.            print "--DML type: $type, num of cols: $col\n$sql\n";
  77.            $tabname=$1;
  78.            $state="upd";
  79.        } elsif ($_ =~ m/^### INSERT INTO ([a-zA-Z._0-9]+)/) {
  80.            $sql = $sql . ");";
  81.            print "--DML type: $type, num of cols: $col\n$sql\n";
  82.            $tabname=$1;
  83.            $state="ins";
  84.        } elsif ($_ =~ m/^### DELETE FROM ([a-zA-Z._0-9]+)/) {
  85.            $sql = $sql . ");";
  86.            print "--DML type: $type, num of cols: $col\n$sql\n";
  87.            $tabname=$1;
  88.            $state="del";
  89.        }
  90.     }

  91.     #print "line $i: $state\n";
  92.     $i++;
  93. }

阅读(2108) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~