异地备份SQL2005数据库
CREATE PROCEDURE [dbo].[backupDatabase]
AS
BEGIN
SET NOCOUNT ON;
---显示高级选项(仅需执行一次)
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: //172.16.1.222/Databasebackup "6141" /user:userqrlove\administrator'
exec master..xp_cmdshell @string
---其中172.16.1.222为文件服务器的地址,DatabaseBackup为该服务器的共享文件夹,userqrlove为机器名,administrator 6141 分别为共享时设置的用户名密码。
----备份数据库至本地
declare @date datetime
set @date = GetDate()
declare @str nvarchar(100)
set @str = 'd:\DatabaseBackup\hydee_syy_'+ convert(nvarchar(12), @date, 112) +'.bak'
backup database hydee_syy 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
---关闭允许执行cmdshell
EXEC sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO
----成功备份