SQL Server命令行备份语句

是否有人知道是否有办法将SQL Server备份编写到batch file中,以便可以从命令行执行?

下面是一个示例,您可以使用Sql Server客户端工具中的SQLCMD实用程序将其作为批处理脚本(复制粘贴到.bat文件中)运行:

备份:

echo off cls echo -- BACKUP DATABASE -- set /p DATABASENAME=Enter database name: :: filename format Name-Date (eg MyDatabase-2009.5.19.bak) set DATESTAMP=%DATE:~-4%.%DATE:~7,2%.%DATE:~4,2% set BACKUPFILENAME=%CD%\%DATABASENAME%-%DATESTAMP%.bak set SERVERNAME=your server name here echo. sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT" echo. pause 

恢复:

 echo off cls echo -- RESTORE DATABASE -- set /p BACKUPFILENAME=Enter backup file name:%CD%\ set /p DATABASENAME=Enter database name: set SERVERNAME=your server name here sqlcmd -E -S %SERVERNAME% -d master -Q "ALTER DATABASE [%DATABASENAME%] SET SINGLE_USER WITH ROLLBACK IMMEDIATE" :: WARNING - delete the database, suits me :: sqlcmd -E -S %SERVERNAME% -d master -Q "IF EXISTS (SELECT * FROM sysdatabases WHERE name=N'%DATABASENAME%' ) DROP DATABASE [%DATABASENAME%]" :: sqlcmd -E -S %SERVERNAME% -d master -Q "CREATE DATABASE [%DATABASENAME%]" :: restore sqlcmd -E -S %SERVERNAME% -d master -Q "RESTORE DATABASE [%DATABASENAME%] FROM DISK = N'%CD%\%BACKUPFILENAME%' WITH REPLACE" :: remap user/login (http://msdn.microsoft.com/en-us/library/ms174378.aspx) sqlcmd -E -S %SERVERNAME% -d %DATABASENAME% -Q "sp_change_users_login 'Update_One', 'login-name', 'user-name'" sqlcmd -E -S %SERVERNAME% -d master -Q "ALTER DATABASE [%DATABASENAME%] SET MULTI_USER" echo. pause 

如果您需要该batch file来安排备份,则SQLpipe理工具已安排计划的任务…

Seba Illingworth的代码,如果你需要时间在你的文件名(它给2014-02-21_1035)

 echo off cls echo -- BACKUP DATABASE -- set /p DATABASENAME=Enter database name: For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b) :: filename format Name-Date (eg MyDatabase-2009.5.19.bak) set DATESTAMP=%mydate%_%mytime% set BACKUPFILENAME=%CD%\%DATABASENAME%-%DATESTAMP%.bak set SERVERNAME=. echo. sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT" echo. pause 

我正在使用SQL Server 2005 Express,并且必须启用命名pipe道连接才能从Windows命令进行备份。 我最后的脚本是这样的:

 @echo off set DB_NAME=Your_DB_Name set BK_FILE=D:\DB_Backups\%DB_NAME%.bak set DB_HOSTNAME=Your_DB_Hostname echo. echo. echo Backing up %DB_NAME% to %BK_FILE%... echo. echo. sqlcmd -E -S np:\\%DB_HOSTNAME%\pipe\MSSQL$SQLEXPRESS\sql\query -d master -Q "BACKUP DATABASE [%DB_NAME%] TO DISK = N'%BK_FILE%' WITH INIT , NOUNLOAD , NAME = N'%DB_NAME% backup', NOSKIP , STATS = 10, NOFORMAT" echo. echo Done! echo. 

在这里工作得很好!

结合使用上面的脚本删除旧的备份文件 ,然后这可以执行一个调度程序备份,保留最后10个备份文件

 echo off :: set folder to save backup files ex. BACKUPPATH=c:\backup set BACKUPPATH=<<back up folder here>> :: set Sql Server location ex. set SERVERNAME=localhost\SQLEXPRESS set SERVERNAME=<<sql host here>> :: set Database name to backup set DATABASENAME=<<db name here>> :: filename format Name-Date (eg MyDatabase-2009-5-19_1700.bak) For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b) set DATESTAMP=%mydate%_%mytime% set BACKUPFILENAME=%BACKUPPATH%\%DATABASENAME%-%DATESTAMP%.bak echo. sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT" echo. :: In this case, we are choosing to keep the most recent 10 files :: Also, the files we are looking for have a 'bak' extension for /f "skip=10 delims=" %%F in ('dir %BACKUPPATH%\*.bak /s/b/od/a-d') do del "%%F"