netnr/ backup_sqlserver.bat 2022-03-22 15:46
SQLServer 备份脚本 Windows
@echo off

:: 配置参数(开始)===

set ymd=%date:~0,4%%date:~5,2%%date:~8,2%
echo 日期:%ymd%
echo.
set keepday=15
echo 保留最近天数:%keepday%
echo.
set dirc=%~dp0
echo 当前目录:%dirc%
echo.
set dirsync=%dirc%sync\
echo 同步目录:%dirsync%
echo.
set dbname=netnr
echo 数据库名:%dbname%
echo.

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

echo.
echo 清理目录 %dirsync% %keepday% 天前的备分文件
forfiles /p %dirsync% /m *.bak /d -%keepday% /c "cmd /c del @path"
echo.
echo 开始备份数据库:%dbname%
sqlcmd -Q "BACKUP DATABASE %dbname% TO DISK='%dirsync%%dbname%_%ymd%.bak' with format"
echo.
echo Done
echo.
::pause