码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • mysql 闪回表工具


    use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
    use POSIX;
    my $SDATE = strftime("%Y-%m-%d",localtime());
    if ( $#ARGV < 4 ){  
            print "please input  like  binglog-0001 '2013-07-01 00:00:00' '2013-07-02 00:00:00' dml db_name table!\n";
            exit(-1);  
                  }; 
          my   $binlog= $ARGV[0];  
          my   $start_dt = $ARGV[1];
          my   $end_dt = $ARGV[2];
          my   $dml =$ARGV[3];
          my   $database=$ARGV[4];
          my   $table=$ARGV[5];
          unlink("$table.$SDATE.txt");
          unlink("$table.$SDATE.txt.tmp.1");
          unlink("$table.$SDATE.txt.tmp.2");
          unlink("$table.$SDATE.txt.tmp.3");
          my @binlog=`mysqlbinlog --base64-output=decode-rows -v --start-datetime='$start_dt' --stop-datetime='$end_dt' $binlog >$table.$SDATE.txt`;
          use DBI;
          my $db_name="$database";
          my $ip='127.0.0.1';
          my $user="root";
          my $passwd="1234567";
          $dbh = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
          $UNLOAD_SRC_DBCONN = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr; 
          my $hostSql = qq{SELECT column_name  from information_schema.columns where table_schema='$db_name' and  table_name='$table'};
          my $DW_DATA_DT ="";
          @lstRlst1=();
          @lstRlst=();
          my ($COLUMN_NAME);
          my $selStmt = $dbh->prepare($hostSql);
          $selStmt->execute();
          $selStmt->bind_col(1, \$COLUMN_NAME);;
          $selStmt->execute();
         while( $selStmt->fetch() ){
            print "$COLUMN_NAME\n";
            push  (@lstRlst1 ,$COLUMN_NAME);
              }
           $selStmt->finish;
           $dbh->disconnect;
           for ($m=1;$m<=@lstRlst1 ; $m++){
              $hash{"\@$m"}=$lstRlst1[$m -1 ];
             };
           print %hash;
           print "\n";
           sub undo_update { 
           local $/='/*!*/;';
           open (A,"<","$table.$SDATE.txt");
           while (){
           if  (( $_ =~/$dml\s+$table/i ) or ($_ =~/$dml\s+`$table`/i) or ($_ =~/$dml\s+`$database`.`$table`/i) or ($_ =~/$dml\s+$database.$table/i )){
                local $/="\n";
               #if ($_ =~/.*?(\@[0-9]+).*/){print "\$id is $1\n";};
               #print $_;
               open DATAFH,">>$table.$SDATE.txt.tmp.1" || die "open csdn file failed:$!"; 
               print DATAFH  $_;
             };
           };
          #print DATAFH  "\n";
          close DATAFH;
          print "\n";
          local $/="\n";
          open (B,"<","$table.$SDATE.txt.tmp.1");
           print "orignal update sql\n";
          while (){
               next unless   /^###/;
            if ($_ =~/.*?(\@[0-9]+).*/)
             {
              my $idnum=$1;
              #print "\$idnum is $idnum\n";
              my $id=$hash{"$idnum"};
              $_ =~ s/$idnum/$id/;
               print $_;
              open (C,">>","$table.$SDATE.txt.tmp.2");
              print C  $_;
              close C
             }
            else {
               print $_;
              open (C,">>","$table.$SDATE.txt.tmp.2");
              print C  $_;};
              close C;
            };
           close B;
           open (C,"<","$table.$SDATE.txt.tmp.2");
           print "rollback update sql\n";
           while (){
               next unless   /^###/;
               #$_ =~ s/^###//;
               if ($_ =~ s/WHERE/SET/i){ print "$_";}
               elsif ($_ =~ s/SET/WHERE/i){ print "$_";}
               else{print "$_";}
              };
           close C;
           };
    	
    	  sub undo_delete {
    	  local $/='/*!*/;';
              open (A,"<","$table.$SDATE.txt");
              while (){
                if  (( $_ =~/$dml\s+from\s+$table/i ) or ($_ =~/$dml\s+from\s+`$table`/i) or ($_ =~/$dml\s+from\s+`$database`.`$table`/i) or ($_ =~/$dml\s+from\s+$database.$table/i )){
                local $/="\n";
               #if ($_ =~/.*?(\@[0-9]+).*/){print "\$id is $1\n";};
               #print $_;
               open DATAFH,">>$table.$SDATE.txt.tmp.1" || die "open csdn file failed:$!"; 
               print DATAFH  $_;
               };
              };
              #print DATAFH  "\n";
              close DATAFH;
              print "\n";
              local $/="\n";
              open (B,"<","$table.$SDATE.txt.tmp.1");
              while (){
               if ($_ =~/.*?(\@[0-9]+).*/)
                 {
                 my $idnum=$1;
                 #print "\$idnum is $idnum\n";
                 my $id=$hash{"$idnum"};
                 $_ =~ s/$idnum/$id/;
                 print $_;
                 open (C,">>","$table.$SDATE.txt.tmp.2");
                 print C  $_;
                 close C
                 }
                 else {
                 print $_;
                 open (C,">>","$table.$SDATE.txt.tmp.2");
                 print C  $_;};
                 close C;
                 };
                close B;    
    		   open (C,"<","$table.$SDATE.txt.tmp.2");
                while ()
                {
                next  unless /^###/; 
                next  if /^$/; 
                open (D,">>","$table.$SDATE.txt.tmp.3");
                if ($_ =~ s/###\s+DELETE FROM/INSERT INTO/i){ print D $_;}
                elsif ($_ =~ s/###\s+WHERE/values/i){  print D $_;}
                elsif($_ =~ s/^###\s+//g){  print D $_;}
                close D;
                  };
               close C;
               print "-----------------------------\n";
                @arr=(); 
                $sql="INSERT INTO $table VALUES";
               open  (E,"<","$table.$SDATE.txt.tmp.3");
               while (){
                 if ($_ =~ /.*?=(.*)/){push (@arr,$1) };
                 if ( @arr + 0 == @lstRlst1 + 0){
                 print "\@arr is @arr\n";
                 for (my $m=0;$m<@arr + 0 ;$m++)
                      {  
                      if  ($m == 0 ){
                      $sql="$sql ($arr[$m]".",";
                      }
                      elsif ($m == @arr - 1){
                      $sql="$sql $arr[$m]".");"
                      }
                      else{$sql="$sql $arr[$m]".","};  
                      };
    		  @arr=();
                      print "\$sql is $sql\n";
                      $sql="INSERT INTO $table VALUES";
                        };
               };
               close E;
               };
     
              if ($dml =~ /update/i){&undo_update}; 
              if ($dml =~ /delete/i){&undo_delete}; 
  • 相关阅读:
    微表情识别API + c++并发服务器系统
    【软件项目管理】敏捷软件开发 —— Scrum模式
    【图像提取】DNA编解码多尺度形态学提取眼前节组织【含Matlab源码 1191期】
    阿里云国际修改域名绑定的DDoS高防服务器
    普通人如何投资理财?
    蓝桥杯算法赛 第 6 场 小白入门赛 解题报告 | 珂学家 | 简单场 + 元宵节日快乐
    【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第一篇 嵌入式Linux入门篇-第二十八章 借助U盘或TF卡拷贝程序到开发板上
    有些段子,外行人根本看不懂,只有程序员看了会狂笑不止
    LeetCode0461.汉明距离 Go语言AC笔记
    HarmonyOS数据管理与应用数据持久化(一)
  • 原文地址:https://blog.csdn.net/m0_71272694/article/details/126678023
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号