從全備中恢復單庫或單表,小心有坑!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"前言:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL 邏輯備份工具最常用的就是 mysqldump 了,一般我們都是備份整個實例或部分業務庫。不清楚你有沒有做過恢復,恢復場景可能就比較多了,比如我想恢復某個庫或某個表等。那麼如何從全備中恢復單庫或單表,這其中又有哪些隱藏的坑呢?這篇文章我們一起來看下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FC8F99","name":"red"}}],"text":" "},{"type":"text","text":" 1.如何恢復單庫或單表"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/cb36e11d7c4088ad60065fdcb","title":""},"content":[{"type":"text","text":"前面文章"}]},{"type":"text","text":"有介紹過 MySQL 的備份與恢復。可能我們每個數據庫實例中都不止一個庫,一般備份都是備份整個實例,但恢復需求又是多種多樣的,比如說我想只恢復某個庫或某張表,這個時候應該怎麼操作呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你的實例數據量不大,可以在另外一個環境恢復出整個實例,然後再單獨備份出所需庫或表用來恢復。不過這種方法不夠靈活,並且只適用數據量比較少的情況。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實從全備中恢復單庫還是比較方便的,有個 "},{"type":"codeinline","content":[{"type":"text","text":"--one-database"}]},{"type":"text","text":" 參數可以指定單庫恢復,下面來具體演示下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 查看及備份所有庫\nmysql> show databases;\n+--------------------+\n| Database |\n+--------------------+\n| information_schema |\n| mysql |\n| performance_schema |\n| sbtest |\n| sys |\n| testdb |\n| testdb2 |\n+--------------------+\n\nmysqldump -uroot -pxxxx -R -E --single-transaction --all-databases > all_db.sql\n\n# 刪除testdb庫 並進行單庫恢復\nmysql> drop database testdb;\nQuery OK, 36 rows affected (2.06 sec)\n\n# 貌似恢復前 testdb庫不存在的話要手動新建\nmysql -uroot -pxxxx --one-database testdb < all_db.sql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了上述方法外,恢復單庫或單表還可以採用手動篩選的方法。這個時候 Linux 下大名鼎鼎的 "},{"type":"codeinline","content":[{"type":"text","text":"sed"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"grep"}]},{"type":"text","text":" 命令就派上用場了,我們可以利用這兩個命令從全備中篩選出單庫或單表的語句,篩選方法如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 從全備中恢復單庫\nsed -n '/^-- Current Database: `testdb`/,/^-- Current Database: `/p' all_db.sql > testdb.sql\n\n# 篩選出單表語句\ncat all_db.sql | sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `test_tb`/!d;q' > /tmp/test_tb_info.sql \ncat all_db.sql | grep --ignore-case 'insert into `test_tb`' > /tmp/test_tb_data.sql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FC8F99","name":"red"}}],"text":" "},{"type":"text","text":" 2.小心有坑"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於上述手動篩選來恢復單庫或單表的方法,看起來簡單方便,其實隱藏着一個小坑,下面我們來具體演示下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 備份整個實例\nmysqldump -uroot -pxxxx -R -E --single-transaction --all-databases > all_db.sql\n\n# 手動備份下test_tb 然後刪除test_tb\nmysql> create table test_tb_bak like test_tb;\nQuery OK, 0 rows affected (0.03 sec)\n\nmysql> insert into test_tb_bak select * from test_tb;\nQuery OK, 4 rows affected (0.02 sec)\nRecords: 4 Duplicates: 0 Warnings: 0\n\nmysql> drop table test_tb;\nQuery OK, 0 rows affected (0.02 sec)\n\n# 從全備中篩選test_db建表及插數據語句\ncat all_db.sql | sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `test_tb`/!d;q' > test_tb_info.sql \ncat all_db.sql | grep --ignore-case 'insert into `test_tb`' > test_tb_data.sql\n\n# 查看得到的語句 貌似沒問題\ncat test_tb_info.sql\n\nDROP TABLE IF EXISTS `test_tb`;\n/*!40101 SET @saved_cs_client = @@character_set_client */;\n/*!40101 SET character_set_client = utf8 */;\nCREATE TABLE `test_tb` (\n `inc_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',\n `col1` int(11) NOT NULL,\n `col2` varchar(20) DEFAULT NULL,\n `col_dt` datetime DEFAULT NULL,\n `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',\n `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',\n PRIMARY KEY (`inc_id`)\n) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='測試表';\n/*!40101 SET character_set_client = @saved_cs_client */;\n\ncat test_tb_data.sql\n\nINSERT INTO `test_tb` VALUES (1,1001,'dsfs','2020-08-04 12:12:36','2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(2,1002,'vfsfs','2020-09-04 12:12:36','2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(3,1003,'adsfsf',NULL,'2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(4,1004,'walfd','2020-09-17 14:19:27','2020-09-17 06:19:27','2020-09-18 07:52:13');\n\n# 執行恢復單表操作\nmysql -uroot -pxxxx testdb < test_tb_info.sql\nmysql -uroot -pxxxx testdb < test_tb_data.sql\n\n# 查看恢復數據 並和備份表比對\nmysql> select * from test_tb;\n+--------+------+--------+---------------------+---------------------+---------------------+\n| inc_id | col1 | col2 | col_dt | create_time | update_time |\n+--------+------+--------+---------------------+---------------------+---------------------+\n| 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 |\n| 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 |\n| 3 | 1003 | adsfsf | NULL | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 |\n| 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 06:19:27 | 2020-09-18 07:52:13 |\n+--------+------+--------+---------------------+---------------------+---------------------+\n4 rows in set (0.00 sec)\n\nmysql> select * from test_tb_bak;\n+--------+------+--------+---------------------+---------------------+---------------------+\n| inc_id | col1 | col2 | col_dt | create_time | update_time |\n+--------+------+--------+---------------------+---------------------+---------------------+\n| 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 3 | 1003 | adsfsf | NULL | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | 2020-09-18 15:52:13 |\n+--------+------+--------+---------------------+---------------------+---------------------+\n4 rows in set (0.00 sec)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你仔細觀察的話,會發現恢復出來的數據有問題,貌似時間不太對,你再仔細看看,是不是有的時間差了8小時!詳細探究下來,我們發現 timestamp 類型字段的時間數據恢復有問題,準確來講備份文件中記錄的是0時區,而我們系統一般採用東八區,所以纔會出現誤差8小時的問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼你會問了,爲什麼全部恢復不會出問題呢?問的好,我們看下備份文件就知道了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 備份文件開頭\n-- MySQL dump 10.13 Distrib 5.7.23, for Linux (x86_64)\n--\n-- Host: localhost Database:\n-- ------------------------------------------------------\n-- Server version 5.7.23-log\n\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n/*!40101 SET NAMES utf8 */;\n/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n/*!40103 SET TIME_ZONE='+00:00' */; \n注意上面兩行\n/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n\n# 備份文件結尾\n/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n\n-- Dump completed on 2020-09-18 15:56:40"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"仔細看備份文件,你會發現 mysqldump 備份出來的文件中,首先會將會話時區改爲0,結尾處再改回原時區。這就代表着,備份文件中記錄的時間戳數據都是以0時區爲基礎的。如果直接執行篩選出的SQL,就會造成0時區的時間戳插入的東八區的系統中,顯然會造成時間相差8小時的問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到這裏,不知道你是否看懂了呢,可能有過備份恢復經驗的同學好理解些。解決上述問題的方法也很簡單,那就是在執行SQL文件前,更改當前會話時區爲0,再次來演示下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 清空test_db表數據\nmysql> truncate table test_tb;\nQuery OK, 0 rows affected (0.02 sec)\n\n# 文件開頭增加時區聲明\nvim test_tb_data.sql\nset session TIME_ZONE='+00:00';\nINSERT INTO `test_tb` VALUES (1,1001,'dsfs','2020-08-04 12:12:36','2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(2,1002,'vfsfs','2020-09-04 12:12:36','2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(3,1003,'adsfsf',NULL,'2020-09-17 06:19:27','2020-09-17 06:19:27'),\n(4,1004,'walfd','2020-09-17 14:19:27','2020-09-17 06:19:27','2020-09-18 07:52:13');\n\n# 執行恢復並比對 發現數據正確\nmysql> select * from test_tb;\n+--------+------+--------+---------------------+---------------------+---------------------+\n| inc_id | col1 | col2 | col_dt | create_time | update_time |\n+--------+------+--------+---------------------+---------------------+---------------------+\n| 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 3 | 1003 | adsfsf | NULL | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | 2020-09-18 15:52:13 |\n+--------+------+--------+---------------------+---------------------+---------------------+\n4 rows in set (0.00 sec)\n\nmysql> select * from test_tb_bak;\n+--------+------+--------+---------------------+---------------------+---------------------+\n| inc_id | col1 | col2 | col_dt | create_time | update_time |\n+--------+------+--------+---------------------+---------------------+---------------------+\n| 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 3 | 1003 | adsfsf | NULL | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 |\n| 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | 2020-09-18 15:52:13 |\n+--------+------+--------+---------------------+---------------------+---------------------+\n4 rows in set (0.00 sec)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"總結:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在網絡中很容易搜索出恢復單庫或單表的方法,大多都有提到上述利用 sed 、grep 命令來手動篩選的方法。但大部分文章都未提及可能出現的問題,如果你的表字段有timestamp 類型,用這種方法要格外注意。無論面對哪種恢復需求,我們都要格外小心,不要造成越恢復越糟糕的情況,最好有個空實例演練下,然後再進行恢復。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章