資料備份恢儲存過程

2021-08-29 10:53:38 字數 3521 閱讀 7128

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_restoredb]') and objectproperty(id, n'isprocedure') = 1)

drop procedure [dbo].[p_restoredb]

go/**//*--恢復指定目錄下的所有資料庫

恢復的資料庫名為備份檔案名(不含副檔名)

備份檔案的副檔名固定為.bak

/*--呼叫示例

--恢復指定目錄下的所有資料庫

exec p_restoredb @bkpath='c:'

--恢復指定目錄下的指定資料庫

exec p_restoredb @bkpath='c:',@bkfile='客戶資料,xzkh_new'

--*/

create proc p_restoredb

@bkpath nvarchar(1000)='', --定義備份檔案的存放目錄,預設為sql的備份目錄

@bkfile nvarchar(4000)='',--定義要恢復的備份檔案名,不含副檔名

@dbpath nvarchar(260)='',--恢復後的資料庫存放目錄,不指定則為sql的預設資料目錄

@overexist bit=1, --是否覆蓋已經存在的資料庫,僅@retype為'db'/'dbnor'是有效

@killuser bit=1 --是否關閉使用者使用程序,僅@overexist=1時有效

asdeclare @sql varchar(8000),@dbname sysname

if isnull(@bkpath,'')=''

begin

select @bkpath=rtrim(reverse(filename)) from master..sysfiles where name='master'

select @bkpath=substring(@bkpath,charindex('',@bkpath)+1,4000)

,@bkpath=reverse(substring(@bkpath,charindex('',@bkpath),4000))+'backup'

endelse if right(@bkpath,1)<>'' set @bkpath=@bkpath+''

--得到恢復後的資料庫存放目錄

if isnull(@dbpath,'')=''

begin

select @dbpath=rtrim(reverse(filename)) from master..sysfiles where name='master'

select @dbpath=reverse(substring(@dbpath,charindex('',@dbpath),4000))

endelse if right(@dbpath,1)<>'' set @dbpath=@dbpath+''

--得到指定目錄下的所有備份檔案

create table #t(fname varchar(260),depth int,isf bit)

insert into #t exec master..xp_dirtree @bkpath,1,1

if isnull(@bkfile,'')=''

declare tb cursor local for select fn=left(fname,patindex('%.bak',fname)-1) from #t

where isf=1 and fname like '%.bak' --取.bak檔案

else

begin

set @bkfile=','+replace(@bkfile,',','.bak,')+'.bak,'

declare tb cursor local for select fn=left(fname,patindex('%.bak',fname)-1) from #t

where isf=1 and fname like '%.bak' and @bkfile like '%,'+fname+',%'

end--恢復資料庫處理

open tb

fetch next from tb into @dbname

while @@fetch_status=0

begin

--生成資料庫恢復語句

set @sql='restore database ['+@dbname

+'] from disk='''+@bkpath+@dbname+'.bak'''

+' with recovery'

+case when @overexist=1 then ',replace' else '' end

--新增移動邏輯檔案的處理

--從備份檔案中獲取邏輯檔名

declare @lfn nvarchar(128),@tp char(1),@i int

--建立臨時表,儲存獲取的資訊

create table #tb(ln nvarchar(128),pn nvarchar(260),tp char(1),fgn nvarchar(128),sz numeric(20,0),msz numeric(20,0))

--從備份檔案中獲取資訊

insert into #tb exec('restore filelistonly from disk='''+@bkpath+@dbname+'.bak''')

declare #f cursor local for select ln,tp from #tb order by tp

open #f

fetch next from #f into @lfn,@tp

set @i=0

while @@fetch_status=0

begin

select @sql=@sql+',move '''+@lfn+''' to '''+@dbpath+@dbname+cast(@i as varchar)

+case @tp when 'd' then '.mdf''' else '.ldf''' end

,@i=@i+1

fetch next from #f into @lfn,@tp

endclose #f

deallocate #f

drop table #tb

--關閉使用者程序處理

if @overexist=1 and @killuser=1

begin

declare hcforeach cursor for

select s='kill '+cast(spid as varchar) from master..sysprocesses

where dbid=db_id(@dbname)

exec sp_msforeach_worker '?'

end--恢復資料庫

exec(@sql)

fetch next from tb into @dbname

endclose tb

deallocate tb

go

Elasticsearch的資料備份和恢復以及遷移

目錄 1.為什麼備份?2.資料備份 3.資料恢復 4.es備份資料遷移目標伺服器 5.指令碼備份恢復 常見的資料庫都會提供備份機制,以解決在資料庫無法使用的情況下通過備份來恢復資料減少損失。elasticsearch 雖然有良好的容災性,但以下原因,其依然需要備份機制 1 資料災備 在整個集群無法正...

elasticsearch 資料的備份與恢復

1.備份資料 1.1 建立備份倉庫 1.2 備份指令碼 cat alidata1 admin scripts elasticsearch es bak del.sh bin bash define vars es url es bak repo hsbc backup es user elastic...

資料庫備份儲存過程

use wechat go object storedprocedure dbo p backupdb script date 2017 11 22 11 38 23 set ansi nulls on goset quoted identifier on gocreate proc dbo p b...