差异备份的存储过程

有时候有这样的需求,需要在做change前备份所有数据库.我们可以使用挨个数据库做全备,但是如果数据库非常多,并且数据库很大,这就非常麻烦,也非常耗费时间了. 所以写了下面的存储过程,使用游标遍历所有数据库,检查数据库有没有全备,如果有的话,再检查磁盘上全备的文件是否可用,如果都没有问题,就对数据库做差异备份.如果检查有问题,就把问题打印出来.

存储过程可以接收参数作为备份路径.如果没有参数就使用默认路径D:\backups\ .

使用下面的存储过程判断文件是否存在.

EXEC xp_fileexist @strphysical_device_name, @result output

CREATE PROCEDURE sp_generateDifferentialBackups 
   @strBackupPath NVARCHAR (25) ='D:\backups\'--variable for location of DIFFERENTIAL backups 
AS 
SET NOCOUNT ON  
-- Get the name of all user databases   
DECLARE @strTimeStamp NVARCHAR(12)   --variable for timestamp value  
DECLARE @strSQL NVARCHAR(100)        -- used for generating dynamic SQL statements  
DECLARE @databaseName NVARCHAR(128)  -- used as variable to store database names  
DECLARE dbCursor CURSOR FOR       -- used for cursor allocation  
   SELECT NAME 
   FROM MASTER.SYS.DATABASES 
   WHERE [database_id] > 0 
   AND NAME NOT IN ('MASTER','MODEL','MSDB','TEMPDB') --does not include the system databases  

OPEN dbCursor  
  
FETCH NEXT FROM dbCursor INTO @databaseName  
  
WHILE (@@FETCH_STATUS = 0)  
BEGIN  
   PRINT 'Checking for the latest FULL database backup for: [' + @databaseName +']' 
      
   DECLARE @strphysical_device_name NVARCHAR(100) -- variable for physical_device_name  
   DECLARE @cursor NVARCHAR(400)  

   -- Reads the MSDB database to check for the latest FULL database backup        
   SELECT @cursor=('DECLARE TabCursor CURSOR FAST_FORWARD GLOBAL FOR ' +  
   'SELECT TOP 1 physical_device_name  
   FROM msdb.dbo.backupset a INNER JOIN msdb.dbo.backupmediafamily b     
   ON a.media_set_id = b.media_set_id     
   WHERE database_name=''' + @databaseName + '''  
   AND type=''D''  
   ORDER BY backup_finish_date desc')  
   EXEC sp_executesql @cursor  
   OPEN TabCursor  
  
   FETCH NEXT FROM TabCursor INTO @strphysical_device_name  
   --Check if the database does not have any FULL backups at all 
   IF @@FETCH_STATUS <> 0 
   BEGIN 
      PRINT '*****WARNING: Database [' + @databaseName + '] does not have any FULL database backups at all. Generate the FULL database backup first. Aborting DIFFERENTIAL backup command.*****' 
   END 
   ELSE    
   --If the database has FULL backups, 
   WHILE @@FETCH_STATUS = 0  
   BEGIN  
      DECLARE @result INT  
      EXEC xp_fileexist @strphysical_device_name, @result output   
      IF (@result = 1)  --@result will return 1 if the specified file exists, if it doesn't, it will return 0  
      BEGIN  
         SET @strTimeStamp=CONVERT(CHAR(8), GETDATE(), 112)  
         SET @strTimeStamp=@strTimeStamp + REPLACE(CONVERT(CHAR(8), GETDATE(), 108),':','')  
         EXEC ('BACKUP DATABASE ' + @databaseName + ' TO DISK=''' + @strBackupPath + @databaseName + '_DIFF_' + @strTimeStamp + '.BAK'' WITH INIT, DIFFERENTIAL, DESCRIPTION=''DIFFERENTIAL Backup for ' + @databasename + ' database''') -- change this value should you decide to change the backup type to something other than differential   
         PRINT '==========================================================================================='  
         PRINT 'DIFFERENTIAL Backup generated for database: [' + @databaseName  + ']' 
         PRINT 'Corresponding FULL database backup: ' + @strphysical_device_name  
         PRINT 'DIFFERENTIAL database backup: ' + @strBackupPath + @databaseName + '_DIFF_' + @strTimeStamp + '.BAK'  
      END  
      ELSE  
         PRINT  '*********WARNING: FULL database backup file is missing. Generate the FULL database backup first. Aborting DIFFERENTIAL backup command.*********'  
         
      FETCH NEXT FROM TabCursor INTO @strphysical_device_name  
   END  
   CLOSE TabCursor  
   DEALLOCATE TabCursor  
       
   PRINT '==========================================================================================='  
   PRINT '                                                                                           '  
   PRINT '                                                                                           '  
   PRINT '                                                                                           '  
   
   FETCH NEXT FROM dbCursor INTO @databaseName  
END  
  
CLOSE dbCursor  
DEALLOCATE dbCursor  

PRINT 'FINISHED'  
GO   

 

posted on 2018-06-21 15:26  (_OwO_)  阅读(258)  评论(0编辑  收藏  举报