Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: 请问下,tidb数据库大家都是怎么进行定时备份的
[TiDB Usage Environment] Production Environment
[TiDB Version] v5.4.0
[Reproduction Path] How does everyone perform scheduled backups for the TiDB database?
[Encountered Issues: Problem Phenomenon and Impact]
[Resource Configuration] Go to TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page
[Attachments: Screenshots/Logs/Monitoring]
Write a script and schedule it to run at regular intervals.
Are you using dumpling for the backup?
br should be backed up on shared storage, right?
NFS and S3 are both fine.
It depends on the data volume. For small databases, you can use Dumpling. For large databases, using Dumpling might take an entire day to back up, and it would be even slower if you need to restore it.
Use BR for backup and enable logging.
For full backups, I set up a scheduled task to back up. Incremental backups can back up binlog logs. If you have the resources, you can set up a replica to create a master-slave configuration. If the master fails, switch to the replica.
Use BR for backup and enable logging.
To restore using br, can it only be restored to an empty database? My data volume is small, and I don’t have shared storage, so I use dumpling.
For small amounts of data, Dumpling backup is fine. For larger amounts of data, it is recommended to use BR.
TiDB supports scheduled backups using the BR tool. Here are the specific steps for the backup:
- Prepare the backup script
Create a backup script on the TiDB server, for example:
#!/bin/bash
export DATEDIR=`date +%Y%m%d`
export BASEDIR=/tidbbak/db
mkdir -p $BASEDIR/$DATEDIR
/usr/local/bin/br backup full --pd "192.168.3.221:2379" --storage "local://$BASEDIR/$DATEDIR" --ratelimit 120 --log-file $BASEDIR/$DATEDIR/fullbackup_`date +%Y%m%d`.log
sync
sleep 10
find ${BASEDIR} -type f -mtime +31 -exec rm {} \;
find ${BASEDIR} -type d -empty -delete
In this script, br backup full
indicates a full backup, the --pd
parameter specifies the PD address, the --storage
parameter specifies the backup storage location, the --ratelimit
parameter specifies the backup rate, and the --log-file
parameter specifies the backup log location.
- Add a scheduled task
Use the crontab command to add a scheduled task, for example:
## 1. Allow the tidb user to add crond scheduled tasks
~]# echo tidb >> /etc/cron.allow
## 2. Add the br backup task
0 2 * * * /bin/bash /root/scripts/fulldbbak.sh
In this example, 0 2 * * *
means performing the backup at 2 AM every day, and /bin/bash /root/scripts/fulldbbak.sh
indicates executing the backup script.
In summary, you can achieve scheduled backups for TiDB using the BR tool and the crontab command.
It would be perfect if you add a section on mounting a shared disk to all TiKV nodes in this manual.