1. 程式人生 > >MySQL用全庫備份資料恢復單表資料

MySQL用全庫備份資料恢復單表資料

備份資料庫時,採用了全庫備份,但是因為某些原因需要回滾一個表的資料到備份資料庫上,如果回滾整個庫就比較費時間,因為可能這個表只有幾十M,但是其它表可能有十幾上百G,這時候就需要將需要恢復的表提取出來了

現在有備份庫fdcsqlmysql-2018_11_30-03_00_01.sql,裡面有多張表,現在需要恢復其中fdc_document這張表的資料

提取建表語句
sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `表名`/!d;q' mysqldump.sql(備份檔案的檔名)
sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `fdc_document`/!d;q' fdcsqlmysql-2018_11_30-03_00_01.sql 

DROP TABLE IF EXISTS `fdc_document`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `fdc_document` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '文件ID',
  `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用者ID',
  `name` char(40) NOT NULL DEFAULT '' COMMENT '標識',
   ...
   ...
   ...
  `entrust_rule` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT ' 經紀人點選是否和使用者籤委託協議:1為有;0為沒有',
  `audit` tinyint(3) NOT NULL DEFAULT '0' COMMENT '稽核:0為未稽核;1為圖片已稽核;2為描述已稽核;3為圖片和描述都已稽核',
  PRIMARY KEY (`id`),
  KEY `idx_area_house` (`partition`,`category_id`,`status`,`is_off`) USING BTREE,
  KEY `idx_model_house` (`model_id`,`status`,`is_off`) USING BTREE,
  KEY `idx_community_house` (`community_id`,`estate`,`status`,`is_off`) USING BTREE,
  KEY `idx_uid_house` (`uid`,`model_id`,`is_off`) USING BTREE,
  KEY `idx_pid_house` (`id`,`pid`,`status`,`is_off`) USING BTREE,
  KEY `is_video` (`is_video`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=211138 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
提取表資料

grep 'INSERT INTO表名' mysqldump.sql(備份檔案的檔名) > table_data.sql

這裡應該執行grep 'INSERT INTOfdc_document' fdcsqlmysql-2018_11_30-03_00_01.sql > document.sql

執行完後會得到檔案document.sql,這就是需要的單獨的表文件,就可以正常恢復表資料了

建庫建表

先建立資料庫,再根據上面的SQL語句建立表fdc_document

匯入表資料
MySQL [document]> souce /data/backup/mysql/document.sql

ok,完工!