SQL Server 2005异地备份

来源:互联网 发布:楼梯净高2200算法 编辑:程序博客网 时间:2024/04/28 03:59

前几天做了数据库镜像,现在也要来做做数据库的备份。本方案采用备份至本地然后copy到文件服务器的方法。

SQL server 2005打了sp2的补丁后好像存储过程xp_cmdshell是不能直接用的

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

EXEC sp_configure 'show advanced options', 1

GO

RECONFIGURE

GO*

  1. 允许执行xp_cmdshell

EXEC sp_configure 'xp_cmdshell', 1

GO

RECONFIGURE

GO

  1. 添加映射驱动器

declare @string nvarchar(200)

set @string = 'net use z: //192.168.1.2/D$/db_backup "123456" /user:fileserver/administrator'

exec master..xp_cmdshell @string

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

  1. 备份数据库至本地

declare @date datetime

set @date = GetDate()

declare @str nvarchar(100)

set @str = 'd:/mydb'+ convert(nvarchar(12), @date, 112) +'.bak'

backup database mydb to disk=@str with init

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

  1. 拷贝到文件服务器

declare @str1 nvarchar(100)

set @str1 = 'copy '+ @str +' z:'

exec master..xp_cmdshell @str1

  1. 删除映射以及本地备份

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

建立sql server 作业执行步骤2-7,成功备份!

原创粉丝点击