autoDeploy/backUpIncremental.sh
zhangkun9038@dingtalk.com 82e5dd55f5 增量式备份
2023-07-15 22:29:01 +08:00

58 lines
2.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
currentTime=$(date +"%Y-%m-%d-%H")
userName=$1
password=$2
dbName=$3
host=$4
# 定义备份文件存放目录
backUpPath=$5
ossPath=$6
# 获取上次备份的binlog文件名和位置
LAST_BACKUP=$(cat "$backUpPath/last_backup.txt")
LAST_BINLOG=$(echo "$LAST_BACKUP" | awk '{print $1}')
LAST_POSITION=$(echo "$LAST_BACKUP" | awk '{print $2}')
# 如果是第一次运行,设置初始值
# 获取当前binlog文件名和位置
CURRENT_BINLOG=$(mysql -u$userName -p$password -h$host -N -e "SHOW MASTER STATUS" | awk '{print $1}')
CURRENT_POSITION=$(mysql -u$userName -p$password -h$host -N -e "SHOW MASTER STATUS" | awk '{print $2}')
if [[ -z "$LAST_BACKUP" ]]; then
LAST_BINLOG="ON.000001"
LAST_POSITION="0"
fi
echo ""
echo current_binlog $CURRENT_BINLOG
echo current_binPostition $CURRENT_POSITION
# 备份增量变更到文件
# 如果上次备份的binlog文件名和位置为空则进行全量备份
cd $backUpPath
#if [ -z "$LAST_BINLOG" ]; then
# mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD -h $MYSQL_HOST $MYSQL_DATABASE > "$BACKUP_DIR/full_backup.sql"
#else
echo "docker exec maria /usr/bin/mysqlbinlog -u"${userName}" -p"${password}" -h"${host}" --start-position="${LAST_POSITION}" --stop-position="${CURRENT_POSITION}" "$LAST_BINLOG" > "${backUpPath}"/"${dbName}"-"${currentTime}".sql"
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-${currentTime}.sql
#fi
# 更新上次备份的binlog文件名和位置
echo "$CURRENT_BINLOG $CURRENT_POSITION" > "last_backup.txt"
backup_files=()
find "$backUpPath" -name "${backUpPath}/${dbName}-incremental-*.sql.gz" -type f -print0 | while IFS= read -r -d '' file; do
file_date=$(stat -c %Y "$file")
current_time=$(date +%s)
if (( (current_time - file_date) > 86400 )); then
backup_files+=("$file")
fi
done
# 遍历数组,打印每个文件名
for file in "${backup_files[@]}"; do
echo "$file"
done