时间:2021-07-01 10:21:17 帮助过:60人阅读
windows下,使用mssql命令行工具sqlcmd备份数据库,并调用rar压缩;不借助mssql维护计划功能,拜托权限问题。
1. set bakupfolder=F:\backup\
@ECHO ON
set d=%date:~0,10%
set d=%d:-=%
set t=%time:~0,8%
set t=%t::=%
set stamp=%p%%d%%t%
set bakupfolder=F:\backup\
rem 1按子目录保存备份文件;0不按
set lay_in_subfolder=1
call :backupone foo
call :backupone foo2
call :backupone foo3
call :backupone foo4
goto :EOF
@ECHO OFF
:backupone
setlocal
echo %1
set dbname=%1
if not exist %bakupfolder%%dbname% mkdir %bakupfolder%%dbname%
if %lay_in_subfolder%==1 (
set subfolder=%dbname%\
)else set subfolder=
rem echo %bakupfolder%%subfolder%%dbname%%stamp%.bak
sqlcmd -U sa -P "sa" -S localhost -Q "backup database %dbname% to disk='%bakupfolder%%subfolder%%dbname%%stamp%.bak'"
"C:\Program Files\WinRAR\RAR.exe" a -ep1 -r -o+ -m5 -s -df "%bakupfolder%%subfolder%%dbname%%stamp%".rar "%bakupfolder%%subfolder%%dbname%%stamp%.bak"
endlocal&goto :EOF