備份和恢復(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.*
等) 對象數據
命令語法
命令行工具 (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 包含 backup
、restore
和 version
三個子命令:
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