备份工具
1.工具下载地址
# 下载链接中的 {version} 为 Dumpling 的版本号。例如,v5.2.2 版本的下载链接为
https://download.pingcap.org/tidb-toolkit-v5.2.2-linux-amd64.tar.gz
2.工具内容详情
[root@ncayu8847 tidb-toolkit-v5.2.2-linux-amd64]# cd bin/
[root@ncayu8847 bin]# ll
总用量 535180
-rwxr-xr-x 1 root root 115875760 10月 29 11:26 br
-rwxr-xr-x 1 root root 79583835 10月 29 11:26 dumpling
-rwxr-xr-x 1 root root 12782832 10月 29 11:26 mydumper
-rwxr-xr-x 1 root root 17258378 10月 29 11:26 pd-tso-bench
-rwxr-xr-x 1 root root 71600963 10月 29 11:26 sync_diff_inspector
-rwxr-xr-x 1 root root 129306304 10月 29 11:26 tidb-lightning
-rwxr-xr-x 1 root root 121594400 10月 29 11:26 tidb-lightning-ctl
官方推荐使用br工具进行数据备份与恢复,由于环境限制,目前使用dumpling工具进行备份,使用tidb-lightning工具进行数据恢复。
数据库备份dumpling
备份命令
# 备份命令:
./dumpling -P 4000 -h 192.168.70.133 -u root -p ncayu123456 --filetype sql -B ncayu_test -t 1 --loglevel warn -F 64MiB -o /data1/tidb_tool/backup/ncayu_test1130
# 参数释义:
-P 端口
-h 数据库IP地址
-u 数据库用户
-p 数据库密码
--filetype sql 备份文件以sql类型保存
-B 指定备份的数据库名
-t 用于指定导出的线程数
--loglevel 输出log日志等级
日志级别 {debug,info,warn,error,dpanic,panic,fatal} 默认是"info"
-F 64MiB 指定备份单个sql文件大小
-o /data1/tidb_tool/backup/ncayu_test1130 指定备份文件存储路径
数据库恢复tidb-lightning
启动tidb-lightning
官方建议运行 tidb-lightning。如果直接在命令行中用 nohup 启动程序,可能会因为 SIGHUP 信号而退出,建议把 nohup 放到脚本里面
方法一、先启动程序,在页面去添加导入配置
mkdir /tmp/tidb-lightning
#!/bin/bash
nohup ./tidb-lightning --server-mode --status-addr :8289 > /tmp/tidb-lightning/lightning.log 2>&1 &
方法二、直接将配置文件修改好进行启动
#!/bin/bash
nohup ./tidb-lightning -config tidb-lightning.toml > nohup.out &
tidb-lightning.toml文件内容
[lightning]
# 日志
level = "info"
file = "/data1/tidb_tool/backup/push_log.log"
#[checkpoint]
#enable = true
# 存储断点的数据库名称。
#schema = "tidb_lightning_checkpoint"
# 存储断点的方式。
# - file:存放在本地文件系统。
# - mysql:存放在兼容 MySQL 的数据库服务器。
#driver = "file"
[tikv-importer]
# backend 设置为 local 模式
backend = "local"
# 设置本地临时存储路径
sorted-kv-dir = "/tmp/sorted-kv-dir"
[mydumper]
# Mydumper 源数据目录。
data-source-dir = "/data1/tidb_tool/backup/ncayu_test1201"
[tidb]
# 目标集群的信息。tidb-server 的监听地址,填一个即可。
# 这里注意,需要使用TiDB管理后台的 Cluster Info 中的 TiDB IP 地址
host = "192.168.70.133"
port = 4000
user = "root"
password = "ncayu123456"
# 表架构信息在从 TiDB 的“状态端口”获取。
status-port = 10080
# pd-server 的地址,填一个即可
# 这里注意,需要使用TiDB管理后台的 Cluster Info 中的 PD 信息
pd-addr = "192.168.70.133:2379"
tidb-lightning.toml文件中,对于不同的tidb数据库,需要修改对应数据库的的信息,还有数据内容的路径。
调整TiDB集群的GC时间
如果数据量很大,最好在导入前将gc(垃圾回收时间)改长
调整TiDB集群的GC时间
SELECT * FROM mysql.tidb WHERE VARIABLE_NAME = 'tikv_gc_life_time';
update mysql.tidb set VARIABLE_VALUE = '10h' where VARIABLE_NAME = 'tikv_gc_life_time';
导入完成后改回原值
update mysql.tidb set VARIABLE_VALUE = '10m' where VARIABLE_NAME = 'tikv_gc_life_time';
注意lightning异常退出情况
如果 tidb-lightning 异常退出,集群可能仍处于“导入模式” (import mode),该模式不适用于生产环境。此时可执行以下命令查看当前使用的模式:(工具拷贝到中控机执行下面命令)
tidb-lightning-ctl --fetch-mode
可执行以下命令强制切换回“普通模式” (normal mode):
tidb-lightning-ctl --switch-mode=normal
导入完成
导入完毕后,TiDB Lightning 会自动退出。若导入成功,日志的最后一行会显示
tidb lightning exit。
数据导入过程中的部分日志
nohup.out 文件
[root@ncayu618 bin]# tail -f nohup.out
Verbose debug logs will be written to /data1/tidb_tool/backup/push_log.log
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| # | CHECK ITEM | TYPE | PASSED |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 1 | Source csv files size is proper | performance | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 2 | checkpoints are valid | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 3 | table schemas are valid | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 4 | Cluster is available | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 5 | Lightning has the correct storage permission | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 6 | local disk resources are rich, estimate sorted data size 7.689GiB, local available is 45.29GiB | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
| 7 | Cluster capacity is rich, capacity is 2.402TiB, we need 23.07GiB | critical | true |
+---+------------------------------------------------------------------------------------------------+-------------+--------+
push_log.log日志文件
[2021/12/01 16:34:40.805 +08:00] [INFO] [restore.go:1221] ["add back PD leader®ion schedulers"]
[2021/12/01 16:34:40.805 +08:00] [INFO] [restore.go:1224] ["cleanup task metas"]
[2021/12/01 16:34:42.973 +08:00] [INFO] [restore.go:1173] ["cancel periodic actions"] [do=true]
[2021/12/01 16:34:42.974 +08:00] [INFO] [restore.go:1038] ["switch to normal mode"]
[2021/12/01 16:34:43.310 +08:00] [WARN] [meta_manager.go:843] ["there are unfinished table in table meta table, cleanup skipped."]
[2021/12/01 16:34:43.310 +08:00] [INFO] [restore.go:1488] ["skip full compaction"]
[2021/12/01 16:34:43.318 +08:00] [INFO] [restore.go:1660] ["clean checkpoints start"] [keepAfterSuccess=false] [taskID=1638344968777845215]
[2021/12/01 16:34:43.319 +08:00] [INFO] [restore.go:1667] ["clean checkpoints completed"] [keepAfterSuccess=false] [taskID=1638344968777845215] [takeTime=105.15µs] []
[2021/12/01 16:34:43.319 +08:00] [INFO] [restore.go:431] ["the whole procedure completed"] [takeTime=45m14.509755009s] []
[2021/12/01 16:34:43.320 +08:00] [INFO] [main.go:93] ["tidb lightning exit"]
[2021/12/01 16:34:43.320 +08:00] [INFO] [checksum.go:468] ["service safe point keeper exited"]