備份資料庫到其它伺服器

來源:互聯網
上載者:User

 

CREATE PROCEDURE [dbo].[backupDatabase]
AS
BEGIN
 SET NOCOUNT ON;

--顯示進階選項
exec sp_configure 'show advanced options',1
reconfigure

--允許執行x-_cmdshell
exec sp_configure 'xp_cmdshell',1
reconfigure

--添加對應磁碟機
declare @string varchar(200)
set @string = 'net use z: //192.168.1.200/d$/DatabaseBackup "123456" /user:192.168.1.200\administrator'
exec master..xp_cmdshell @string

--備份資料庫至本地
declare @fileName nvarchar(100)
set @fileName ='d:\DatabaseBackup\register_'+convert(char(8),getdate(),112)+'.bak'
backup database register to disk=@fileName with init

--拷貝到映射目錄
declare @cmdStr nvarchar(150)
set @cmdStr= 'copy '+@fileName+ ' z:'
exec master..xp_cmdshell @cmdStr

--刪除映射以及本地備份
exec master..xp_cmdshell 'net use z: /delete'
--set @cmdStr = 'del '+@str + ''
--exec master..xp_cmdshell @cmdStr

--關閉允許執行cmdshell
exec sp_configure 'xp_cmdshell',0
reconfigure
END

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.