Br备份还原shell脚本(分享)

ticde-backup.sh  #支持全量、增量
```bash
#! /bin/sh
#!/bin/bash#!/bin/bash
# ticde-backup.sh
# 
# # 1)手动增加bakall 是全备
# ticde-backup.sh bakall
# # 2)周日 是全备
# ticde-backup.sh 
# # 3)非周日 是增备
# ticde-backup.sh 
#
# 使用cron定时 每天 01:00:00 执行一次
#  0 1 * * * ticde-backup.sh 
#
# const 【不要修改】
DateFull=`date -d today +"%Y-%m-%d %H:%M:%S"`
DateMin=`date -d today +"%Y-%m-%d"`
DateIncr=`date -d today +"%Y-%m-%d-%H%M"`
DateLog=`date -d today +"%Y-%m"`
CurWeek=`date +%u`
BakFullFlag=0

BakRoot=/tidb-bak111/brdata
BakRootData=${BakRoot}/brbak/brbak${DateMin}
BakRootDataIncr=${BakRoot}/brbakincr/brbakincr${DateIncr}
BakRootLog=${BakRoot}/brbaklog/log-${DateLog}.log
# 强制全备 标记 bakall
CustormArg1=$1
LAST_BACKUP_TS=0


# config 【可以修改】
DBName=test
BrBin=/usr/local0/webserver/tidb/tidb-toolkit-v5.3.0/bin
Pd=127.0.0.1:2379
Concurrency=16
Ratelimit=256


# 逻辑判断
if [ "$1" = "bakall" ];then
    # 临时全备防止目录冲突
    BakRootData=${BakRootData}-`date -d today +"%H%M%S"`
    BakFullFlag=1
fi


if [ ${CurWeek} -eq 7 ];then
    BakFullFlag=1
fi


if [ ${BakFullFlag} -ne 1 ];then 
	#找到上一次的路径
	LAST_BACKUP_DIR=${BakRoot}/brbak/brbak$(date -d "-${CurWeek} days" +%Y-%m-%d)
	if [ ! -d "${LAST_BACKUP_DIR}" ];then
		echo '上次全备(上周日的全备目录 ${LAST_BACKUP_DIR} )不存在!'
		exit 0
	fi
	LAST_BACKUP_TS=`${BrBin}/br validate decode --field="end-version" -s local://${LAST_BACKUP_DIR} | tail -n1`
	if [ ${LAST_BACKUP_TS} -lt 100 ];then 
		echo '上次全备(上周日的全备目录 ${LAST_BACKUP_DIR} )获取 LAST_BACKUP_TS 失败!'
		exit 0
	fi 
	
	# 执行增量备份
	mkdir -p ${BakRootDataIncr}/
	chmod 777 ${BakRootDataIncr}/

	echo "增量备份到:${BakRootDataIncr}/"
	
	${BrBin}/br backup db \
    --db ${DBName} \
    -s local://${BakRootDataIncr}/ \
    --pd ${Pd} \
    --log-file ${BakRootLog} \
	--lastbackupts ${LAST_BACKUP_TS} \
	--ratelimit ${Ratelimit} \
    --concurrency ${Concurrency}

else

	# 执行全量备份
	mkdir -p ${BakRootData}/
	chmod 777 ${BakRootData}/
	
	echo "全量备份到:${BakRootData}/"
	
	${BrBin}/br backup db \
    --db ${DBName} \
    -s local://${BakRootData}/ \
    --pd ${Pd} \
    --log-file ${BakRootLog} \
	--ratelimit ${Ratelimit} \
    --concurrency ${Concurrency}

fi

echo '---  end --- '
ticde-backup.sh #恢复
```shell
#!/bin/bash
# ticde-backup.sh
# 
# # 1)手动 需要传递 back的目录
# ticde-restore.sh /tidb-bak111/brdata/brbak/brbak2022-03-17/
#	先恢复全备,再恢复增备
#
# const 【不要修改】
DateFull=`date -d today +"%Y-%m-%d %H:%M:%S"`
DateMin=`date -d today +"%Y-%m-%d"`
DateLog=`date -d today +"%Y-%m"`


BakRoot=/tidb-bak111/brdata
BakRootLog=${BakRoot}/brbaklog/restore-log-${DateLog}.log
# 原备份的目录
BakDir=$1

# config 【可以修改】
DBName=test
BrBin=/usr/local0/webserver/tidb/tidb-toolkit-v5.3.0/bin
Pd=127.0.0.1:2379
Concurrency=16
Ratelimit=256


# 逻辑判断
if [ -z "${BakDir}" ]  || [ ! -d "${BakDir}" ] ;then
	echo "请输入备份所在的目录"
	exit 0
fi
	
echo "从备份 ${BakDir}/  恢复到数据库:${DBName}"

	${BrBin}/br restore db \
    --db ${DBName} \
    -s local://${BakDir}/ \
    --pd ${Pd} \
    --log-file ${BakRootLog} \
	--ratelimit ${Ratelimit} \
    --concurrency ${Concurrency}

echo '---  end --- '

参考:https://tidb.io/blog/af8080f7#TiDB-最小实践Cluster111

4赞

有备份到远程电脑和删除历史备份的方案不、?

与远程存储备份的机器配置上互信,如下是否可行

########## #rm 3 days ago bak ############

cd $BAK_BASE
find $BAK_BASE -mtime +3 -type d -exec rm -rf {} ;

#scp $BAK_BASE/$BAK_FILENAME.tar.gz backup@****:/home/backup

#ssh backup@****:/home/backup 'echo “--------------delete *.zip --------------”;

cd /home/backup;

find /home/backup -mtime +7 -name “*.tar.gz” -exec rm -r {} ;’

脚本很好