TiDB Log Backup and PITR Command Manual

This document describes the commands used in TiDB log backup and point-in-time recovery (PITR).

For more information about log backup and PITR, refer to:

Perform log backup

You can start and manage log backup using the tiup br log command.

  1. tiup br log --help
  2. backup stream log from TiDB/TiKV cluster
  3. Usage:
  4. br log [command]
  5. Available Commands:
  6. metadata get the metadata of log dir
  7. pause pause a log backup task
  8. resume resume a log backup task
  9. start start a log backup task
  10. status get status for the log backup task
  11. stop stop a log backup task
  12. truncate truncate the log data until sometime

Each subcommand is described as follows:

  • tiup br log start: start a log backup task.
  • tiup br log status: query the status of the log backup task.
  • tiup br log pause: pause a log backup task.
  • tiup br log resume: resume a paused log backup task.
  • tiup br log stop: stop a log backup task and delete the task metadata.
  • tiup br log truncate: clean up the log backup data from the backup storage.
  • tiup br log metadata: query the metadata of the log backup data.

Start a backup task

You can run the tiup br log start command to start a log backup task. This task runs in the background of your TiDB cluster and automatically backs up the change log of KV storage to the backup storage.

Run tiup br log start --help to see the help information:

  1. tiup br log start --help
  2. start a log backup task
  3. Usage:
  4. br log start [flags]
  5. Flags:
  6. -h, --help help for start
  7. --start-ts string usually equals last full backupTS, used for backup log. Default value is current ts. support TSO or datetime, e.g. '400036290571534337' or '2018-05-11 01:42:23+0800'.
  8. --task-name string The task name for the backup log task.
  9. Global Flags:
  10. --ca string CA certificate path for TLS connection
  11. --cert string Certificate path for TLS connection
  12. --key string Private key path for TLS connection
  13. -u, --pd strings PD address (default [127.0.0.1:2379])
  14. -s, --storage string specify the url where backup storage, eg, "s3://bucket/path/prefix"

The example output only shows the common parameters. These parameters are described as follows:

  • --start-ts: specifies the start timestamp for the log backup. If this parameter is not specified, the backup program uses the current time as start-ts.
  • task-name: specifies the task name for the log backup. This name is also used to query, pause, and resume the backup task.
  • --ca, --cert, --key: specifies the mTLS encryption method to communicate with TiKV and PD.
  • --pd: specifies the PD address for the backup cluster. BR needs to access PD to start the log backup task.
  • --storage: specifies the backup storage address. Currently, BR supports Amazon S3, Google Cloud Storage (GCS), or Azure Blob Storage as the storage for log backup. The preceding command uses Amazon S3 as an example. For details, see URI Formats of External Storage Services.

Usage example:

  1. tiup br log start --task-name=pitr --pd="${PD_IP}:2379" \
  2. --storage='s3://backup-101/logbackup?access-key=${access-key}&secret-access-key=${secret-access-key}"'

Query the backup status

You can run the tiup br log status command to query the backup status.

Run tiup br log status --help to see the help information:

  1. tiup br log status --help
  2. get status for the log backup task
  3. Usage:
  4. br log status [flags]
  5. Flags:
  6. -h, --help help for status
  7. --json Print JSON as the output.
  8. --task-name string The task name for backup stream log. If default, get status of all of tasks (default "*")
  9. Global Flags:
  10. --ca string CA certificate path for TLS connection
  11. --cert string Certificate path for TLS connection
  12. --key string Private key path for TLS connection
  13. -u, --pd strings PD address (default [127.0.0.1:2379])

In the example output, task-name is used to specify the name of the backup task. The default value is *, which means querying the status of all tasks.

Usage example:

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

Expected output:

  1. Total 1 Tasks.
  2. > #1 <
  3. name: pitr
  4. status: NORMAL
  5. start: 2022-07-14 20:08:03.268 +0800
  6. end: 2090-11-18 22:07:45.624 +0800
  7. storage: s3://backup-101/logbackup
  8. speed(est.): 0.82 ops/s
  9. checkpoint[global]: 2022-07-25 22:52:15.518 +0800; gap=2m52s

The output fields are described as follows:

  • status: the status of the backup task, which can be NORMAL, ERROR, or PAUSE.
  • start: the start time of the backup task. It is the start-ts value specified when the backup task is started.
  • storage: the backup storage address.
  • speed: the total QPS of the backup task. QPS means the number of logs backed per second.
  • checkpoint [global]: all data before this checkpoint is backed up to the backup storage. This is the latest timestamp available for restoring the backup data.
  • error [store]: the error the log backup program encounters on the storage node.

Pause and resume a backup task

You can run the tiup br log pause command to pause a running backup task.

Run tiup br log pause --help to see the help information:

  1. tiup br log pause --help
  2. pause a log backup task
  3. Usage:
  4. br log pause [flags]
  5. Flags:
  6. --gc-ttl int the TTL (in seconds) that PD holds for BR's GC safepoint (default 86400)
  7. -h, --help help for status
  8. --task-name string The task name for backup stream log.
  9. Global Flags:
  10. --ca string CA certificate path for TLS connection
  11. --cert string Certificate path for TLS connection
  12. --key string Private key path for TLS connection
  13. -u, --pd strings PD address (default [127.0.0.1:2379])

Log Backup and PITR Command Manual - 图1

Note

  • After the log backup task is paused, to prevent the MVCC data that generates the change log from being deleted, the backup program automatically sets the current backup checkpoint as the service safepoint, which retains MVCC data within the latest 24 hours. If the backup task is paused for more than 24 hours, the corresponding data is garbage collected and is not backed up.
  • Retaining too much MVCC data has a negative impact on the storage capacity and performance of the TiDB cluster. Therefore, it is recommended to resume the backup task in time.

Usage example:

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

You can run the tiup br log resume command to resume a paused backup task.

Run tiup br log resume --help to see the help information:

  1. tiup br log resume --help
  2. resume a log backup task
  3. Usage:
  4. br log resume [flags]
  5. Flags:
  6. -h, --help help for status
  7. --task-name string The task name for backup stream log.
  8. Global Flags:
  9. --ca string CA certificate path for TLS connection
  10. --cert string Certificate path for TLS connection
  11. --key string Private key path for TLS connection
  12. -u, --pd strings PD address (default [127.0.0.1:2379])

After the backup task is paused for more than 24 hours, running tiup br log resume reports an error, and BR prompts that backup data is lost. To handle this error, refer to Backup & Restore FAQs.

Usage example:

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

Stop and restart a backup task

You can stop a log backup task by running the tiup br log stop command and restart a backup task that is stopped by using the original --storage directory.

Stop a backup task

You can run the tiup br log stop command to stop a log backup task. This command cleans up the task metadata in the backup cluster.

Run tiup br log stop --help to see the help information:

  1. tiup br log stop --help
  2. stop a log backup task
  3. Usage:
  4. br log stop [flags]
  5. Flags:
  6. -h, --help help for status
  7. --task-name string The task name for the backup log task.
  8. Global Flags:
  9. --ca string CA certificate path for TLS connection
  10. --cert string Certificate path for TLS connection
  11. --key string Private key path for TLS connection
  12. -u, --pd strings PD address (default [127.0.0.1:2379])

Log Backup and PITR Command Manual - 图2

Note

Use this command with caution. If you need to pause a log backup task, use tiup br log pause and tiup br log resume instead.

Usage example:

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

Restart a backup task

After running the tiup br log stop command to stop a log backup task, you can create a new log backup task in another --storage directory or restart the log backup task in the original --storage directory by running the tiup br log start command. If you restart the task in the original --storage directory, pay attention to the following points:

  • Parameters of the --storage directory for restarting a task must be the same as the task that is stopped.
  • The --start-ts does not need to be specified. BR automatically starts the backup from the last backup checkpoint.
  • If the task is stopped for a long time and multiple versions of the data have been garbage collected, the error BR:Backup:ErrBackupGCSafepointExceeded is reported when you attempt to restart the task. In this case, you have to create a new log backup task in another --storage directory.

Clean up backup data

You can run the tiup br log truncate command to clean up the outdated or no longer needed log backup data.

Run tiup br log truncate --help to see the help information:

  1. tiup br log truncate --help
  2. truncate the incremental log until sometime.
  3. Usage:
  4. br log truncate [flags]
  5. Flags:
  6. --dry-run Run the command but don't really delete the files.
  7. -h, --help help for truncate
  8. --until string Remove all backup data until this TS.(support TSO or datetime, e.g. '400036290571534337' or '2018-05-11 01:42:23+0800'.)
  9. -y, --yes Skip all prompts and always execute the command.
  10. Global Flags:
  11. -s, --storage string specify the url where backup storage, eg, "s3://bucket/path/prefix"

This command only accesses the backup storage and does not access the TiDB cluster. Some parameters are described as follows:

  • --dry-run: run the command but do not really delete the files.
  • --until: delete all log backup data before the specified timestamp.
  • --storage: the backup storage address. Currently, BR supports Amazon S3, GCS, or Azure Blob Storage as the storage for log backup. For details, see URI Formats of External Storage Services.

Usage example:

  1. tiup br log truncate --until='2022-07-26 21:20:00+0800' \
  2. –-storage='s3://backup-101/logbackup?access-key=${access-key}&secret-access-key=${secret-access-key}"'

Expected output:

  1. Reading Metadata... DONE; take = 277.911599ms
  2. We are going to remove 9 files, until 2022-07-26 21:20:00.0000.
  3. Sure? (y/N) y
  4. Clearing data files... DONE; take = 43.504161ms, kv-count = 53, kv-size = 4573(4.573kB)
  5. Removing metadata... DONE; take = 24.038962ms

View the backup metadata

You can run the tiup br log metadata command to view the backup metadata in the storage system, such as the earliest and latest timestamp that can be restored.

Run tiup br log metadata --help to see the help information:

  1. tiup br log metadata --help
  2. get the metadata of log backup storage
  3. Usage:
  4. br log metadata [flags]
  5. Flags:
  6. -h, --help help for metadata
  7. Global Flags:
  8. -s, --storage string specify the url where backup storage, eg, "s3://bucket/path/prefix"

This command only accesses the backup storage and does not access the TiDB cluster.

The --storage parameter is used to specify the backup storage address. Currently, BR supports Amazon S3, GCS, or Azure Blob Storage as the storage for log backup. For details, see URI Formats of External Storage Services.

Usage example:

  1. tiup br log metadata –-storage='s3://backup-101/logbackup?access-key=${access-key}&secret-access-key=${secret-access-key}"'

Expected output:

  1. [2022/07/25 23:02:57.236 +08:00] [INFO] [collector.go:69] ["log metadata"] [log-min-ts=434582449885806593] [log-min-date="2022-07-14 20:08:03.268 +0800"] [log-max-ts=434834300106964993] [log-max-date="2022-07-25 23:00:15.618 +0800"]

Restore to a specified point in time (PITR)

You can run the tiup br restore point command to perform a PITR on a new cluster or just restore the log backup data.

Run tiup br restore point --help to see the help information:

  1. tiup br restore point --help
  2. restore data from log until specify commit timestamp
  3. Usage:
  4. br restore point [flags]
  5. Flags:
  6. --full-backup-storage string specify the backup full storage. fill it if want restore full backup before restore log.
  7. -h, --help help for point
  8. --restored-ts string the point of restore, used for log restore. support TSO or datetime, e.g. '400036290571534337' or '2018-05-11 01:42:23+0800'
  9. --start-ts string the start timestamp which log restore from. support TSO or datetime, e.g. '400036290571534337' or '2018-05-11 01:42:23+0800'
  10. Global Flags:
  11. --ca string CA certificate path for TLS connection
  12. --cert string Certificate path for TLS connection
  13. --key string Private key path for TLS connection
  14. -u, --pd strings PD address (default [127.0.0.1:2379])
  15. -s, --storage string specify the url where backup storage, eg, "s3://bucket/path/prefix"

The example output only shows the common parameters. These parameters are described as follows:

  • --full-backup-storage: the storage address for the snapshot (full) backup. To use PITR, specify this parameter and choose the latest snapshot backup before the restore timestamp. To restore only log backup data, you can omit this parameter. Note that when initializing the recovery cluster for the first time, you must specify a snapshot backup. Currently, BR supports Amazon S3, GCS, and Azure Blob Storage as the storage for log backup. For details, see URI Formats of External Storage Services.
  • --restored-ts: the timestamp that you want to restore data to. If this parameter is not specified, BR restores data to the latest timestamp available in the log backup, that is, the checkpoint of the backup data.
  • --start-ts: the start timestamp that you want to restore log backup data from. If you only need to restore log backup data, you must specify this parameter.
  • --pd: the PD address of the restore cluster.
  • --ca, --cert, --key: specify the mTLS encryption method to communicate with TiKV and PD.
  • --storage: the storage address for the log backup. Currently, BR supports Amazon S3, GCS, or Azure Blob Storage as the storage for log backup. For details, see URI Formats of External Storage Services.

Usage example:

  1. tiup br restore point --pd="${PD_IP}:2379"
  2. --storage='s3://backup-101/logbackup?access-key=${access-key}&secret-access-key=${secret-access-key}"'
  3. --full-backup-storage='s3://backup-101/snapshot-202205120000?access-key=${access-key}&secret-access-key=${secret-access-key}"'
  4. Full Restore <--------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  5. *** ***["Full Restore success summary"] ****** [total-take=3.112928252s] [restore-data-size(after-compressed)=5.056kB] [Size=5056] [BackupTS=434693927394607136] [total-kv=4] [total-kv-size=290B] [average-speed=93.16B/s]
  6. Restore Meta Files <--------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  7. Restore KV Files <----------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
  8. "restore log success summary"] [total-take=192.955533ms] [restore-from=434693681289625602] [restore-to=434693753549881345] [total-kv-count=33] [total-size=21551]

Log Backup and PITR Command Manual - 图3

Note

  • When you restore the cluster for the first time, you must specify the full snapshot data. Otherwise, some data in the newly created table might be incorrect due to rewriting Table ID rules.
  • You cannot restore the log backup data of a certain time period repeatedly. If you restore the log backup data of a range [t1=10, t2=20) repeatedly, the restored data might be inconsistent.
  • When you restore log data of different time periods in multiple batches, ensure that the log data is restored in consecutive order. If you restore the log backup data of [t1, t2), [t2, t3), and [t3, t4) in consecutive order, the restored data is consistent. However, if you restore [t1, t2) and then skip [t2, t3) to restore [t3, t4), the restored data might be inconsistent.