【TomYu 】SQL Server 2005異地備份

iDotNetSpace發表於2008-07-21

前幾天做了資料庫映象,現在也要來做做資料庫的備份。本方案採用備份至本地然後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,成功備份!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-406685/,如需轉載,請註明出處,否則將追究法律責任。

相關文章