netnr/ backup_sqlserver.sh 2022-03-22 16:22
SQLServer 备份脚本 Linux
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="netnr"
echo "数据库:$dbname"
echo
sqlcmd="/opt/mssql-tools/bin/sqlcmd"
echo "sqlcmd路径:$sqlcmd"
echo
dbconn="-U SA -P 'Your&Strong(!)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"
backsql="\"BACKUP DATABASE $dbname TO DISK='$dirsync/($dbname)_$ymd.bak' with format\""
backcmd="$sqlcmd $dbconn -Q $backsql"
echo "$backcmd"
eval $backcmd
echo
echo "Done"
echo