,歡迎訪問網頁設計愛好者web開發。 注意,下面備份還原都是用存儲過程實現!
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]
go
/*--備份數據庫的通用存儲過程
--鄒建 2003.10--*/
/*--調用示例
--備份當前數據庫
exec p_backupdb @bkpath='c:/',@bkfname='/dbname/_/date/_db.bak'
存儲過程實現備份和還原數據庫:
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]
go
/*--備份數據庫的通用存儲過程
--鄒建 2003.10--*/
/*--調用示例
--備份當前數據庫
exec p_backupdb @bkpath='c:/',@bkfname='/dbname/_/date/_db.bak'
--差異備份當前數據庫
exec p_backupdb @bkpath='c:/',@bkfname='db_/date/_df.bak',@bktype='df'
--備份當前數據庫日志
exec p_backupdb @bkpath='c:/',@bkfname='db_/date/_log.bak',@bktype='log'
--*/
create proc p_backupdb
@dbname sysname='', --要備份的數據庫名稱,不指定則備份當前數據庫
@bkpath nvarchar(260)='', --備份文件的存放目錄,不指定則使用sql默認的備份目錄
@bkfname nvarchar(260)='', --備份文件名,文件名中可以用/dbname/代表數據庫名,/date/代表日期,/time/代表時間
@bktype nvarchar(10)='db', --備份類型:'db'備份數據庫,'df' 差異備份,'log' 日志備份
@appendfile bit=1, --追加/覆蓋備份文件
@password nvarchar(20)='' --為備份文件設置的密碼(僅sql2000支持),設置后,恢復時必須提供此密碼
as
declare @sql varchar(8000)
if isnull(@dbname,'')='' set @dbname=db_name()
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
if isnull(@bkfname,'')='' set @bkfname='/dbname/_/date/_/time/.bak'
set @bkfname=replace(replace(replace(@bkfname,'/dbname/',@dbname)
,'/date/',convert(varchar,getdate(),112))
,'/time/',replace(convert(varchar,getdate(),108),':',''))
set @sql='backup '+case @bktype when 'log' then 'log ' else 'database ' end [email protected]
+' to disk='''[email protected][email protected]
+''' with '+case @bktype when 'df' then 'differential,' else '' end
+case @appendfile when 1 then 'noinit' else 'init' end
+case isnull(@password,'') when '' then '' else ',password='''[email protected]+'''' end
exec(@sql)
go
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
/*--恢復數據庫的通用存儲過程
--鄒建 2003.10--*/
/*--調用示例
--完整恢復數據庫
exec p_restoredb @bkfile='c:/db_20031015_db.bak',@dbname='db'
--差異備份恢復
exec p_restoredb @bkfile='c:/db_20031015_db.bak',@dbname='db',@retype='dbnor'
exec p_restoredb @bkfile='c:/db_20031015_df.bak',@dbname='db',@retype='df'
--日志備份恢復
exec p_restoredb @bkfile='c:/db_20031015_db.bak',@dbname='db',@retype='dbnor'
exec p_restoredb @bkfile='c:/db_20031015_log.bak',@dbname='db',@retype='log'
--*/
create proc p_restoredb
@bkfile nvarchar(1000), --定義要恢復的備份文件名(帶路徑)
@dbname sysname='', --定義恢復后的數據庫名,默認為備份的文件名
@dbpath nvarchar(260)='', --恢復后的數據庫存放目錄,不指定則為sql的默認數據目錄
@retype nvarchar(10)='db', --恢復類型:'db'完事恢復數據庫,'dbnor' 為差異恢復,日志恢復進行完整恢復,'df' 差異備份的恢復,'log' 日志恢復
@filenumber int=1, --恢復的文件號
@overexist bit=1, --是否覆蓋已經存在的數據庫,僅@retype為'db'/'dbnor'是有效
@killuser bit=1, --是否關閉用戶使用進程,僅@overexist=1時有效
@password nvarchar(20)='' --備份文件的密碼(僅sql2000支持),如果備份時設置了密碼,必須提供此密碼
as
declare @sql varchar(8000)
--得到恢復后的數據庫名
if isnull(@dbname,'')=''
select @sql=reverse(@bkfile)
,@sql=case when charindex('.',@sql)=0 then @sql
else substring(@sql,charindex('.',@sql)+1,1000) end
,@sql=case when charindex('/',@sql)=0 then @sql
else left(@sql,charindex('/',@sql)-1) end
,@dbname=reverse(@sql)
--得到恢復后的數據庫存放目錄
if isnull(@dbpath,'')=''
begin
select @dbpath=rtrim(reverse(filename)) from master..sysfiles where name='master'
select @dbpath=reverse(substring(@dbpath,charindex('/',@dbpath),4000))
end
--生成數據庫恢復語句
set @sql='restore '+case @retype when 'log' then 'log ' else 'database ' [email protected]
+' from disk='''[email protected]+''''
+' with file='+cast(@filenumber as varchar)
+case when @overexist=1 and @retype in('db','dbnor') then ',replace' else '' end
+case @retype when 'dbnor' then ',norecovery' else ',recovery' end
+case isnull(@password,'') when '' then '' else ',password='''[email protected]+'''' end
--添加移動邏輯文件的處理
if @retype='db' or @retype='dbnor'
begin
--從備份文件中獲取邏輯文件名
declare @lfn nvarchar(128),@tp char(1),@i int,@s varchar(1000)
--創建臨時表,保存獲取的信息
create table #tb(ln nvarchar(128),pn nvarchar(260),tp char(1),fgn nvarchar(128),sz numeric(20,0),msz numeric(20,0))
--從備份文件中獲取信息
set @s='restore filelistonly from disk='''[email protected]+''''
++case isnull(@password,'') when '' then '' else ' with password='''[email protected]+'''' end
insert into #tb exec(@s)
declare #f cursor for select ln,tp from #tb
open #f
fetch next from #f into @lfn,@tp
set @i=0
while @@fetch_status=0
begin
select @[email protected]+',move '''[email protected]+''' to '''[email protected][email protected]+cast(@i as varchar)
+case @tp when 'd' then '.mdf''' else '.ldf''' end
,@[email protected]+1
fetch next from #f into @lfn,@tp
end
close #f
deallocate #f
end
--關閉用戶進程處理
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)
go
鄒建說:
說白了,就是備份數據庫和還原數據庫的sql語句的應用:
--備份
backup database 數據庫 to disk='c:/你的備份文件名'
--還原
restore database 數據庫 from disk='c:/你的備份文件名'
ps:鄒建老大真是我的偶像阿!