Skip to content

Commit 602948d

Browse files
committed
attempt to add feature / fix issue #1672
1 parent 8a7da67 commit 602948d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

sp_DatabaseRestore.sql

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,22 @@ WHERE BackupFile LIKE N'%.bak'
475475
BackupFile LIKE N'%' + @Database + N'%'
476476
AND
477477
(@StopAt IS NULL OR REPLACE(LEFT(RIGHT(BackupFile, 19), 15),'_','') <= @StopAt);
478+
/* To get all backups that belong to the same set we can do two things:
479+
1. RESTORE HEADERONLY of ALL backup files in the folder and look for BackupSetGUID.
480+
Backups that belong to the same split will have the same BackupSetGUID.
481+
2. Olla Hallengren's solution appends file index at the end of the name:
482+
SQLSERVER1_TEST_DB_FULL_20180703_213211_1.bak
483+
SQLSERVER1_TEST_DB_FULL_20180703_213211_2.bak
484+
SQLSERVER1_TEST_DB_FULL_20180703_213211_N.bak
485+
We can and find all related files with the same timestamp but different index.
486+
This option is simpler and requires less changes to this procedure */
487+
SELECT BackupFile
488+
INTO #SplitBackups
489+
FROM @FileList
490+
WHERE LEFT(BackupFile,LEN(BackupFile)-PATINDEX('%[_]%',REVERSE(BackupFile))) = LEFT(@LastFullBackup,LEN(@LastFullBackup)-PATINDEX('%[_]%',REVERSE(@LastFullBackup)))
491+
AND PATINDEX('%[_]%',REVERSE(@LastFullBackup)) <= 7 -- there is a 1 or 2 digit index at the end of the string which indicates split backups. Olla only supports up to 64 file split.
478492

493+
-- File list can be obtained by running RESTORE FILELISTONLY of any file from the given BackupSet therefore we do not have to cater for split backups when building @FileListParamSQL
479494

480495
SET @FileListParamSQL =
481496
N'INSERT INTO #FileListParameters WITH (TABLOCK)
@@ -504,6 +519,7 @@ EXEC (@sql);
504519
IF @Debug = 1
505520
BEGIN
506521
SELECT '#FileListParameters' AS table_name, * FROM #FileListParameters
522+
SELECT '#SplitBackups' AS table_name, * FROM #SplitBackups
507523
END
508524

509525

@@ -621,7 +637,20 @@ IF @ContinueLogs = 0
621637

622638
IF @Execute = 'Y' RAISERROR('@ContinueLogs set to 0', 0, 1) WITH NOWAIT;
623639

624-
SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathFull + @LastFullBackup + N''' WITH NORECOVERY, REPLACE' + @MoveOption + NCHAR(13);
640+
/* now take split backups into account */
641+
IF (SELECT COUNT(*) FROM #SplitBackups) > 0
642+
BEGIN
643+
RAISERROR('Split backups found', 0, 1) WITH NOWAIT
644+
SELECT @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM ' + STUFF((
645+
SELECT CHAR(10) + ',DISK=''' + @BackupPathFull + BackupFile + ''''
646+
FROM #SplitBackups
647+
ORDER BY BackupFile
648+
FOR XML PATH('')),1,2,'') + N' WITH NORECOVERY, REPLACE' + @MoveOption + NCHAR(13);
649+
END
650+
ELSE
651+
BEGIN
652+
SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathFull + @LastFullBackup + N''' WITH NORECOVERY, REPLACE' + @MoveOption + NCHAR(13);
653+
END
625654

626655
IF @Debug = 1 OR @Execute = 'N'
627656
BEGIN

0 commit comments

Comments
 (0)