1. 程式人生 > 其它 >寶塔面板mysql表誤操作導致原資料表被刪恢復過程(附php程式碼正則插入id)

寶塔面板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

如果沒有報錯就可以看 檔案在生成了沒有,生成了以後就可以開啟看看裡面的插入和修改檔案了

  1 <?php
  2 
  3 namespace app\api\controller;
  4 
  5 use app\common\controller\Api;
  6 use think\Db;
  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})\/\*\!\*\/\;\n\#[0-9]{1,10}.*\nSET TIMESTAMP=[0-9]{1,12}\/\*\!\*\/\;\n/'; 30 31 //插入正則 32 $insert_pre ='/SET INSERT_ID=([0-9]{1,10})\/\*\!\*\/\;\n\#[0-9]{1,10}.*\nSET TIMESTAMP=[0-9]{1,12}\/\*\!\*\/\;\n(INSERT INTO \`shop_goods`.*)/'; 33 34 //可以先自己除錯 35 // $insert_pre ='/SET INSERT_ID=([0-9]{1,10})\/\*\!\*\/\;.*\n\r.*(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 }