From a4e2340d7a037c10a3e08105d3c606ba7f47607c Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Sun, 16 Jul 2023 01:00:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E9=87=8F=E5=BC=8F=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backUpIncremental.sh | 48 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/backUpIncremental.sh b/backUpIncremental.sh index 5ec1dcf..36c5906 100755 --- a/backUpIncremental.sh +++ b/backUpIncremental.sh @@ -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