一、Shell备份恢复简介
1.1 基本说明
- MySQL Shell 8.0.21,新增逻辑备份方式,可以选择instance或指定的schema备份。
- 支持多线程并行备份、文件压缩,并可显示备份进度信息。
- 当选择保存的路径时,MySQL必须有权限访问。
- ( 实例或者指定库 )备份文件包括DDL文件以及包含数据的制表符分割的.tsv文件,也可以选择只备份DDL文件或者数据文件。
- 可以选择是否在备份期间锁定实例,以确保数据一致性。
- 默认情况下,备份将表数据分成多个数据文件并进行压缩
1.2 限制说明
-
源MySQL实例和目标MySQL实例都需要MySQL 5.7或更高版本。
-
备份实例或者库的对象必须是latin1 or utf8字符集。
-
仅对使用InnoDB存储引擎的表保证数据一致性 。
-
要导入到MySQL数据库系统中,请将 ocimds 选项设置为 true,
1.3 环境说明
- 当前文档实验环境:MySQL 8.0.21
- 5.7 版本是否可行,有待测试
二、实例级备份恢复
2.1 实例级备份
-
检查是否满足 mysql shell 备份的条件
-
关键参数:
dryRun: true
-
权限参数:
compatibility: ["strip_definers", "strip_restricted_grants"]:
- 根据检查的结果,我们知道有些用户具有受限制的权限,所以,需要使用 compatibility 选项的strip_restricted_grants模式修改备份文件
-
MySQL 10.186.60.54:3306 ssl JS > util.dumpInstance("/tmp/instance_3306",{dryRun: true,ocimds: true,compatibility: ["strip_definers", "strip_restricted_grants"]})
- 真正执行备份操作
- 删除参数:
dryRun: true
- 删除参数:
## 根据输出可以看到,开启了四个线程备份
## 表数据备份根据主键进行分块
## 进度信息,备份数据大小,备份速度等信息比较全
MySQL 10.186.60.54:3306 ssl JS > util.dumpInstance("/tmp/instance_3306",{ocimds: true,compatibility: ["strip_definers", "strip_restricted_grants"]})
....(略)
1 thds dumping - 101% (800.00K rows / ~789.41K rows), 153.24K rows/s, 29.58 MB/s uncompressed, 13.44 MB/s compressed
Duration: 00:00:05s
Schemas dumped: 1
Tables dumped: 4
Uncompressed data size: 154.46 MB
Compressed data size: 70.17 MB
Compression ratio: 2.2
Rows written: 800000
Bytes written: 70.17 MB
Average uncompressed throughput: 29.43 MB/s
Average compressed throughput: 13.37 MB/s
2.2 查看文件
## 默认排除了基本库和个别表,具体见上面基础了解信息介绍;
## @.done.json文件,包含备份结束时间,备份数据总大小,以及各个表的数据大小,单位字节;
## @.json文件,包含MySQL Shell版本,Linux服务器以及备份的MySQL实例版本信息,备份的库和用户信息,其他字符集、UTC时区、Linux用户和主机名信息、gtidExecuted、是否一致性备份、备份开始时间、是否使用compatibility选项自动兼容性修改等;
## @.sql文件,信息较少,类似@.json文件开头部分,各版本信息及服务器IP
## @.post.sql,信息类似@.sql文件文件
[root@centos ~]# ls /tmp/instance_3306
@.done.json testdb.json testdb@sbtest1.sql testdb@sbtest2.sql testdb@sbtest3.sql testdb@sbtest4.sql
@.json testdb@sbtest1@@0.tsv.zst testdb@sbtest2@@0.tsv.zst testdb@sbtest3@@0.tsv.zst testdb@sbtest4@@0.tsv.zst testdb.sql
@.post.sql testdb@sbtest1@@0.tsv.zst.idx testdb@sbtest2@@0.tsv.zst.idx testdb@sbtest3@@0.tsv.zst.idx testdb@sbtest4@@0.tsv.zst.idx @.users.sql
@.sql testdb@sbtest1.json testdb@sbtest2.json testdb@sbtest3.json testdb@sbtest4.json
2.3 实例级恢复
-
转储加载实用程序使用该 LOAD DATA LOCAL INFILE 语句,因此 local_infile 目标MySQL实例上系统变量的全局设置 必须 ON在导入期间内进行。默认情况下,此系统变量ON在标准MySQL数据库系统配置中设置为 。
-
resetProgress: [ true | false ]
- 设置此选项可true
重置进度状态
,并从头开始重新导入。默认值为false。请注意,使用此选项,转储加载实用程序不会跳过已创建的对象,并且不会管理重复数据删除
。如果要使用此选项,为确保正确导入,必须首先从目标MySQL实例中手动删除所有先前加载的对象,包括该转储中的架构,表,用户,视图,触发器,例程和事件。否则,如果目标MySQL实例中已经存在转储文件中的对象,则导入将因错误而停止。在适当谨慎的情况下,您可以使用ignoreExistingObjects选项,以使实用程序报告重复的对象,但跳过它们并继续导入。
- 设置此选项可true
-
恢复 3306 备份到 3307 实例
- 关键参数:
resetProgress: true
- 关键参数:
MySQL 10.186.60.54:3307 ssl JS > \sql
MySQL 10.186.60.54:3307 ssl SQL > set global local_infile = on;
MySQL 10.186.60.54:3307 ssl SQL > \js
MySQL 10.186.60.54:3307 ssl JS > util.loadDump("/tmp/instance_3306",{resetProgress: true})
....(略)
4 chunks (800.00K rows, 154.46 MB) for 4 tables in 1 schemas were loaded in 14 sec (avg throughput 11.03 MB/s)
0 warnings were reported during the load.
2.4 查看恢复状态
- 直接连接 3307 实例查看数据库( 也可以使用 M有SQL Shell 连接切换 SQL 模式查看 )
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| sbtest1 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
+------------------+
4 rows in set (0.00 sec)
三、忽略库备份恢复
3.1 忽略库备份
- 当前 3306 实例有两个库:testdb、testdb_wsp
- 本次操作实现只备份 testdb_wsp 库,忽略 testdb 库的备份
- 如果需要
排除多个库
,excludeSchemas: ["testdb","testdb_wsp"]
MySQL 10.186.60.54:3306 ssl JS > util.dumpInstance("/tmp/instance_3306",{ocimds: true,excludeSchemas: ["testdb"],compatibility: ["strip_definers", "strip_restricted_grants"]})
[root@centos ~]# ls /tmp/instance_3306
@.done.json testdb_wsp.json testdb_wsp@sbtest1.sql testdb_wsp@sbtest2.sql testdb_wsp@sbtest3.sql testdb_wsp@sbtest4.sql
@.json testdb_wsp@sbtest1@@0.tsv.zst testdb_wsp@sbtest2@@0.tsv.zst testdb_wsp@sbtest3@@0.tsv.zst testdb_wsp@sbtest4@@0.tsv.zst testdb_wsp.sql
@.post.sql testdb_wsp@sbtest1@@0.tsv.zst.idx testdb_wsp@sbtest2@@0.tsv.zst.idx testdb_wsp@sbtest3@@0.tsv.zst.idx testdb_wsp@sbtest4@@0.tsv.zst.idx @.users.sql
@.sql testdb_wsp@sbtest1.json testdb_wsp@sbtest2.json testdb_wsp@sbtest3.json testdb_wsp@sbtest4.json
3.2 忽略库恢复
- 恢复 3306 实例备份的 testdb_wsp库数据到 3307 实例 ( 与上方实例级恢复操作一样 )
MySQL 10.186.60.54:3307 ssl JS > util.loadDump("/tmp/instance_3306",{resetProgress: true})
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb_wsp |
+--------------------+
6 rows in set (0.00 sec)
四、忽略表备份恢复
4.1 忽略表备份
- 如果需要排除多个表,excludeTables: ["testdb.sbtest1","testdb_wsp.sbtest2"]
MySQL 10.186.60.54:3306 ssl JS > util.dumpInstance("/tmp/instance_3306",{ocimds: true,excludeTables: ["testdb.sbtest1","testdb_wsp.sbtest2"],compatibility: ["strip_restricted_grants"]})
....(略)
Bytes written: 105.25 MB
Average uncompressed throughput: 30.31 MB/s
Average compressed throughput: 13.77 MB/s
4.2 忽略表恢复
- 恢复 3306 实例备份的 testdb、testdb_wsp 库数据到 3307 实例,不过没有备份 testdb.sbtest1、testdb_wsp.sbtest2 这两张表( 与上方实例级恢复操作一样 )
MySQL 10.186.60.54:3307 ssl JS > util.loadDump("/tmp/instance_3306",{resetProgress: true})
....(略)
6 chunks (1.20M rows, 231.68 MB) for 6 tables in 2 schemas were loaded in 23 sec (avg throughput 10.07 MB/s)
0 warnings were reported during the load.
五、指定库备份恢复
5.1 指定库备份
- 指定 testdb 库,只备份 testdb 库
util.dumpSchemas
MySQL 10.186.60.54:3306 ssl JS > util.dumpSchemas(["testdb"],"/tmp/schema_3306",{ocimds: true,compatibility: ["strip_definers", "strip_restricted_grants"]})
....(略)
Bytes written: 70.17 MB
Average uncompressed throughput: 26.45 MB/s
Average compressed throughput: 12.02 MB/s
[root@centos ~]# ls /tmp/schema_3306
@.done.json testdb.json testdb@sbtest1.sql testdb@sbtest2.sql testdb@sbtest3.sql testdb@sbtest4.sql
@.json testdb@sbtest1@@0.tsv.zst testdb@sbtest2@@0.tsv.zst testdb@sbtest3@@0.tsv.zst testdb@sbtest4@@0.tsv.zst testdb.sql
@.post.sql testdb@sbtest1@@0.tsv.zst.idx testdb@sbtest2@@0.tsv.zst.idx testdb@sbtest3@@0.tsv.zst.idx testdb@sbtest4@@0.tsv.zst.idx
@.sql testdb@sbtest1.json testdb@sbtest2.json testdb@sbtest3.json testdb@sbtest4.json
5.2 指定库恢复
MySQL 10.186.60.54:3307 ssl JS > util.loadDump("/tmp/schema_3306")
....(略)
4 chunks (800.00K rows, 154.46 MB) for 4 tables in 1 schemas were loaded in 14 sec (avg throughput 11.03 MB/s)
0 warnings were reported during the load.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
-
官方备份参考链接:https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-dump-instance-schema.html
-
官方恢复参考链接:https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-load-dump.html
-
操作有问题请及时查看官方手册,里面包含各种参数使用方式及说明解释