原文:
SQL遠(yuǎn)程恢復(fù)
-- =============================================
-- Author: dcrenl
-- Create date: 2013-9-5 14:08:35
-- Description: WEB服務(wù)器遠(yuǎn)程恢復(fù)數(shù)據(jù)庫(kù),遠(yuǎn)程恢復(fù)和遠(yuǎn)程備份的思路是一樣的。
--不過(guò)恢復(fù)的時(shí)候有個(gè)釋放連接的問(wèn)題,所以多加了一個(gè)釋放連接的存儲(chǔ)過(guò)程。
--本來(lái)可以整合到一個(gè)里面但是考慮到其它地方也會(huì)用到釋放連接所以把它單獨(dú)拿出來(lái)了。
-- =============================================
ALTER PROCEDURE [dbo].[RemoteRestore]
@DataDisk nvarchar(max),--需要在數(shù)據(jù)庫(kù)服務(wù)器上映射的盤符
@WEBAddr nvarchar(max),--WEB服務(wù)器地址
@WEBDisk nvarchar(max),--WEB服務(wù)器共享目錄(例如:D$ 或 C$\Windows)
@Password nvarchar(max),--WEB服務(wù)器密碼
@UserName nvarchar(max),--WEB服務(wù)器用戶名
@DataName nvarchar(max),--需要恢復(fù)的數(shù)據(jù)庫(kù)名
@BackName nvarchar(max)--需要恢復(fù)的數(shù)據(jù)庫(kù)名
AS
BEGIN
SET NOCOUNT ON;
?
--打開高級(jí)設(shè)置
exec sp_configure 'show advanced options',1
reconfigure
--開啟xp_cmdshell
exec sp_configure 'xp_cmdshell',1
reconfigure
--將WEB服務(wù)器路徑映射到數(shù)據(jù)庫(kù)服務(wù)器
exec ('master..xp_cmdshell ''net use ' + @DataDisk + ': \\' + @WEBAddr + '\' + @WEBDisk + ' "' + @Password + '" /user:' + @WEBAddr + '\' + @UserName + '''')
--開始備份并復(fù)制到WEB服務(wù)器的映射路徑
declare ?@DataBaseName varchar(50)
set @DataBaseName = 'KF_Reg_' + @BackName
exec p_killspid @DataBaseName
exec ('RESTORE DATABASE [' + @DataName + '] FROM DISK = N'''+@DataDisk+':\' +@BackName+'.bak'' WITH REPLACE')
--關(guān)閉映射路徑
exec ('master..xp_cmdshell ''net use ' + @DataDisk + ': /delete''')
--關(guān)閉xp_cmdshell
exec sp_configure 'xp_cmdshell',0
reconfigure
--關(guān)閉高級(jí)設(shè)置
exec sp_configure 'show advanced options',0
reconfigure
END
?
?
-------------------------------------------------------------------------------------------------
?
?
--斷開所有用戶連接的存儲(chǔ)過(guò)和
ALTER proc [dbo].[p_killspid] (@dbname varchar(100)) ??
as ??
begin ??
declare @sql nvarchar(500) ??
declare @spid int ??
set @sql='declare getspid cursor for ? ?
select spid from master.sys.sysprocesses where dbid=db_id('''+@dbname+''')' ??
exec (@sql) ??
open getspid ??
fetch next from getspid into @spid ??
while @@fetch_status<>-1 ??
begin ??
exec('kill '+@spid) ??
fetch next from getspid into @spid ??
end ??
close getspid ??
deallocate getspid ??
end ??
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
