autoDeploy/backUpIncremental.sh
zhangkun9038@dingtalk.com 057bdbb930 增量式备份
2023-07-14 17:26:50 +08:00

39 lines
1.4 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 -e "SHOW MASTER STATUS" | awk '{print $1}')
CURRENT_POSITION=$(mysql -u$userName -p$password -h$host -e "SHOW MASTER STATUS" | awk '{print $2}')
# 备份增量变更到文件
# 如果上次备份的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
docker exec maria /usr/bin/mysqlbinlog -u$userName -p$password -h$host --start-position=$LAST_POSITION --stop-position=$CURRENT_POSITION --result-file="$backUpPath/$dbName-$currentTime.sql" $LAST_BINLOG
#fi
# 更新上次备份的binlog文件名和位置
echo "$CURRENT_BINLOG $CURRENT_POSITION" > "last_backup.txt"
# 获取备份文件列表
backup_files=($(find "$backUpPath" -name "$dbName-*.sql" -type f -printf "%f\n"))
# 打印备份文件列表
for file in "${backup_files[@]}"; do
echo "$file"
done