Home  >  Article  >  Database  >  批处理备份数据库

批处理备份数据库

WBOY
WBOYOriginal
2016-06-07 15:00:171059browse

简单介绍一个实例用命令行的方式备份数据库.这种情况主要发生在SQL版本不支持维护计划和Job的情况下,使用起来挺方便的. 可以配合Windows的任务计划实现每天自动备份. sqlcmd.exe -S EM\TIM2008R2 (1) -U sa (2) -P 123456 (3) -d Test (4) -b -c cmd_end –Q

简单介绍一个实例用命令行的方式备份数据库.这种情况主要发生在SQL版本不支持维护计划和Job的情况下,使用起来挺方便的.

可以配合Windows的任务计划实现每天自动备份.

sqlcmd.exe -S EM\TIM2008R2(1) -U sa(2) -P 123456(3) -d Test(4) -b -c cmd_end –Q

"declare @name varchar(250) set @name='D:\DB_test\Test_'+convert(varchar(50),getdate(),112)+'.bak' backup database Test to disk=@name"(5)

备注:

(1) 数据库连接实例

(2) 登录名
(3) 密码

(4) 数据库名字

(5) SQL语句,备份语句.

 

另附数据库备份和还原的语法:

declare @name varchar(250)
set @name='D:\DB_test\Test_'+
convert(varchar(50),getdate(),112)+'.bak'
BACKUP DATABASE [Test] TO 
DISK = @name
WITH NOFORMAT, NOINIT, 
NAME = N'Test_FullBackUp',
SKIP, NOREWIND, NOUNLOAD


USE master;
GO
RESTORE FILELISTONLY
   FROM DISK ='D:\Temp files\Test_20120706.bak'

RESTORE DATABASE Test1
   FROM DISK ='D:\Temp files\Test_20120706.bak'
   WITH RECOVERY,
   MOVE 'Test' TO 'D:\Temp files\Test1.mdf',
   MOVE 'Test__log' TO 'D:\Temp files\Test1_Log.ldf';
GO

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn