TiDB-BR数据备份和恢复工具

备份和恢复(Backup & Restore)

Backup & Restore(简称 BR),它是 TiDB 分布式备份恢复的命令行工具,用于对 TiDB 集群进行数据备份和恢复。

软件下载

curl -sSL -C - -O https://download.pingcap.org/tidb-toolkit-latest-linux-amd64.tar.gz
curl -sSL -C - -O https://download.pingcap.org/tidb-toolkit-latest-linux-amd64.sha256

安装部署(br)

tar -xf tidb-toolkit-latest-linux-amd64.tar.gz -C /tidb/app/bin

适用场景

  • 大数据量
  • 只支持 TiDB v3.1 及以上版本

备份文件类型

备份路径下会生成以下两种类型文件:

  • SST 文件:存储 TiKV 备份下来的数据信息
  • backupmeta 文件:存储本次备份的元信息,包括备份文件数、备份文件的 Key 区间、备份文件大小和备份文件 Hash (sha256) 值

工具使用方式

SQL 语句和br命令行工具进行备份恢复。

SQL命令

在 v4.0.2 及以上版本的 TiDB 支持使用 SQL 语句进行备份及恢复。

前提条件
  • 用户需要 SUPER 权限
  • 对集群中所有的TiKV节点的目标存储有读或写权限
  • 备份不会包含系统表 (mysql.*INFORMATION_SCHEMA.*PERFORMANCE_SCHEMA.* 等) 对象数据
命令语法

backup-usage

restore-usage

命令行工具 (br)

语法

一条 br 命令是由子命令、选项和参数组成的。子命令即不带 - 或者 -- 的字符。选项即以 - 或者 -- 开头的字符。参数即子命令或选项字符后紧跟的、并传递给命令和选项的字符。

br is a TiDB/TiKV cluster backup restore tool.

Usage:
  br [command]

Available Commands:
  backup      backup a TiDB/TiKV cluster
  help        Help about any command
  restore     restore a TiDB/TiKV cluster

Flags:
      --ca string                       CA certificate path for TLS connection
      --cert string                     Certificate path for TLS connection
      --check-requirements              Whether start version check before execute command (default true)
      --checksum                        Run checksum at end of task (default true)
      --gcs.credentials-file string     (experimental) Set the GCS credentials file path
      --gcs.endpoint string             (experimental) Set the GCS endpoint URL
      --gcs.predefined-acl string       (experimental) Specify the GCS predefined acl for objects
      --gcs.storage-class string        (experimental) Specify the GCS storage class for objects
  -h, --help                            help for br
      --key string                      Private key path for TLS connection
      --log-file string                 Set the log file path. If not set, logs will output to temp file (default "/tmp/br.log.2021-01-06T10.15.08+0800")
      --log-format string               Set the log format (default "text")
  -L, --log-level string                Set the log level (default "info")
  -u, --pd strings                      PD address (default [127.0.0.1:2379])
      --ratelimit uint                  The rate limit of the task, MB/s per node
      --s3.acl string                   (experimental) Set the S3 canned ACLs, e.g. authenticated-read
      --s3.endpoint string              (experimental) Set the S3 endpoint URL, please specify the http or https scheme explicitly
      --s3.provider string              (experimental) Set the S3 provider, e.g. aws, alibaba, ceph
      --s3.region string                (experimental) Set the S3 region, e.g. us-east-1
      --s3.sse string                   Set S3 server-side encryption, e.g. aws:kms
      --s3.sse-kms-key-id string        KMS CMK key id to use with S3 server-side encryption.Leave empty to use S3 owned key.
      --s3.storage-class string         (experimental) Set the S3 storage class, e.g. STANDARD
  -c, --send-credentials-to-tikv        Whether send credentials to tikv (default true)
      --status-addr string              Set the HTTP listening address for the status report service. Set to empty string to disable
  -s, --storage string                  specify the url where backup storage, eg, "s3://bucket/path/prefix"
      --switch-mode-interval duration   maintain import mode on TiKV during restore (default 5m0s)
  -V, --version                         Display version information about BR

Use "br [command] --help" for more information about a command.

注意:

  • 在使用 local storage 的时候,备份数据会分散在各个节点的本地文件系统中
  • 不建议在生产环境中备份到本地磁盘,因为在日后恢复的时候,必须手动聚集这些数据才能完成恢复工作
  • 建议在各个节点挂载 NFS 网盘,或者直接备份到 S3 等远端存储中。
命令和子命令

BR 由多层命令组成。目前,BR 包含 backuprestoreversion 三个子命令:

  • br backup 用于备份 TiDB 集群
  • br restore 用于恢复 TiDB 集群

以上三个子命令可能还包含这些子命令:

  • full:可用于备份或恢复全部数据。
  • db:可用于备份或恢复集群中的指定数据库。
  • table:可用于备份或恢复集群指定数据库中的单张表。
常用选项
选项 说明 示例
--pd 用于连接的选项,表示 PD 服务地址 "${PDIP}:2379"
-h/--help 获取所有命令和子命令的使用帮助 br backup --help
-V (或 --version) 检查 BR 版本
--ca 指定 PEM 格式的受信任 CA 的证书文件路径
--cert 指定 PEM 格式的 SSL 证书文件路径
--key 指定 PEM 格式的 SSL 证书密钥文件路径
--status-addr BR 向 Prometheus 提供统计数据的监听地址
备份恢复示例
备份
  • 备份前需确认已将 GC 时间调长,确保备份期间不会因为数据丢失导致中断
  • 备份前需确认 TiDB 集群没有执行 DDL 操作
# 对集群中的全部数据进行备份
br backup full -s local:///tmp/backup --pd "${PDIP}:2379" --log-file backup.log
恢复

恢复操作前,需确认待恢复的 TiKV 集群是全新的集群

br restore full -s local:///tmp/backup --pd "${PDIP}:2379" --log-file restore.log
原文地址:https://www.cnblogs.com/binliubiao/p/14300249.html