前言:
MySQL 逻辑备份工具最常用的就是 mysqldump 了,一般我们都是备份整个实例或部分业务库。不清楚你有没有做过恢复,恢复场景可能就比较多了,比如我想恢复某个库或某个表等。那么如何从全备中恢复单库或单表,这其中又有哪些隐藏的坑呢?这篇文章我们一起来看下。
1.如何恢复单库或单表
前面文章有介绍过 MySQL 的备份与恢复。可能我们每个数据库实例中都不止一个库,一般备份都是备份整个实例,但恢复需求又是多种多样的,比如说我想只恢复某个库或某张表,这个时候应该怎么操作呢?
如果你的实例数据量不大,可以在另外一个环境恢复出整个实例,然后再单独备份出所需库或表用来恢复。不过这种方法不够灵活,并且只适用数据量比较少的情况。
其实从全备中恢复单库还是比较方便的,有个 –one-database 参数可以指定单库恢复,下面来具体演示下:
# 查看及备份所有库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sbtest | | sys | | testdb | | testdb2 | +--------------------+ mysqldump -uroot -pxxxx -R -E --single-transaction --all-databases > all_db.sql # 删除testdb库 并进行单库恢复 mysql> drop database testdb; Query OK, 36 rows affected (2.06 sec) # 貌似恢复前 testdb库不存在的话要手动新建 mysql -uroot -pxxxx --one-database testdb < all_db.sql
除了上述方法外,恢复单库或单表还可以采用手动筛选的方法。这个时候 Linux 下大名鼎鼎的 sed 和 grep 命令就派上用场了,我们可以利用这两个命令从全备中筛选出单库或单表的语句,筛选方法如下:
# 从全备中恢复单库 sed -n '/^-- Current Database: `testdb`/,/^-- Current Database: `/p' all_db.sql > testdb.sql # 筛选出单表语句 cat all_db.sql | sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `test_tb`/!d;q' > /tmp/test_tb_info.sql cat all_db.sql | grep --ignore-case 'insert into `test_tb`' > /tmp/test_tb_data.sql
2.小心有坑
对于上述手动筛选来恢复单库或单表的方法,看起来简单方便,其实隐藏着一个小坑,下面我们来具体演示下:
# 备份整个实例 mysqldump -uroot -pxxxx -R -E --single-transaction --all-databases > all_db.sql # 手动备份下test_tb 然后删除test_tb mysql> create table test_tb_bak like test_tb; Query OK, 0 rows affected (0.03 sec) mysql> insert into test_tb_bak select * from test_tb; Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> drop table test_tb; Query OK, 0 rows affected (0.02 sec) # 从全备中筛选test_db建表及插数据语句 cat all_db.sql | sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `test_tb`/!d;q' > test_tb_info.sql cat all_db.sql | grep --ignore-case 'insert into `test_tb`' > test_tb_data.sql # 查看得到的语句 貌似没问题 cat test_tb_info.sql DROP TABLE IF EXISTS `test_tb`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test_tb` ( `inc_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `col1` int(11) NOT NULL, `col2` varchar(20) DEFAULT NULL, `col_dt` datetime DEFAULT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', PRIMARY KEY (`inc_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='测试表'; /*!40101 SET character_set_client = @saved_cs_client */; cat test_tb_data.sql INSERT 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'), (2,1002,'vfsfs','2020-09-04 12:12:36','2020-09-17 06:19:27','2020-09-17 06:19:27'), (3,1003,'adsfsf',NULL,'2020-09-17 06:19:27','2020-09-17 06:19:27'), (4,1004,'walfd','2020-09-17 14:19:27','2020-09-17 06:19:27','2020-09-18 07:52:13'); # 执行恢复单表操作 mysql -uroot -pxxxx testdb < test_tb_info.sql mysql -uroot -pxxxx testdb < test_tb_data.sql # 查看恢复数据 并和备份表比对 mysql> select * from test_tb; +--------+------+--------+---------------------+---------------------+---------------------+ | inc_id | col1 | col2 | col_dt | create_time | update_time | +--------+------+--------+---------------------+---------------------+---------------------+ | 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 | | 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 | | 3 | 1003 | adsfsf | NULL | 2020-09-17 06:19:27 | 2020-09-17 06:19:27 | | 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 06:19:27 | 2020-09-18 07:52:13 | +--------+------+--------+---------------------+---------------------+---------------------+ 4 rows in set (0.00 sec) mysql> select * from test_tb_bak; +--------+------+--------+---------------------+---------------------+---------------------+ | inc_id | col1 | col2 | col_dt | create_time | update_time | +--------+------+--------+---------------------+---------------------+---------------------+ | 1 | 1001 | dsfs | 2020-08-04 12:12:36 | 2020-09-17 14:19:27 | 2020-0<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i>9-17 14:19:27 | | 2 | 1002 | vfsfs | 2020-09-04 12:12:36 | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | | 3 | 1003 | adsfsf | NULL | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | | 4 | 1004 | walfd | 2020-09-17 14:19:27 | 2020-09-17 14:19:27 | 2020-09-18 15:52:13 | +--------+------+--------+---------------------+---------------------+---------------------+ 4 rows in set (0.00 sec)