autoDeploy/backUpIncremental.sh
zhangkun9038@dingtalk.com 2bc6139516 增量式备份
2023-07-14 17:59:58 +08:00

47 lines
1.8 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}')
# 如果是第一次运行,设置初始值
if [[ -z "$LAST_BACKUP" ]]; then
LAST_BINLOG="none"
LAST_POSITION="0"
fi
echo ""
# 获取当前binlog文件名和位置
CURRENT_BINLOG=$(mysql -u$userName -p$password -h$host -e "SHOW MASTER STATUS\G" | grep File | awk '{print substr($1, 4)}')
CURRENT_POSITION=$(mysql -u$userName -p$password -h$host -e "SHOW MASTER STATUS\G" | grep Position | 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
echo "docker exec maria /usr/bin/mysqlbinlog -u"${userName}" -p"${password}" -h"${host}" --start-position="${LAST_BINLOG}" --stop-position="${CURRENT_POSITION}" --result-file='"${backUpPath}"/"${dbName}"-"${currentTime}".sql' "${LAST_BINLOG}
docker exec maria /usr/bin/mysqlbinlog -u${userName} -p${password} -h${host} --start-position=${LAST_BINLOG} --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