1. set bakupfolder=F:\backup\
备份文件存放于目录F:\backup\ (此目录需要事先建好)
2. 默认每个数据库放置到bakupfolder下的同名的子目录(脚本自动创建)中;设置lay_in_subfolder=0后,将直接放置到bakupfolder
3. 备份文件名中自动添加备份时的时间字符串,不用担心备份目录下名字重复,也便于管理。
4. call :backupone foo
foo是需要备份的数据库,需要备份其它数据库,按同样的方式一行写一条即可
5. 命令行驱动备份操作:sqlcmd -U sa -P “sa” -S localhost -Q “xxx”
这里连接数据库的用户名密码都是sa,请改成你的实际用户名密码。mssql密码中如果有一些特殊字符,可能报错,所以加上双引号。如果密码简单,不加也可以;不过sa密码,一般都是很变态的吧~~
复制代码 代码如下:
@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