欢迎光临
我们一直在努力

编程实现备份和还原数据库-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

 注意,下面备份还原都是用存储过程实现!
 
if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[p_backupdb]) and objectproperty(id, nisprocedure) = 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, nisprocedure) = 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 +@dbname
  + to disk=+@bkpath+@bkfname
  + 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=+@password+ end
 exec(@sql)
go
 
if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[p_restoredb]) and objectproperty(id, nisprocedure) = 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 end+@dbname
 + from disk=+@bkfile+
 + 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=+@password+ 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=+@bkfile+
  ++case isnull(@password,) when then else with password=+@password+ 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 @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
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:邹建老大真是我的偶像阿!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 编程实现备份和还原数据库-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章