TiDB Snapshot Backup and Restore Command Manual
This document describes the commands of TiDB snapshot backup and restore according to the application scenarios, including:
- Back up cluster snapshots
- Back up a database
- Back up a table
- Back up multiple tables with table filter
- Encrypt the backup data
- Restore cluster snapshots
- Restore a database
- Restore a table
- Restore multiple tables with table filter
- Restore encrypted snapshots
For more information about snapshot backup and restore, refer to:
Back up cluster snapshots
You can back up the latest or specified snapshot of the TiDB cluster using the br backup full
command. For more information about the command, run the br backup full --help
command.
br backup full \
--pd "${PD_IP}:2379" \
--backupts '2022-09-08 13:30:00' \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file backupfull.log
In the preceding command:
--backupts
: The time point of the snapshot. The format can be TSO or timestamp, such as400036290571534337
or2018-05-11 01:42:23
. If the data of this snapshot is garbage collected, thebr backup
command returns an error and 'br' exits. If you leave this parameter unspecified,br
picks the snapshot corresponding to the backup start time.--ratelimit
: The maximum speed per TiKV performing backup tasks. The unit is in MiB/s.--log-file
: The target file wherebr
log is written.
During backup, a progress bar is displayed in the terminal, as shown below. When the progress bar advances to 100%, the backup is complete.
Full Backup <---------/................................................> 17.12%.
Back up a database or a table
Backup & Restore (BR) supports backing up partial data of a specified database or table from a cluster snapshot or incremental data backup. This feature allows you to filter out unwanted data from snapshot backup and incremental data backup, and back up only business-critical data.
Back up a database
To back up a database in a cluster, run the br backup db
command.
The following example backs up the test
database to Amazon S3:
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
In the preceding command, --db
specifies the database name, and other parameters are the same as those in Back up TiDB cluster snapshots.
Back up a table
To back up a table in a cluster, run the br backup table
command.
The following example backs up the test.usertable
table to Amazon S3:
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
In the preceding command, --db
and --table
specify the database name and table name respectively, and other parameters are the same as those in Back up TiDB cluster snapshots.
Back up multiple tables with table filter
To back up multiple tables with more criteria, run the br backup full
command and specify the table filters with --filter
or -f
.
The following example backs up tables that match the db*.tbl*
filter rule to Amazon S3:
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
Encrypt the backup data
BR supports encrypting backup data at the backup side and at the storage side when backing up to Amazon S3. You can choose either encryption method as required.
Since TiDB v5.3.0, you can encrypt backup data by configuring the following parameters:
--crypter.method
: Encryption algorithm, which can beaes128-ctr
,aes192-ctr
, oraes256-ctr
. The default value isplaintext
, indicating that data is not encrypted.--crypter.key
: Encryption key in hexadecimal string format. It is a 128-bit (16 bytes) key for the algorithmaes128-ctr
, a 24-byte key for the algorithmaes192-ctr
, and a 32-byte key for the algorithmaes256-ctr
.--crypter.key-file
: The key file. You can directly pass in the file path where the key is stored as a parameter without passing in thecrypter.key
.
The following is an example:
br backup full\
--pd ${PD_IP}:2379 \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--crypter.method aes128-ctr \
--crypter.key 0123456789abcdef0123456789abcdef
Restore cluster snapshots
You can restore a TiDB cluster snapshot by running the br restore full
command.
br restore full \
--pd "${PD_IP}:2379" \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--ratelimit 128 \
--log-file restorefull.log
In the preceding command:
--ratelimit
: The maximum speed per TiKV performing backup tasks. The unit is in MiB/s.--log-file
: The target file where thebr
log is written.
During restore, a progress bar is displayed in the terminal as shown below. When the progress bar advances to 100%, the restore task is completed. Then br
will verify the restored data to ensure data security.
Full Restore <---------/...............................................> 17.12%.
Restore a database or a table
You can use br
to restore partial data of a specified database or table from backup data. This feature allows you to filter out data that you do not need during the restore.
Restore a database
To restore a database to a cluster, run the br restore db
command.
The following example restores the test
database from the backup data to the target cluster:
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
In the preceding command, --db
specifies the name of the database to be restored and other parameters are the same as those in Restore TiDB cluster snapshots.
Restore a table
To restore a single table to a cluster, run the br restore table
command.
The following example restores the test.usertable
table from Amazon S3 to the target cluster:
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
In the preceding command, --table
specifies the name of the table to be restored, and other parameters are the same as those in Restore a database.
Restore multiple tables with table filter
To restore multiple tables with more complex filter rules, run the br restore full
command and specify the table filters with --filter
or -f
.
The following example restores tables that match the db*.tbl*
filter rule from Amazon S3 to the target cluster:
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
Restore encrypted snapshots
After encrypting the backup data, you need to pass in the corresponding decryption parameters to restore the data. Ensure that the decryption algorithm and key are correct. If the decryption algorithm or key is incorrect, the data cannot be restored. The following is an example:
br restore full\
--pd "${PD_IP}:2379" \
--storage "s3://${backup_collection_addr}/snapshot-${date}?access-key=${access-key}&secret-access-key=${secret-access-key}" \
--crypter.method aes128-ctr \
--crypter.key 0123456789abcdef0123456789abcdef