autoDeploy/backup.sh
zhangkun9038@dingtalk.com 6b05afd110 增量式备份
2023-07-16 01:07:03 +08:00

49 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
# 获取当前日期和时间
current_time=$(date +"%Y-%m-%d-%H")
userName=$1
password=$2
dbName=$3
host=$4
backUpPath=$5
ossPath=$6
# 备份文件名
backup_file="${dbName}_backUp_${current_time}.sql"
echo "backup_file: "${backup_file}
mkdir ${backUpPath} -p || true
# 备份MySQL数据库
mysqldump --column-statistics=0 -R --opt -u${userName} -p"${password}" -h${host} ${dbName} > ${backUpPath}/"$backup_file"
cd ${backUpPath}
rm sql -rf || true
mkdir sql || true
mv ${backup_file} sql/
tar -czvf ${backup_file}".tar.gz" "sql/"
ossutil cp ${backup_file}".tar.gz" oss://${ossPath}/${backup_file}".tar.gz"
rm -fr sql
# 存储60个小时前的备份文件名的数组
old_backup_files=()
cd ${backUpPath}
# 遍历60个小时前的备份文件并将文件名存入数组
while IFS= read -r -d '' file; do
old_backup_files+=("$file")
done < <(find ${backUpPath} -name "${dbName}_backUp_*.tar.gz" -type f -mmin +1440 -print0)
# 遍历数组,删除备份文件
for file in "${old_backup_files[@]}"; do
rm "$file" || true
ossutil rm $file oss://${ossPath}/$file -f || true
done
#删除10天前的备份
#delete_date=$(date -d "10 days ago" +%Y-%m-%d)
#delete_file="${dbName}_backup_${delete_date}.sql.tar.gz"
#rm ${backUpPath}/"$delete_file"
# 输出日志
echo "[$current_date $current_time] 执行${dbName}数据库备份并删除10天前备份成功"