Advertisement
Merzavets

SQL backup and restore (robust strategy)

Mar 15th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.75 KB | None | 0 0
  1. /***
  2. copied from https://www.mssqltips.com/sqlservertip/1584/auto-generate-sql-server-restore-script-from-backup-files-in-a-directory/
  3.  
  4. Before we get started the script below assumes the following:
  5.  
  6. - The restored database will have the same name as the backed up database
  7. - The restored database will be restored in the same location as the backed up database
  8. - The files have the following naming format
  9.       dbName_YYYYMMDDHHMM.xxx
  10. - File extensions are as follows
  11.       Full backup - BAK
  12.       Differential backup - DIF
  13.       Transaction log backup - TRN
  14. - XP_CMDSHELL is enabled
  15. - There are no missing transaction logs that may break the restore chain
  16. ***/
  17.  
  18. USE Master;
  19. GO  
  20. SET NOCOUNT ON
  21.  
  22. -- 1 - Variable declaration
  23. DECLARE @dbName sysname
  24. DECLARE @backupPath NVARCHAR(500)
  25. DECLARE @cmd NVARCHAR(500)
  26. DECLARE @fileList TABLE (backupFile NVARCHAR(255))
  27. DECLARE @lastFullBackup NVARCHAR(500)
  28. DECLARE @lastDiffBackup NVARCHAR(500)
  29. DECLARE @backupFile NVARCHAR(500)
  30.  
  31. -- 2 - Initialize variables
  32. SET @dbName = 'Customer'
  33. SET @backupPath = 'D:\SQLBackups\'
  34.  
  35. -- 3 - get list of files
  36. SET @cmd = 'DIR /b "' + @backupPath + '"'
  37.  
  38. INSERT INTO @fileList(backupFile)
  39. EXEC master.sys.xp_cmdshell @cmd
  40.  
  41. -- 4 - Find latest full backup
  42. SELECT @lastFullBackup = MAX(backupFile)  
  43. FROM @fileList  
  44. WHERE backupFile LIKE '%.BAK'  
  45.    AND backupFile LIKE @dbName + '%'
  46.  
  47. SET @cmd = 'RESTORE DATABASE [' + @dbName + '] FROM DISK = '''  
  48.        + @backupPath + @lastFullBackup + ''' WITH NORECOVERY, REPLACE'
  49. PRINT @cmd
  50.  
  51. -- 4 - Find latest diff backup
  52. SELECT @lastDiffBackup = MAX(backupFile)  
  53. FROM @fileList  
  54. WHERE backupFile LIKE '%.DIF'  
  55.    AND backupFile LIKE @dbName + '%'
  56.    AND backupFile > @lastFullBackup
  57.  
  58. -- check to make sure there is a diff backup
  59. IF @lastDiffBackup IS NOT NULL
  60. BEGIN
  61.    SET @cmd = 'RESTORE DATABASE [' + @dbName + '] FROM DISK = '''  
  62.        + @backupPath + @lastDiffBackup + ''' WITH NORECOVERY'
  63.    PRINT @cmd
  64.    SET @lastFullBackup = @lastDiffBackup
  65. END
  66.  
  67. -- 5 - check for log backups
  68. DECLARE backupFiles CURSOR FOR  
  69.    SELECT backupFile  
  70.    FROM @fileList
  71.    WHERE backupFile LIKE '%.TRN'  
  72.    AND backupFile LIKE @dbName + '%'
  73.    AND backupFile > @lastFullBackup
  74.  
  75. OPEN backupFiles  
  76.  
  77. -- Loop through all the files for the database  
  78. FETCH NEXT FROM backupFiles INTO @backupFile  
  79.  
  80. WHILE @@FETCH_STATUS = 0  
  81. BEGIN  
  82.    SET @cmd = 'RESTORE LOG [' + @dbName + '] FROM DISK = '''  
  83.        + @backupPath + @backupFile + ''' WITH NORECOVERY'
  84.    PRINT @cmd
  85.    FETCH NEXT FROM backupFiles INTO @backupFile  
  86. END
  87.  
  88. CLOSE backupFiles  
  89. DEALLOCATE backupFiles  
  90.  
  91. -- 6 - put database in a useable state
  92. SET @cmd = 'RESTORE DATABASE [' + @dbName + '] WITH RECOVERY'
  93. PRINT @cmd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement