题外话,最近一阵子 MinIO 突然宣布进入 “维护模式”,但是不影响本地测试,刚开始安装的时候,我就在web页面没找到创建用户等的功能,以为是姿势不对,原来 MinIO 很早之前就从web页面上把这个功能撤掉了。闲话不多说,继续 TiDB BR 本地备份实战验证(二)。

参照官方:br 命令行手册/

备份集群快照

执行 br backup full 命令,可以备份 TiDB 最新的或者指定时间点的快照数据。执行 br backup full --help 可获取该命令的使用帮助。

1
2
3
4
5
6
br backup full \
--pd "${PD_IP}:2379" \
--backupts '2022-09-08 13:30:00 +08:00' \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file backupfull.log

以上命令中:

  • --backupts:快照对应的物理时间点。如果该快照的数据已经被 GC,那么 br backup命令会报错退出;如果没有指定该参数,br 命令行工具会选取备份开始的时间点所对应的快照。
  • --ratelimit每个 TiKV 执行备份任务的速度上限(单位 MiB/s)。
  • --log-file:备份日志写入的目标文件。

注意

BR 工具已支持自适应 GC,会自动将 backupTS(默认是最新的 PD timestamp)注册到 PD 的 safePoint,保证 TiDB 的 GC Safe Point 在备份期间不会向前移动,即可避免手动设置 GC。

备份期间终端会显示进度条,效果如下。当进度条达到 100% 时,表示备份完成。

1
Full Backup <---------/................................................> 17.12%.

备份验证,本次在 基于 MinIO S3 兼容存储的 TiDB BR 本地备份实战验证(一) 已经验证过。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@tihtap ~]#
PD_IP="10.0.0.15"
MINIO_IP="10.0.0.15"
ACCESS_KEY="tidb"
SECRET_KEY="SecurePassword123"
BUCKET_NAME="tidb-backup"
[tidb@tihtap ~]$
[tidb@tihtap ~]$
[tidb@tihtap ~]$ tiup br backup full \
--pd "$PD_IP:2379" \
--storage "s3://$BUCKET_NAME/snapshot-$(date +%F%T)?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--log-file backupfull_$(date +%F).log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[tidb@tihtap br]$ tiup br backup full \
--pd "$PD_IP:2379" \
--storage "s3://$BUCKET_NAME/snapshot-$(date +%F%T)?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--log-file backupfull_$(date +%F).log
Checking updates for component br...
A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br backup full --pd 10.0.0.15:2379 --storage s3://tidb-backup/snapshot-2025-12-2411:03:04?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --log-file backupfull_2025-12-24.log
Detail BR log in backupfull_2025-12-24.log
Full Backup <-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Checksum <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2025/12/24 11:03:09.505 +08:00] [INFO] [collector.go:77] ["Full Backup success summary"] [total-ranges=23] [ranges-succeed=23] [ranges-failed=0] [backup-checksum=23.798279ms] [backup-total-ranges=102] [backup-total-regions=102] [total-take=3.743376507s] [total-kv-size=3.235MB] [average-speed=864.1kB/s] [backup-data-size(after-compressed)=606.4kB] [Size=606437] [BackupTS=463089273635864577] [total-kv=45982]

备份 TiDB 集群指定库表的数据

br 工具支持只备份集群快照和增量数据中指定库或表的局部数据。在快照备份和增量数据备份的基础上,该功能可过滤掉不需要的数据,只备份关键业务的数据。

备份单个数据库的数据

执行 br backup db 命令,可备份集群中指定单个数据库的数据。

下面是将数据库 test 备份到 Amazon S3 的示例:

1
2
3
4
5
6
br backup db \
--pd "${PD_IP}:2379" \
--db test \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file backuptable.log

db 子命令的选项为 --db,用来指定数据库名。其他选项的含义与备份 TiDB 集群快照相同。

本地验证测试:备份titest 数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[tidb@tihtap ~]$ tiup br backup db \
--pd "${PD_IP}:2379" \
--db titest \
--storage "s3://$BUCKET_NAME/snapshot-$(date +%F%T)?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--ratelimit 128 \
--log-file backuptable_titest$(date +%F).log

A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br backup db --pd 10.0.0.15:2379 --db titest --storage s3://tidb-backup/snapshot-2025-12-2415:27:47?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --ratelimit 128 --log-file backuptable_titest2025-12-24.log
Detail BR log in backuptable_titest2025-12-24.log
[2025/12/24 15:27:47.983 +08:00] [WARN] [backup.go:314] ["setting `--ratelimit` and `--concurrency` at the same time, ignoring `--concurrency`: `--ratelimit` forces sequential (i.e. concurrency = 1) backup"] [ratelimit=134.2MB/s] [concurrency-specified=4]
Database Backup <-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Checksum <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2025/12/24 15:27:51.601 +08:00] [INFO] [collector.go:77] ["Database Backup success summary"] [total-ranges=1] [ranges-succeed=1] [ranges-failed=0] [backup-checksum=2.661138ms] [backup-total-ranges=3] [backup-total-regions=3] [total-take=3.628369435s] [Size=471116] [BackupTS=463093437059301377] [total-kv=40000] [total-kv-size=2.551MB] [average-speed=703.1kB/s] [backup-data-size(after-compressed)=471.1kB]

备份单张表的数据

执行 br backup table 命令,可备份集群中指定单张表的数据。

下面是将表 test.usertable 备份到 Amazon S3 的示例:

1
2
3
4
5
6
7
br backup table \
--pd "${PD_IP}:2379" \
--db test \
--table usertable \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file backuptable.log

table 子命令有 --db--table 两个选项,分别用来指定数据库名和表名。其他选项的含义与备份 TiDB 集群快照相同。

本地实战验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[tidb@tihtap ~]$ tiup br backup table \
--pd "${PD_IP}:2379" \
--db titest \
--table users \
--storage "s3://$BUCKET_NAME/snapshot-$(date +%F%T)?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--ratelimit 128 \
--log-file backuptable_titest.users.log

A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br backup table --pd 10.0.0.15:2379 --db titest --table users --storage s3://tidb-backup/snapshot-2025-12-2415:29:39?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --ratelimit 128 --log-file backuptable_titest.users.log
Detail BR log in backuptable_titest.users.log
[2025/12/24 15:29:40.293 +08:00] [WARN] [backup.go:314] ["setting `--ratelimit` and `--concurrency` at the same time, ignoring `--concurrency`: `--ratelimit` forces sequential (i.e. concurrency = 1) backup"] [ratelimit=134.2MB/s] [concurrency-specified=4]
Table Backup <----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Checksum <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2025/12/24 15:29:43.916 +08:00] [INFO] [collector.go:77] ["Table Backup success summary"] [total-ranges=1] [ranges-succeed=1] [ranges-failed=0] [backup-checksum=2.396816ms] [backup-total-ranges=1] [backup-total-regions=1] [total-take=3.62623258s] [backup-data-size(after-compressed)=471.1kB] [Size=471116] [BackupTS=463093466498072577] [total-kv=40000] [total-kv-size=2.551MB] [average-speed=703.5kB/s]

使用表库过滤功能备份多张表的数据

如果你需要以更复杂的过滤条件来备份多个库或表,执行 br backup full 命令,并使用 --filter-f 来指定表库过滤规则。

下面是将所有符合 db*.tbl* 条件的表的数据备份到 Amazon S3 的示例:

1
2
3
4
5
6
br backup full \
--pd "${PD_IP}:2379" \
--filter 'db*.tbl*' \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file backupfull.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
mysql> use titest2
Database changed
mysql> create table users2(i int);
Query OK, 0 rows affected (0.12 sec)

mysql> create table user3(i int);
Query OK, 0 rows affected (0.13 sec)

mysql> show tables;
+-------------------+
| Tables_in_titest2 |
+-------------------+
| user3 |
| users2 |
+-------------------+
2 rows in set (0.00 sec)

mysql> use titest
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_titest |
+------------------+
| orders |
| products |
| users |
+------------------+
3 rows in set (0.00 sec)

mysql>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[tidb@tihtap ~]$ tiup br backup full \
--pd "${PD_IP}:2379" \
--filter 'ti*.user*' \
--storage "s3://$BUCKET_NAME/snapshot-$(date +%F%T)?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--ratelimit 128 \
--log-file backupfull_tidb_usert.log

A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br backup full --pd 10.0.0.15:2379 --filter ti*.user* --storage s3://tidb-backup/snapshot-2025-12-2415:33:51?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --ratelimit 128 --log-file backupfull_tidb_usert.log
Detail BR log in backupfull_tidb_usert.log
[2025/12/24 15:33:52.231 +08:00] [WARN] [backup.go:314] ["setting `--ratelimit` and `--concurrency` at the same time, ignoring `--concurrency`: `--ratelimit` forces sequential (i.e. concurrency = 1) backup"] [ratelimit=134.2MB/s] [concurrency-specified=4]
Full Backup <-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Checksum <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2025/12/24 15:33:55.845 +08:00] [INFO] [collector.go:77] ["Full Backup success summary"] [total-ranges=1] [ranges-succeed=1] [ranges-failed=0] [backup-checksum=3.560824ms] [backup-total-ranges=3] [backup-total-regions=3] [total-take=3.618117846s] [total-kv=40000] [total-kv-size=2.551MB] [average-speed=705.1kB/s] [backup-data-size(after-compressed)=471.1kB] [Size=471116] [BackupTS=463093532545253377]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| METRICS_SCHEMA |
| PERFORMANCE_SCHEMA |
| liups |
| mysql |
| test |
| titest |
| titest2 |
+--------------------+
8 rows in set (0.00 sec)

mysql> drop database titest2;
Query OK, 0 rows affected (0.29 sec)

mysql>

恢复快照备份数据

执行 br restore full 命令,可将集群恢复到快照备份对应的数据状态。

1
2
3
4
5
6
br restore full \
--pd "${PD_IP}:2379" \
--with-sys-table \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file restorefull.log

以上命令中,

  • --with-sys-table:恢复集群数据的同时恢复部分系统表的数据,包括恢复账号权限数据和 SQL Binding 信息,但暂不支持恢复统计信息 (mysql.stat_*) 和系统参数 (mysql.tidb, mysql.global_variables) 等信息,更多信息详见恢复 mysql 数据库下的表
  • --ratelimit每个 TiKV 执行恢复任务的速度上限(单位 MiB/s)。
  • --log-file:备份日志写入的目标文件。

恢复期间终端会显示进度条,效果如下。当进度条达到 100% 时,表示恢复完成。在完成恢复后,br 工具为了确保数据安全性,还会校验恢复数据。

1
Full Restore <---------/...............................................> 17.12%.

查看备份目录

1
2
3
4
5
6
7
8
9
mc ls myminio/tidb-backup/
mc ls myminio/tidb-backup/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-11-1906:41:10/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-12-2411:03:04/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-12-2415:27:47/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-12-2415:29:39/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-12-2415:33:51/
[2025-12-24 15:42:26 CST] 0B snapshot-2025-12-2415:40:16/
[2025-12-24 15:42:26 CST] 0B v1/
1
2
3
4
5
6
br restore full \
--pd "${PD_IP}:2379" \
--with-sys-table \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file restorefull.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
tiup br restore full \
--pd "${PD_IP}:2379" \
--with-sys-table \
--storage "s3://$BUCKET_NAME/snapshot-2025-12-2411:03:04/?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" \
--ratelimit 128 \
--log-file restorefull1103.log

A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br restore full --pd 10.0.0.15:2379 --with-sys-table --storage s3://tidb-backup/snapshot-2025-12-2411:03:04/?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --ratelimit 128 --log-file restorefull1103.log
Detail BR log in restorefull1103.log
[2025/12/24 15:56:35.552 +08:00] [INFO] [collector.go:77] ["Full Restore failed summary"] [total-ranges=0] [ranges-succeed=0] [ranges-failed=0]
#######################################################################
# the target cluster is not fresh, cannot restore.
# you can drop existing databases and tables and start restore again
#######################################################################
Error: user db/tables: titest.users, titest.products, titest.orders, liups.: [BR:Restore:ErrRestoreNotFreshCluster]cluster is not fresh
[tidb@tihtap br]$
[tidb@tihtap br]$
[tidb@tihtap br]$ mysql -uroot -p -h10.0.0.15 -P4002 -pPassword123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 415
Server version: 5.7.25-TiDB-v7.1.3 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> drop database titest ;
Query OK, 0 rows affected (0.27 sec)

mysql> drop database liups.;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 20 near "."
mysql> drop database liups;
Query OK, 0 rows affected (0.28 sec)

mysql> exit
Bye
[tidb@tihtap br]$ tiup br restore full --pd "${PD_IP}:2379" --with-sys-table --storage "s3://$BUCKET_NAME/snapshot-2025-12-2411:03:04/?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true" --ratelimit 128 --log-file restorefull1103.log

A new version of br is available: v8.5.3 -> v8.5.4

To update this component: tiup update br
To update all components: tiup update --all

Starting component br: /home/tidb/.tiup/components/br/v8.5.3/br restore full --pd 10.0.0.15:2379 --with-sys-table --storage s3://tidb-backup/snapshot-2025-12-2411:03:04/?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true --ratelimit 128 --log-file restorefull1103.log
Detail BR log in restorefull1103.log
Full Restore <----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2025/12/24 15:58:14.165 +08:00] [INFO] [collector.go:77] ["Full Restore success summary"] [total-ranges=18] [ranges-succeed=18] [ranges-failed=0] [restore-ranges=6] [total-take=4.785518951s] [BackupTS=463089273635864577] [RestoreTS=463093914541228033] [total-kv=40005] [total-kv-size=2.552MB] [average-speed=533.2kB/s] [restore-data-size(after-compressed)=480.3kB] [Size=480254]

恢复的时候必须是集群必须是 fresh 的,如果在源库上恢复,需要把已经已经存在的数据库删除。

恢复备份数据中指定库表的数据

br 命令行工具支持只恢复备份数据中指定库/表的局部数据。该功能在恢复过程中过滤掉不需要的数据,可以用于往 TiDB 集群上恢复指定库/表的数据。

恢复单个数据库的数据

执行 br restore db 命令,可将单个数据库恢复到对应的状态。

示例:恢复 S3 中的库 test 的数据:

1
2
3
4
5
6
br restore db \
--pd "${PD_IP}:2379" \
--db "test" \
--ratelimit 128 \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--log-file restore_db.log

以上命令中 --db 选项指定了需要恢复的数据库名字。其余选项的含义与恢复快照备份数据相同。

注意

由于备份数据的元文件 backupmeta 记录了数据库名 --db,因此只能将数据恢复到同名的数据库,否则无法恢复成功。推荐把备份文件恢复到另一个集群的同名数据库中。

恢复单张表的数据

执行 br restore table 命令,可将单张表的数据恢复到对应的状态。

下面是恢复 Amazon S3 中 test.usertable 表数据的示例:

1
2
3
4
5
6
7
br restore table \
--pd "${PD_IP}:2379" \
--db "test" \
--table "usertable" \
--ratelimit 128 \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--log-file restore_table.log

以上命令中 --table 选项指定了需要恢复的表名。其余选项的含义与恢复单个数据库相同。

使用表库功能过滤恢复数据

如果你需要用复杂的过滤条件来恢复多个表,执行 br restore full 命令,并用 --filter-f 指定使用表库过滤

下面是恢复 Amazon S3 中符合 db*.tbl* 条件的表的数据的示例:

1
2
3
4
5
br restore full \
--pd "${PD_IP}:2379" \
--filter 'db*.tbl*' \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--log-file restorefull.log

确定备份策略

为了满足业务最小数据丢失、快速恢复、一个月内任意时间点审计需求,你可以制定如下备份策略:

  • 运行日志备份,持续不断备份数据库数据变更;
  • 每隔两天在零点左右进行一次快照备份;
  • 保存 30 天内的快照备份和日志备份数据,清理超过 30 天的备份数据。

执行日志备份

启动日志备份任务后,日志备份进程会在 TiKV 集群运行,持续不断将数据库变更数据备份到 S3 中。日志备份任务启动命令:

1
2
tiup br log start --task-name=pitr --pd="${PD_IP}:2379" \
--storage='s3://tidb-pitr-bucket/backup-data/log-backup'

启动日志备份任务后,可以查询日志备份任务状态:

1
2
3
4
5
6
7
8
9
10
11
tiup br log status --task-name=pitr --pd="${PD_IP}:2379"

● Total 1 Tasks.
> #1 <
name: pitr
status: ● NORMAL
start: 2022-05-13 11:09:40.7 +0800
end: 2035-01-01 00:00:00 +0800
storage: s3://tidb-pitr-bucket/backup-data/log-backup
speed(est.): 0.00 ops/s
checkpoint[global]: 2022-05-13 11:31:47.2 +0800; gap=4m53s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[tidb@instance-20250610-1641 ~]$ tiup br:v7.1.3 log start --task-name=pitr --pd="${PD_IP}:2379" --storage="s3://$BUCKET_NAME/tidb_log-backup/?access-key=$ACCESS_KEY&secret-access-key=$SECRET_KEY&endpoint=http://$MINIO_IP:9000&force-path-style=true"
Starting component br: /home/tidb/.tiup/components/br/v7.1.3/br log start --task-name=pitr --pd=10.0.0.15:2379 --storage=s3://tidb-backup/tidb_log-backup/?access-key=tidb&secret-access-key=SecurePassword123&endpoint=http://10.0.0.15:9000&force-path-style=true
Detail BR log in /tmp/br.log.2025-12-24T16.43.53+0800
[2025/12/24 16:43:53.473 +08:00] [INFO] [collector.go:77] ["log start"] [streamTaskInfo="{taskName=pitr,startTs=463094561997848578,endTS=999999999999999999,tableFilter=*.*}"] [pausing=false] [rangeCount=2]
[2025/12/24 16:43:55.690 +08:00] [INFO] [collector.go:77] ["log start success summary"] [total-ranges=0] [ranges-succeed=0] [ranges-failed=0] [total-take=2.3357859s]
[tidb@instance-20250610-1641 ~]$ tiup br:v7.1.3 log status --pd="${PD_IP}:2379"
Starting component br: /home/tidb/.tiup/components/br/v7.1.3/br log status --pd=10.0.0.15:2379
Detail BR log in /tmp/br.log.2025-12-24T16.44.02+0800
● Total 1 Tasks.
> #1 <
name: pitr
status: ● NORMAL
start: 2025-12-24 16:39:19.379 +0800
end: 2090-11-18 22:07:45.624 +0800
storage: s3://tidb-backup/tidb_log-backup
speed(est.): 0.00 ops/s
checkpoint[global]: 2025-12-24 16:39:19.379 +0800; gap=4m44s

查看 日志备份状态:tiup br:v7.1.3 log status –pd=”${PD_IP}:2379”

1
2
3
4
5
6
7
8
9
10
11
12
tiup br:v7.1.3 log status --pd="${PD_IP}:2379"
Starting component br: /home/tidb/.tiup/components/br/v7.1.3/br log status --pd=10.0.0.15:2379
Detail BR log in /tmp/br.log.2025-12-24T16.44.02+0800
● Total 1 Tasks.
> #1 <
name: pitr
status: ● NORMAL
start: 2025-12-24 16:39:19.379 +0800
end: 2090-11-18 22:07:45.624 +0800
storage: s3://tidb-backup/tidb_log-backup
speed(est.): 0.00 ops/s
checkpoint[global]: 2025-12-24 16:39:19.379 +0800; gap=4m44s

停止备份日志:

1
tiup br:v7.1.3 log stop --task-name=pitr --pd="${PD_IP}:2379"

执行快照备份

通过自动化运维工具(如 crontab)设置定期的快照备份任务,例如:每隔两天在零点左右进行一次快照(全量)备份。下面是两次备份的示例:

  • 在 2022/05/14 00:00:00 执行一次快照备份:

    1
    2
    3
    tiup br backup full --pd="${PD_IP}:2379" \
    --storage='s3://tidb-pitr-bucket/backup-data/snapshot-20220514000000' \
    --backupts='2022/05/14 00:00:00 +08:00'
  • 在 2022/05/16 00:00:00 执行一次快照备份:

    1
    2
    3
    tiup br backup full --pd="${PD_IP}:2379" \
    --storage='s3://tidb-pitr-bucket/backup-data/snapshot-20220516000000' \
    --backupts='2022/05/16 00:00:00 +08:00'

执行 PITR

假设你接到需求,要准备一个集群查询 2022/05/15 18:00:00 时间点的用户数据。此时,你可以制定 PITR 方案,恢复 2022/05/14 的快照备份和该快照到 2022/05/15 18:00:00 之间的日志备份数据,从而收集到目标数据。执行命令如下:

1
2
3
4
5
6
7
8
9
10
tiup br restore point --pd="${PD_IP}:2379" \
--storage='s3://tidb-pitr-bucket/backup-data/log-backup' \
--full-backup-storage='s3://tidb-pitr-bucket/backup-data/snapshot-20220514000000' \
--restored-ts '2022-05-15 18:00:00+0800'

Full Restore <--------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2022/05/29 18:15:39.132 +08:00] [INFO] [collector.go:69] ["Full Restore success summary"] [total-ranges=12] [ranges-succeed=xxx] [ranges-failed=0] [split-region=xxx.xxxµs] [restore-ranges=xxx] [total-take=xxx.xxxs] [restore-data-size(after-compressed)=xxx.xxx] [Size=xxxx] [BackupTS={TS}] [total-kv=xxx] [total-kv-size=xxx] [average-speed=xxx]
Restore Meta Files <--------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Restore KV Files <----------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2022/05/29 18:15:39.325 +08:00] [INFO] [collector.go:69] ["restore log success summary"] [total-take=xxx.xx] [restore-from={TS}] [restore-to={TS}] [total-kv-count=xxx] [total-size=xxx]

清理过期备份数据

通过自动化运维工具(如 crontab) 每两天定期清理过期备份数据的任务。

下面是执行过期备份数据清理任务:

  • 删除早于 2022/05/14 00:00:00 的快照备份

    1
    rm s3://tidb-pitr-bucket/backup-data/snapshot-20220514000000
  • 删除早于 2022/05/14 00:00:00 的日志备份数据

    1
    tiup br log truncate --until='2022-05-14 00:00:00 +0800' --storage='s3://tidb-pitr-bucket/backup-data/log-backup'

原文作者: liups.com

原文链接: http://liups.com/posts/2a25b561/

许可协议: 知识共享署名-非商业性使用 4.0 国际许可协议