netnr/ backup_mysql.sh 2021-09-29 15:13
MySQL 备份脚本
Edit Delete Raw Download
# 配置参数(开始)===

ymd=$(date +%Y%m%d)
echo "日期:$ymd"
echo
keepday=5
echo "保留最近天数:$keepday"
echo
dirsync="/package/autosync/sync"
echo "同步目录:$dirsync"
echo
dbname="abc"
echo "数据库:$dbname"
echo
mysqldump="/usr/bin/mysqldump"
echo "mysqldump目录:$mysqldump"
echo
dbconn="-uroot -pStrong@123@Password"
echo "数据库连接字符串:$dbconn"
echo

# 配置参数(结束)===

echo
echo "清理同步目录 $keepday 天前的备分文件"
find ${dirsync} -mtime +$keepday -name "*.zip" | xargs -I {} rm -rf {}
find ${dirsync} -mtime +$keepday -name "*.gz" | xargs -I {} rm -rf {}
echo
echo "开始备份数据库:$dbname"
backcmd="$mysqldump $dbconn $dbname --no-tablespaces > /tmp/${dbname}_$ymd.sql"
echo "$backcmd"
eval $backcmd
echo
echo "完成备份"
echo
echo "开始打包"
zip -r -q -o /tmp/${dbname}_${ymd}.zip /tmp/${dbname}_${ymd}.* -P123456789
echo
echo "移动压缩包"
mv /tmp/${dbname}_${ymd}.zip ${dirsync}
echo
echo "清理备份文件:/tmp/${dbname}_$ymd.*"
rm -f /tmp/${dbname}_${ymd}.*
echo
echo "Done"
echo