Tidb br备份脚本

Tidb br备份脚本

𝓓𝓸𝓷 Lv6

Tidb快照备份和Tidb日志备份脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
#set env


PATH=/home/tidb/.tiup/bin:$PATH
DATE=`date +%Y%m%d`
keep_days=7
keep_backup=`date -d "$keep_days days ago" +%Y%m%d`
mount_point=/backup
log_path=$mount_point/tidb/log
backup_path=$mount_point/tidb/br
logfile=$backup_path/logfile
pd='172.28.6.31:2379'

if mount | grep -q " on ${mount_point} type "; then
mkdir -p $backup_path/{$DATE,logfile}
else
echo "`date +%Y%m%d` Please check if the directory $backup_path exists!" >> /tmp/tidb_backup.log
exit 1
fi


echo "----------------------------- Backup Start: `date "+%Y-%m-%d %H:%M:%S"` -----------------------------";

tiup br backup full \
--pd $pd \
--storage local://$backup_path/$DATE \
--log-file $logfile/full-br-backup-${DATE}.log

echo "----------------------------- Backup End: `date "+%Y-%m-%d %H:%M:%S"` -----------------------------";

sleep 10

if [ -d $backup_path/$keep_backup ];then

full_backup_ts=`tiup br validate decode --field="end-version" --storage /backup/tidb/br/${keep_backup} |tail -n 1`
tiup br log truncate --until=${full_backup_ts} --storage="$log_path" -y

find $log_path -mindepth 1 -type d -empty -atime +$keep_days -print0 | xargs -0 rm -rf
find $backup_path/* -maxdepth 0 -type d -mtime +$keep_days |xargs rm -rf
find $logfile -name '*.log' -mtime +$keep_days |xargs rm -rf

fi



[tidb@1 script]$ crontab -l
30 22 * * * /home/tidb/script/tidb_backup.sh > /home/tidb/script/tidb_backup.log 2>&1
  • Title: Tidb br备份脚本
  • Author: 𝓓𝓸𝓷
  • Created at : 2024-12-30 12:30:02
  • Updated at : 2025-04-28 09:51:57
  • Link: https://www.zhangdong.me/tidb-br-backup-script.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论