How to Perform Incremental Backup in TiDB

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: TIDB 如何增量备份

| username: Cheriston

TiDB Full Backup:

#! /bin/bash

# Set MinIO admin account and password
export MINIO_ROOT_USER=myminio_admin
export MINIO_ROOT_PASSWORD=mJ7gm4SXFJF28UoD6T9a4W

# Set AWS access keys
export AWS_ACCESS_KEY_ID=myminio_admin
export AWS_SECRET_ACCESS_KEY=mJ7gm4SXFJF28UoD6T9a4W

# Add MinIO host alias
mc config host add tidb-test http://192.168.136.101:9000 myminio_admin mJ7gm4SXFJF28UoD6T9a4W
mc config host ls tidb_test

# Create bucket
mc mb /tidb/backup/bucket

# Generate unique bucket name
timestamp=$(date +"%Y%m%d%H%M%S")
bucket_name="backup-${timestamp}"

# Perform full backup
/usr/bin/br backup full --ratelimit 100 --pd "192.168.136.101:2379" --storage "s3://bucket/${bucket_name}" \
--send-credentials-to-tikv=true --s3.endpoint "http://192.168.136.101:9000" --log-file "/tidb/backup/backup_${timestamp}.log"

Incremental Backup:

#! /bin/bash
export MINIO_ROOT_USER=myminio_admin
export MINIO_ROOT_PASSWORD=mJ7gm4SXFJF28UoD6T9a4W

export AWS_ACCESS_KEY_ID=myminio_admin
export AWS_SECRET_ACCESS_KEY=mJ7gm4SXFJF28UoD6T9a4W

# Add MinIO host alias
mc config host add tidb-test http://192.168.136.101:9000 myminio_admin mJ7gm4SXFJF28UoD6T9a4W
mc config host ls tidb_test

# Create bucket
mc mb tidb/backup/bucket

# Generate unique bucket name
timestamp=$(date +"%Y%m%d%H%M%S")
bucket_name="backup-${timestamp}"

# Get the timestamp of the last full backup
last_backup=$(mc cat /tidb/backup/bucket/last_backup)

# Determine if a full backup is needed
if [ -z "$last_backup" ]; then
    echo "$timestamp" | mc pipe /tidb/backup/bucket/last_backup
    # Perform full backup
    /usr/bin/br backup full --ratelimit 100 --pd "192.168.136.101:2379" --storage "s3://bucket/${bucket_name}" \
    --send-credentials-to-tikv=true --s3.endpoint "http://192.168.136.101:9000" --log-file "/tidb/backup/backup_${timestamp}.log"
else
    # Perform incremental backup
    /usr/bin/br backup incremental --ratelimit 100 --pd "192.168.136.101:2379" --storage "s3://bucket/${bucket_name}" \
    --send-credentials-to-tikv=true --s3.endpoint "http://192.168.136.101:9000" --last-backup "$last_backup" --log-file "/tidb/backup/backup_${timestamp}.log"
fi

# Update the timestamp of the last full backup
echo "$timestamp" | mc pipe /tidb/backup/bucket/last_backup

| username: Cheriston | Original post link

How should this issue be handled?

| username: 有猫万事足 | Original post link

One is - and the other is _.
Then it couldn’t be found.

My suggestion is to eat first, as it’s close to mealtime. Low blood sugar can easily cause this issue. Don’t ask me how I know. :joy:

| username: Jellybean | Original post link

Just a reminder, OP, please be mindful of desensitizing content when posting, especially things like admin accounts and passwords, AWS access keys, etc. Try to protect such information and avoid posting it online.

Additionally, you can refer to the solution mentioned by the expert upstairs, pay attention to the naming issues, and try again after making adjustments.

| username: Cheriston | Original post link

Still not working.

| username: Cheriston | Original post link

No issues with my virtual environment.

| username: 小龙虾爱大龙虾 | Original post link

What version of BR are you using? The backup command you provided is different from the official documentation. I checked the documentation for versions 6.5 and 7.5, and your command isn’t there. Here’s the link to the official documentation: TiDB 增量备份与恢复使用指南 | PingCAP 文档中心
Also, the official incremental backup is no longer maintained. It is recommended to use log backup (PITR) in the future.

| username: 有猫万事足 | Original post link

Is it still not working? What error is it reporting?

| username: 连连看db | Original post link

This option is used incorrectly, right? Check if it should be --lastbackupts.

| username: 连连看db | Original post link

You’ve already used version 7.5 of TiDB, so why not choose log backup for incremental backups?

| username: Cheriston | Original post link

Is this a bug in the version?

| username: 有猫万事足 | Original post link

No. BR requires either using shared storage like NFS or S3 for backups, or having a folder with the same path on each TiKV with the same access permissions.

You should be using S3 for storing backups, so why is it asking for a local folder? It’s likely that there’s something incorrect in the command settings. Please provide the BR command.

| username: 有猫万事足 | Original post link

I looked at the logic of your script and the current method, and there is indeed a significant difference. The current br backup method is:

TiDB supports two types of backups, which one should be used? Full backups include all data at a specific point in time in the cluster, while log backups include records of data changes generated by business writes in TiDB. It is recommended to use both backup methods together:

  • Enable log backup: Run the br log start command to start the log backup task. The task will continuously run on each TiKV node, periodically backing up TiDB change data to the specified storage in small batches.
  • Regularly perform snapshot (full) backup: Run the br backup full command to back up the cluster snapshot to the backup storage, for example, performing a cluster snapshot backup at midnight every day.

The br log start log backup starts and does not stop. It does not need to be executed periodically. The br backup full snapshot backup needs to be executed regularly. Your script might be an approach from an earlier version.

| username: TiDBer_5cwU0ltE | Original post link

Scripts and screenshots can be attached as files. When there’s too much content, it can be overwhelming…

| username: dba远航 | Original post link

There is an issue with the hostname alias, please check it.

| username: ffeenn | Original post link

Check the directory.

| username: TiDBer_rvITcue9 | Original post link

Bookmark it.

| username: Soysauce520 | Original post link

It is recommended that when using PITR in real-time, the rollback time point should also be improved.