增量式备份

This commit is contained in:
zhangkun9038@dingtalk.com 2023-07-16 01:00:52 +08:00
parent 42f975375c
commit a4e2340d7a

View File

@ -39,19 +39,59 @@ cd $backUpPath
docker exec maria /usr/bin/mysqlbinlog -u${userName} -p${password} -h${host} --start-position=${LAST_POSITION} --stop-position=${CURRENT_POSITION} /var/lib/mysql/$LAST_BINLOG > ${backUpPath}/${dbName}-incremental-${LAST_POSITION}-${CURRENT_POSITION}-${currentTime}.sql
zip ${backUpPath}/${dbName}-incremental-${LAST_POSITION}-${CURRENT_POSITION}-${currentTime}.sql.zip ${backUpPath}/${dbName}-incremental-${LAST_POSITION}-${CURRENT_POSITION}-${currentTime}.sql
rm ${backUpPath}/${dbName}-incremental-${LAST_POSITION}-${CURRENT_POSITION}-${currentTime}.sql
ossutil cp $file $ossPath/$file
#fi
# 更新上次备份的binlog文件名和位置
echo "$CURRENT_BINLOG $CURRENT_POSITION" > "last_backup.txt"
calculate_hour_diff() {
# 传入两个时间字符串作为参数
local time1=$1
local time2=$2
# 截取时间字符串中的年、月、日和小时部分
local year1=${time1:0:4}
local month1=${time1:4:2}
local day1=${time1:6:2}
local hour1=${time1:8:2}
local year2=${time2:0:4}
local month2=${time2:4:2}
local day2=${time2:6:2}
local hour2=${time2:8:2}
# 将时间字符串转换为时间戳
local timestamp1=$(date -d "$year1-$month1-$day1 $hour1:00:00" +%s)
local timestamp2=$(date -d "$year2-$month2-$day2 $hour2:00:00" +%s)
# 计算两个时间戳的差值,单位为小时
local diff_hours=$(( ($timestamp2 - $timestamp1) / 3600 ))
# 返回计算结果
echo $diff_hours
}
CURRENT_DATE=$(date +%Y%m%d%H)
OLD_BACKUPS=()
for file in ${dbName}-incremental-*.sql.zip; do
file_date=$(echo "$file" | grep -oP "(?<=-$dbName-)\d{10}(?=.sql.zip)")
if [[ $((CURRENT_DATE - file_date)) -gt 600 ]]; then
file_date=$(echo "$file" | grep -oP "\d{10}(?=.sql.zip)")
if [ ${#file_date} -le 1 ]; then
continue
fi
echo $file_date
difft=$(calculate_hour_diff $file_date $CURRENT_DATE )
echo difft $difft
if [[ difft -gt 24 ]]; then
echo $file $CURRENT_DATE $file_date $((CURRENT_DATE - file_date))
OLD_BACKUPS+=("$file")
fi
done
# 遍历数组,打印每个文件名
for file in "${backup_files[@]}"; do
# 遍历数组,打印每个文件名, 超过24小时的增量备份文件要删除
for file in "${OLD_BACKUPS[@]}"; do
echo "$file"
rm $file
ossutil rm $ossPath/$file
done