データベースのバックアップとリカバリのストレージ・プロシージャ




             ,                      。 
                    。        。     

  T-SQL  ,               

   SQL Server       : 

1.   SQL Server          

2.   SQL      

3.   SQL     ,                      

4.     SQL      



/*1.--           

@dbname              
          ,    SQL           
    NULL,      SQL      
*/ 

/*--     
select        =dbo.f_getdbpath('tempdb') 
,[  SQL SERVER    ]=dbo.f_getdbpath('') 
,[  SQL SERVER    ]=dbo.f_getdbpath(null) 
--*/ 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdbpath]') and xtype in (N'FN', N'IF', N'TF')) 
drop function [dbo].[f_getdbpath] 
GO 

create function f_getdbpath(@dbname sysname) 
returns nvarchar(260) 
as 
begin 
declare @re nvarchar(260) 
if @dbname is null or db_id(@dbname) is null 
select @re=rtrim(reverse(filename)) from master..sysdatabases where name='master' 
else 
select @re=rtrim(reverse(filename)) from master..sysdatabases where name=@dbname 

if @dbname is null 
set @re=reverse(substring(@re,charindex('\',@re)+5,260))+'BACKUP' 
else 
set @re=reverse(substring(@re,charindex('\',@re),260)) 
return(@re) 
end 
go 


/*2.--      

*/ 

/*--     

--        
exec p_backupdb @bkpath='c:\',@bkfname='db_\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' 

--*/ 

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 

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 --  /       
as 
declare @sql varchar(8000) 
if isnull(@dbname,'')='' set @dbname=db_name() 
if isnull(@bkpath,'')='' set @bkpath=dbo.f_getdbpath(null) 
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 
print @sql 
exec(@sql) 
go 



/*3.--      

*/ 

/*--     
--        
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_backupdb @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_backupdb @bkfile='c:\db_20031015_log.bak',@dbname='db',@retype='LOG' 

--*/ 

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 

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  
@killuser bit=1 --          , @overexist=1    
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,'')='' set @dbpath=dbo.f_getdbpath('') 

--          
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 
print @sql 
--            
if @retype='DB' or @retype='DBNOR' 
begin 
--              
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='''+@bkfile+'''') 
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 @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) 

go 

/*4.--     

*/ 

/*--     

--        
exec p_createjob @jobname='mm',@sql='select * from syscolumns',@freqtype='month' 

--        
exec p_createjob @jobname='ww',@sql='select * from syscolumns',@freqtype='week' 

--        
exec p_createjob @jobname='a',@sql='select * from syscolumns' 

--       ,   4        
exec p_createjob @jobname='b',@sql='select * from syscolumns',@fsinterval=4 

--*/ 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_createjob]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 
drop procedure [dbo].[p_createjob] 
GO 

create proc p_createjob 
@jobname varchar(100), --     
@sql varchar(8000), --       
@dbname sysname='', --           
@freqtype varchar(6)='day', --    ,month  ,week  ,day   
@fsinterval int=1, --           
@time int=170000 --      ,         ,  0  23:59  
as 
if isnull(@dbname,'')='' set @dbname=db_name() 

--     
exec msdb..sp_add_job @job_name=@jobname 

--       
exec msdb..sp_add_jobstep @job_name=@jobname, 
@step_name = '    ', 
@subsystem = 'TSQL', 
@database_name=@dbname, 
@command = @sql, 
@retry_attempts = 5, --     
@retry_interval = 5 --     

--     
declare @ftype int,@fstype int,@ffactor int 
select @ftype=case @freqtype when 'day' then 4 
when 'week' then 8 
when 'month' then 16 end 
,@fstype=case @fsinterval when 1 then 0 else 8 end 
if @fsinterval<>1 set @time=0 
set @ffactor=case @freqtype when 'day' then 0 else 1 end 

EXEC msdb..sp_add_jobschedule @job_name=@jobname, 
@name = '    ', 
@freq_type=@ftype , --  ,8   ,16    
@freq_interval=1, --       
@freq_subday_type=@fstype, --       
@freq_subday_interval=@fsinterval, --     
@freq_recurrence_factor=@ffactor, 
@active_start_time=@time --  17:00:00    

go 

/*--    --    : 
    (       )+    (      )+    ( 2      ) 

             
--*/ 

declare @sql varchar(8000) 
--    (       ) 
set @sql='exec p_backupdb @dbname=''        ''' 
exec p_createjob @jobname='    ',@sql,@freqtype='week' 

--    (      ) 
set @sql='exec p_backupdb @dbname=''        '',@bktype='DF'' 
exec p_createjob @jobname='      ',@sql,@freqtype='day' 

--    ( 2      ) 
set @sql='exec p_backupdb @dbname=''        '',@bktype='LOG'' 
exec p_createjob @jobname=' 2      ',@sql,@freqtype='day',@fsinterval=2 

/*--    2 

       :PRODUCE 

      : 
1.      ,   PRODUCE       ,    ,     
2.      ,     :    ,    ,     
3.      ,                  。 

  :    produce           ,                  TABLE  。 
--*/ 

declare @sql varchar(8000) 

--1.                  ,   1   16:40   : 
set @sql=' 
declare @path nvarchar(260),@fname nvarchar(100) 
set @fname=''PRODUCE_''+convert(varchar(10),getdate(),112)+''_m.bak'' 
set @path=dbo.f_getdbpath(null)+@fname 

--   
exec p_backupdb @dbname=''PRODUCE'',@bkfname=@fname 

--           
exec p_RestoreDb @bkfile=@path,@dbname=''PRODUCE_ '' 

--               
exec p_RestoreDb @bkfile=@path,@dbname=''PRODUCE_ '',@retype=''DBNOR'' 

--               
exec p_RestoreDb @bkfile=@path,@dbname=''PRODUCE_ '',@retype=''DBNOR'' 
' 
exec p_createjob @jobname='    ',@sql,@freqtype='month',@time=164000 

--2.                    ,     17:00   : 
set @sql=' 
declare @path nvarchar(260),@fname nvarchar(100) 
set @fname=''PRODUCE_''+convert(varchar(10),getdate(),112)+''_w.bak'' 
set @path=dbo.f_getdbpath(null)+@fname 

--     
exec p_backupdb @dbname=''PRODUCE'',@bkfname=@fname,@bktype=''DF'' 

--         
exec p_backupdb @bkfile=@path,@dbname=''PRODUCE_ '',@retype=''DF'' 
' 
exec p_createjob @jobname='      ',@sql,@freqtype='week',@time=170000 

--3.                    ,     17:15   : 
set @sql=' 
declare @path nvarchar(260),@fname nvarchar(100) 
set @fname=''PRODUCE_''+convert(varchar(10),getdate(),112)+''_l.bak'' 
set @path=dbo.f_getdbpath(null)+@fname 

--     
exec p_backupdb @dbname=''PRODUCE'',@bkfname=@fname,@bktype=''LOG'' 

--         
exec p_backupdb @bkfile=@path,@dbname=''PRODUCE_ '',@retype=''LOG'' 
' 
exec p_createjob @jobname='      ',@sql,@freqtype='day',@time=171500