宝塔面板mysql表误操作导致原数据表被删恢复过程(附php代码正则插入id)

由于程序员要修改表里面的字段,用Navicat数据库工具导出了表的结构,又运行了导出的结构,导致里面的数据都没有了,数据库一直没有备份过,到时没有备份文件,结果百度了一大把,说要开启binlog,结果看配置里面没有开,无意看到宝塔有开启,于是按照教程

“找到宝塔面板的/www/server/data文件夹”

看里面有没有mysql-bin.0000x文件 (如果有就有很大的希望了,最好时间是最近的,数据文件也比较大)

我这里是mysql-bin.000008

然后运行终端命令 

先到需要还原文件目录里面
自己替换
mysql-bin.000008替换成自己的名字
后面的/www/1.sql 是保存后的地址(最后就看这个文件)
cd /www/server/data

/www/server/mysql/bin/mysqlbinlog --base64-output=DECODE-ROWS -v -d mysql-bin.000008 > /www/1.sql

如果没有报错就可以看 文件在生成了没有,生成了以后就可以打开看看里面的插入和修改文件了

下面的代码是thinkphp5运行的,也可以改成非框架的 自行修改

  1 <?php
  2 
  3 namespace appapicontroller;
  4 
  5 use appcommoncontrollerApi;
  6 use thinkDb;
  7 /**
  8  * 替换sql
  9  */
 10 class Sql extends Api
 11 {
 12 
 13     //这个是fastadmin的 不需要就注释
 14     // 无需登录的接口,*表示全部
 15     protected $noNeedLogin = ['*'];
 16     // 无需鉴权的接口,*表示全部
 17     protected $noNeedRight = ['*'];
 18 
 19     /**
 20      * 获取插入
 21      *
 22      * @return void
 23      */
 24     public function get_in()
 25     {
 26         //获取文件内容
 27         $str = file_get_contents('2.sql');
 28 
 29         // $insert_pre ='/SET INSERT_ID=([0-9]{1,10})/*!*/;
#[0-9]{1,10}.*
SET TIMESTAMP=[0-9]{1,12}/*!*/;
/';
 30         
 31         //插入正则
 32         $insert_pre ='/SET INSERT_ID=([0-9]{1,10})/*!*/;
#[0-9]{1,10}.*
SET TIMESTAMP=[0-9]{1,12}/*!*/;
(INSERT INTO \`shop_goods`.*)/';
 33         
 34         //可以先自己调试
 35         // $insert_pre ='/SET INSERT_ID=([0-9]{1,10})/*!*/;.*

.*(INSERT INTO \`shop_goods`.*)/';
 36         // $insert_pre ='/(INSERT INTO \`shop_goods`.*)/';
 37         
 38       
 39 
 40 
 41 
 42         preg_match_all($insert_pre, $str, $newarr);//
 43 
 44         foreach($newarr[1] as $key=>$id){
 45 
 46             $in_sql = $this->tihuan_in($id,$newarr[2][$key]);
 47             $arr[$id] = $in_sql; 
 48             $res = Db::table('shop_goods')->find($id);
 49             if(!$res)
 50             $res = Db::query($in_sql);
 51             // halt($arr);
 52         }
 53         halt($arr);
 54         // $this->success('返回成功', ['action' => 'test3']);
 55     }
 56 
 57     /**
 58      * 替换插入字段
 59      *
 60      * @param int $id binlog里面的插入id
 61      * @param string $str 需要加入id的字符串
 62      * @return void
 63      */
 64     public function tihuan_in($id,$str)
 65     {
 66         $str1 = str_replace("`shop_goods` (`","`shop_goods` (`id`,`",$str);
 67         $str1 = str_replace("VALUES ('","VALUES ('".$id."','",$str1);
 68         return $str1;
 69     }
 70 
 71 
 72 
 73 
 74 
 75      /**
 76      * 获取修改
 77      *
 78      * @return void
 79      */
 80     public function get_update()
 81     {
 82         //获取文件内容
 83         $str = file_get_contents('2.sql');
 84 
 85         //         UPDATE `shop_goods` SET `yi_id`='10214' WHERE ( id=10725 )
 86         // /*!*/;
 87             //修改正则
 88         $insert_pre ='/(UPDATE \`shop_goods` SET.*)/';
 89 
 90 
 91 
 92 
 93         preg_match_all($insert_pre, $str, $newarr);//
 94 
 95         halt($newarr);
 96         foreach($newarr[1] as $key=>$up_sql){
 97 
 98             // halt($up_sql);
 99             $res = Db::query($up_sql);
100             // halt($arr);
101         }
102         //   halt($newarr);
103     }
104 
105     /**
106      * 替换修改字段
107      *
108      * @param int $id binlog里面的修改id
109      * @param string $str 需要加入id的字符串
110      * @return void
111      */
112     public function tihuan_up($id,$str)
113     {
114         $str1 = str_replace("`shop_goods` (`","`shop_goods` (`id`,`",$str);
115         $str1 = str_replace("VALUES ('","VALUES ('".$id."','",$str1);
116         return $str1;
117     }
118 
119 
120     /**
121      * 获取这个表的总数
122      *
123      * @return void
124      */
125     public function get_all()
126     {
127         $num = Db::table('shop_goods')->count();
128         echo $num;
129     }
130 }
原文地址:https://www.cnblogs.com/xiaohe520/p/14892768.html