局域网中将备份数据库拷贝到其他服务器中(可以设置成任务,定时自动执行)

当然还有其他方式实现异机备份。该方法可以实现,但目前速度较慢,在查找原因中。

 

显示高级选项(仅需执行一次)

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO

允许执行xp_cmdshell

EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO

添加映射驱动器 declare @string nvarchar(200)
set @string = 'net use z: \\192.168.0.114\D$\backup_db "yakoterry" /user:Dev\administrator'
exec master..xp_cmdshell @string

其中192.168.0.114为文件服务器的地址,db_backup为该服务器的共享文件夹,fileserver为机器名,administrator 123456 分别为共享时设置的用户名密码。

 

备份数据库至本地
declare @date datetime
set @date = GetDate()
declare @str nvarchar(100)
set @str = 'd:\DB_'+ convert(nvarchar(12), @date, 112) +'.bak'
backup database TMSalesSample2 to disk=@str with init

 With init为覆盖同名文件(本例设计为1天执行一次,不会出现覆盖的情况)

 

拷贝到文件服务器

declare @str1 nvarchar(100)
set @str1 = 'copy '+ @str +' z:'
exec master..xp_cmdshell @str1

 删除映射以及本地备份

exec master..xp_cmdshell 'net use z: /delete'
declare @str2 nvarchar(100)
set @str2 = 'del '+@str+''
exec master..xp_cmdshell @str2

7关闭允许执行cmdshell

EXEC sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO

posted @ 2011-03-14 17:16  梅里之巅  阅读(626)  评论(0编辑  收藏  举报