備份/恢複系統資料庫

來源:互聯網
上載者:User
1.備份/恢複系統資料庫  在SQL Server資料庫中,系統資訊儲存在系統資料庫中,主要的系統資料庫包括:
  master-從整體上控制使用者資料庫和SQL Server操作,在建立了任何使用者定義物件後,都要備份它
  model-為新資料庫提供模版和原型
  msdb-包含了有關作業、警示及操作員等資訊備份方法和備份普通資料庫一樣,備份後,將備份檔案複製到另一SQL伺服器上,然後進行恢複a.進入單一使用者模式的方法:
 1.在命令列模式下輸入sqlservr -c -f -m或者輸入sqlservr -m
 其中:-c 可以縮短啟動時間,SQL Server 不作為Windows NT的服務啟動
  -f 用最小配置啟動SQL Server
  -m 單一使用者模式啟動SQL Server  2.可以在控制台-服務-MSSQLServer的啟動參數中輸入-c -f -m或者輸入-m,點擊開始
 
3.進行master資料庫的恢複
a.直接進入查詢分析器,有個提示不要理會它
輸入恢複語句進行資料庫恢複:
RESTORE DATABASE master from disk='c:/具體的備份檔案名'b.或者用這個,在命令提示字元下輸入,注意大小寫
 使用"windows身分識別驗證"的,輸入:isql /E
 使用"sql server和windows身分識別驗證"的,輸入:isql /U"使用者名稱" /P"密碼"
然後在出現的提示符下輸入(注意1>,2>是提示符):
1>RESTORE DATABASE master from disk='c:/具體的備份檔案名'
2>go 2.--1.建立一個目錄,用來儲存備份的資料庫,然後在master資料庫中建立此預存程序,並調用它來完成使用者資料庫的備份./*--備份所有資料庫
 
 備份的檔案名稱為資料庫名+.bak--鄒建 2003.10--*//*--調用樣本--備份所有使用者資料庫
exec p_backupdb @bkpath='c:/',@dbname=''--備份指定資料庫
exec p_backupdb @bkpath='c:/',@dbname='客戶資料,xzkh_new'
--*/if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_backupdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_backupdb]
GOcreate proc p_backupdb
@bkpath nvarchar(260)='', --備份檔案的存放目錄,不指定則使用SQL預設的備份目錄
@dbname nvarchar(4000)='' --要備份的資料庫名稱列表,不指定則備份所有使用者資料庫
as
declare @sql varchar(8000)--檢查參數
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/'
end
else if right(@bkpath,1)<>'/' set @bkpath=@bkpath+'/'--得到要備份的資料庫列表
if isnull(@dbname,'')=''
 declare tb cursor local for
 select name from master..sysdatabases where name not in('master','tempdb','model','msdb')
else
 declare tb cursor local for
 select name from master..sysdatabases
  where name not in('master','tempdb','model','msdb')
   and(@dbname like '%,'+name+',%' or @dbname like name+',%' or @dbname like '%,'+name)--備份處理
open tb
fetch next from tb into @dbname
while @@fetch_status=0
begin
 set @sql='backup database '+@dbname
  +' to disk='''+@bkpath+@dbname
  +'.bak'' with init'
 exec(@sql)
 fetch next from tb into @dbname
end
close tb
deallocate tb
go--複製上面備份的所有使用者資料庫到另一台伺服器上.在master資料庫中建立下面的預存程序,然後用它來恢複所有的使用者資料庫./*--恢複指定目錄下的所有資料庫 恢複的資料庫名為備份檔案名(不含副檔名)
 備份檔案的副檔名固定為.bak--鄒建 2003.10--*//*--調用樣本
--恢複指定目錄下的所有資料庫
exec p_RestoreDb @bkpath='c:/'--恢複指定目錄下的指定資料庫
exec p_RestoreDb @bkpath='c:/',@bkfile='客戶資料,xzkh_new'
--*/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]
GOcreate 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時有效
as
declare @sql varchar(8000),@dbname sysnameif 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/'
end
else 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))
end
else 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,1if 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
 end
 close #f
 deallocate #f
 drop table #tb --關閉使用者進程處理
 if @overexist=1 and @killuser=1
 begin
  declare @spid varchar(20)
  declare #spid cursor for
   select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
  open #spid
  fetch next from #spid into @spid
  while @@fetch_status=0
  begin 
   exec('kill '+@spid)
   fetch next from #spid into @spid
  end 
  close #spid
  deallocate #spid
 end --恢複資料庫
 exec(@sql)
 fetch next from tb into @dbname
end
close tb
deallocate tb
go

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.