MS SQL数据库备份和恢复存储过程

本文提供了一个SQL Server数据库备份和恢复的实用示例,包括创建存储过程进行数据库备份、获取文件路径及通过存储过程实现数据库恢复等功能。
 
ExpandedBlockStart.gifContractedBlock.gif/**//*备份数据库*/
None.gif
create proc pr_backup_db
None.gif@flag 
varchar(20) out,
None.gif@backup_db_name 
varchar(128),
None.gif@filename 
varchar(1000)  --路径+文件名字
None.gif
as
None.gif
declare @sql nvarchar(4000),@par nvarchar(1000)
None.gif
if not exists(
None.gif 
select * from master..sysdatabases
None.gif  
where name=@backup_db_name
None.gif  )
None.gif
begin
ExpandedBlockStart.gifContractedBlock.gif 
select @flag='db not exist'  /**//*数据库不存在*/
None.gif 
return
None.gif
end
None.gif
else
None.gif
begin
None.gif 
if right(@filename,1)<>'\' and charindex('\',@filename)<>0
None.gif 
begin
None.gif  
select @par='@filename varchar(1000)'
None.gif  
select @sql='BACKUP DATABASE '+@backup_db_name
                         +' to disk=@filename with init'
None.gif  
execute sp_executesql @sql,@par,@filename
None.gif  
select @flag='ok' 
None.gif  
return
None.gif 
end
None.gif 
else
None.gif 
begin
ExpandedBlockStart.gifContractedBlock.gif  
select @flag='file type error'  /**//*参数@filename输入格式错误*/
None.gif  
return
None.gif 
end
None.gif
end
None.gif
None.gif
GO
None.gif
None.gif

 

ExpandedBlockStart.gifContractedBlock.gif/**//*创建函数,得到文件得路径*/
None.gif
create function fn_GetFilePath(@filename nvarchar(260))
None.gif
returns nvarchar(260)   
None.gif
as
None.gif
begin
None.gif 
declare @file_path nvarchar(260)
None.gif 
declare @filename_reverse nvarchar(260)
None.gif 
select @filename_reverse=reverse(@filename)
None.gif 
select @file_path=substring(@filename,1,len(@filename)+1-charindex('',@filename_reverse))
None.gif 
return @file_path
None.gif
end
None.gif
None.gif
None.gif
GO
None.gif
None.gif

None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*恢复数据库*/
None.gif
CREATE  proc pr_restore_db    
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif------------------------------------------------
InBlock.gifCreate Time:    2004-03-20 
InBlock.gifUpdate Time:    2004-03-29 11:05
InBlock.gifAuthor:         aierong
InBlock.gifRemark:         恢复数据库
InBlock.gif  
InBlock.gif------------------------------------------------
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifContractedBlock.gif
/**//*过程运行的状态标志,是输入参数*/      
None.gif@flag 
varchar(20) out,    
ExpandedBlockStart.gifContractedBlock.gif
/**//*要恢复的数据名字*/
None.gif@restore_db_name 
nvarchar(128), 
ExpandedBlockStart.gifContractedBlock.gif
/**//*备份文件存放的路径+备份文件名字*/ 
None.gif@filename 
nvarchar(260)         
None.gif
as
ExpandedBlockStart.gifContractedBlock.gif
/**//*返回系统存储过程xp_cmdshell运行结果*/
None.gif
declare @proc_result tinyint 
ExpandedBlockStart.gifContractedBlock.gif
/**//*循环次数*/ 
None.gif
declare @loop_time smallint  
ExpandedBlockStart.gifContractedBlock.gif
/**//*@tem表的ids列最大数*/
None.gif
declare @max_ids smallint    
ExpandedBlockStart.gifContractedBlock.gif
/**//*原数据库存放路径*/
None.gif
declare @file_bak_path nvarchar(260)  
ExpandedBlockStart.gifContractedBlock.gif
/**//*文件存放标志*/
None.gif
declare @flag_file bit   
ExpandedBlockStart.gifContractedBlock.gif
/**//*数据库master文件路径*/
None.gif
declare @master_path nvarchar(260)  
None.gif
declare @sql nvarchar(4000),@par nvarchar(1000)
None.gif
declare @sql_sub nvarchar(4000)
None.gif
declare @sql_cmd nvarchar(100
None.gif
declare @sql_kill nvarchar(100
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif判断参数@filename文件格式合法性,以防止用户输入类似d: 或者 c:a 等非法文件名
InBlock.gif参数@filename里面必须有''并且不以''结尾
ExpandedBlockEnd.gif
*/

None.gif
if right(@filename,1)<>'' and charindex('',@filename)<>0
None.gif
begin 
None.gif 
select @sql_cmd='dir '+@filename
None.gif 
EXEC @proc_result = master..xp_cmdshell @sql_cmd,no_output
ExpandedBlockStart.gifContractedBlock.gif 
/**//*系统存储过程xp_cmdshell返回代码值:0(成功)或1(失败)*/
None.gif 
IF (@proc_result<>0)  
None.gif 
begin
ExpandedBlockStart.gifContractedBlock.gif  
/**//*备份文件不存在*/
None.gif  
select @flag='not exist'   
ExpandedBlockStart.gifContractedBlock.gif  
/**//*退出过程*/
None.gif  
return  
None.gif 
end
ExpandedBlockStart.gifContractedBlock.gif 
/**//*创建临时表,保存由备份集内包含的数据库和日志文件列表组成的结果集*/
None.gif 
create table #tem(
ExpandedBlockStart.gifContractedBlock.gif     
/**//*文件的逻辑名称*/ 
None.gif     LogicalName 
nvarchar(128), 
ExpandedBlockStart.gifContractedBlock.gif     
/**//*文件的物理名称或操作系统名称*/
None.gif     PhysicalName 
nvarchar(260) , 
ExpandedBlockStart.gifContractedBlock.gif            
/**//*数据文件 (D) 或日志文件 (L)*/
None.gif     Type 
char(1),  
ExpandedBlockStart.gifContractedBlock.gif     
/**//*包含文件的文件组名称*/
None.gif     FileGroupName 
nvarchar(128), 
ExpandedBlockStart.gifContractedBlock.gif                          
/**//*当前大小(以字节为单位)*/
None.gif     
[Size] numeric(20,0),  
ExpandedBlockStart.gifContractedBlock.gif     
/**//*允许的最大大小(以字节为单位)*/
None.gif     
[MaxSize] numeric(20,0)  
None.gif   )
ExpandedBlockStart.gifContractedBlock.gif 
/**//*
InBlock.gif 创建表变量,表结构与临时表基本一样
InBlock.gif 就是多了两列,
InBlock.gif 列ids(自增编号列),
InBlock.gif 列file_path,存放文件的路径
ExpandedBlockEnd.gif 
*/

None.gif 
declare @tem table(       
ExpandedBlockStart.gifContractedBlock.gif     
/**//*自增编号列*/
None.gif     ids 
smallint identity,  
None.gif     LogicalName 
nvarchar(128), 
None.gif     PhysicalName 
nvarchar(260), 
None.gif     File_path 
nvarchar(260), 
None.gif     Type 
char(1),  
None.gif     FileGroupName 
nvarchar(128)
None.gif   )
None.gif 
insert into #tem 
None.gif  
execute('restore filelistonly from disk='''+@filename+'''')
ExpandedBlockStart.gifContractedBlock.gif 
/**//*将临时表导入表变量中,并且计算出相应得路径*/
None.gif 
insert into @tem(LogicalName,PhysicalName,File_path,Type,FileGroupName)  
None.gif  
select LogicalName,PhysicalName,dbo.fn_GetFilePath(PhysicalName),Type,FileGroupName 
None.gif   
from #tem
None.gif 
if @@rowcount>0 
None.gif 
begin
None.gif  
drop table #tem
None.gif 
end
None.gif 
select @loop_time=1
ExpandedBlockStart.gifContractedBlock.gif 
/**//*@tem表的ids列最大数*/
None.gif 
select @max_ids=max(ids)  
None.gif  
from @tem
None.gif 
while @loop_time<=@max_ids
None.gif 
begin
None.gif  
select @file_bak_path=file_path 
None.gif   
from @tem where ids=@loop_time
None.gif  
select @sql_cmd='dir '+@file_bak_path
None.gif  
EXEC @proc_result = master..xp_cmdshell @sql_cmd,no_output
ExpandedBlockStart.gifContractedBlock.gif  
/**//*系统存储过程xp_cmdshell返回代码值:0(成功)或1(失败)*/
None.gif  
IF (@proc_result<>0
None.gif   
select @loop_time=@loop_time+1  
None.gif  
else
ExpandedBlockStart.gifContractedBlock.gif   
/**//*没有找到备份前数据文件原有存放路径,退出循环*/
None.gif   
BREAK 
None.gif 
end
None.gif 
select @master_path=''
None.gif 
if @loop_time>@max_ids 
ExpandedBlockStart.gifContractedBlock.gif  
/**//*备份前数据文件原有存放路径存在*/
None.gif  
select @flag_file=1   
None.gif 
else
None.gif 
begin
ExpandedBlockStart.gifContractedBlock.gif  
/**//*备份前数据文件原有存放路径不存在*/
None.gif  
select @flag_file=0  
None.gif  
select @master_path=dbo.fn_GetFilePath(filename)
None.gif   
from master..sysdatabases 
None.gif   
where name='master'
None.gif 
end
None.gif 
select @sql_sub=''
ExpandedBlockStart.gifContractedBlock.gif 
/**//*type='d'是数据文件,type='l'是日志文件 */
ExpandedBlockStart.gifContractedBlock.gif 
/**//*@flag_file=1时新的数据库文件还是存放在原来路径,否则存放路径和master数据库路径一样*/
None.gif 
select @sql_sub=@sql_sub+'move '''+LogicalName+''' to '''
None.gif   
+case type 
None.gif         
when 'd' then case @flag_file 
None.gif             
when 1 then  File_path 
None.gif      
else @master_path 
None.gif          
end    
None.gif         
when 'l' then case  @flag_file
None.gif      
when 1 then  File_path 
None.gif      
else @master_path 
None.gif          
end    
None.gif   
end
None.gif   
+case type
None.gif    
when 'd' then @restore_db_name
None.gif           
+'_DATA'
ExpandedBlockStart.gifContractedBlock.gif           
/**//*给文件编号*/ 
None.gif           
+convert(sysname,ids)  
None.gif           
+'.'
ExpandedBlockStart.gifContractedBlock.gif           
/**//*给文件加入后缀名,mdf or ndf*/ 
None.gif           
+right(PhysicalName,3)  
None.gif           
+''','  
None.gif    
when 'l' then @restore_db_name
None.gif           
+'_LOG'
ExpandedBlockStart.gifContractedBlock.gif           
/**//*给文件编号*/ 
None.gif           
+convert(sysname,ids)   
None.gif           
+'.'
ExpandedBlockStart.gifContractedBlock.gif           
/**//*给文件加入后缀名,mdf or ndf*/ 
None.gif           
+right(PhysicalName,3)  
None.gif           
+''','  
None.gif    
end
None.gif   
from @tem
None.gif 
select @sql='RESTORE DATABASE @db_name '
                        +'FROM DISK=@filename with 
'
None.gif 
select @sql=@sql+@sql_sub+'replace'
None.gif 
select @par='@db_name nvarchar(128),@filename nvarchar(260)'
ExpandedBlockStart.gifContractedBlock.gif 
/**//*关闭相关进程,把相应进程状况导入临时表中*/
None.gif 
select identity(int,1,1) ids, spid 
None.gif  
into #temp
None.gif  
from master..sysprocesses
None.gif  
where dbid=db_id(@restore_db_name)
ExpandedBlockStart.gifContractedBlock.gif 
/**//*找到相应进程*/
None.gif 
if @@rowcount>0 
None.gif 
begin   
None.gif  
select @max_ids=max(ids) 
None.gif   
from #temp
None.gif  
select @loop_time=1
None.gif  
while @loop_time<=@max_ids
None.gif  
begin
None.gif   
select @sql_kill='kill '+convert(nvarchar(20),spid) 
None.gif    
from #temp
None.gif    
where ids=@loop_time
None.gif   
execute sp_executesql @sql_kill
None.gif   
select @loop_time=@loop_time+1 
None.gif  
end
None.gif 
end 
None.gif 
drop table #temp
None.gif 
execute sp_executesql @sql,
                                      @par,
                                      @db_name
=@restore_db_name,
                                      @filename
=@filename
ExpandedBlockStart.gifContractedBlock.gif 
/**//*操作成功*/
None.gif 
select @flag='ok'   
None.gif
end
None.gif
else
None.gif
begin
ExpandedBlockStart.gifContractedBlock.gif 
/**//*参数@filename输入格式错误*/
None.gif 
SELECT @flag='file type error'  
None.gif
end
None.gif
None.gif
None.gif
GO
None.gif
/*运行*/

--备份数据库test_database
declare @fl varchar(10)
execute pr_backup_db @fl out,'test_database','c:\test_database.bak'
select @fl

--恢复数据库,输入的参数错误
declare @fl varchar(20)
exec pr_restore_db @fl out,'sa','c:\'
select @fl


--恢复数据库,即创建数据库test_database的复本test_db
declare @fl varchar(20)
exec pr_restore_db @fl out,'test_db','c:\test_database.bak'
select @fl

转载于:https://www.cnblogs.com/hzuIT/articles/483668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值