Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c3cd89

Browse files
Merge pull request BrentOzarULTD#1251 from James-DBA-Anderson/dev
Add standby mode for log restores to sp_DatabaseRestore
2 parents 84a9c6d + 69df0e3 commit 1c3cd89

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

‎sp_DatabaseRestore.sql‎

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
IF OBJECT_ID('dbo.sp_DatabaseRestore') IS NULL
2-
EXEC ('CREATE PROCEDURE dbo.sp_DatabaseRestore AS RETURN 0;');
2+
EXEC ('CREATE PROCEDURE dbo.sp_DatabaseRestore AS RETURN 0;');
33
GO
44

55
ALTER PROCEDURE [dbo].[sp_DatabaseRestore]
@@ -15,8 +15,10 @@ ALTER PROCEDURE [dbo].[sp_DatabaseRestore]
1515
@RunCheckDB BIT = 0,
1616
@RestoreDiff BIT = 0,
1717
@ContinueLogs BIT = 0,
18+
@StandbyMode BIT = 0,
19+
@StandbyUndoPath NVARCHAR(MAX) = NULL,
1820
@RunRecovery BIT = 0,
19-
@ForceSimpleRecovery BIT = 0,
21+
@ForceSimpleRecovery BIT = 0,
2022
@StopAt NVARCHAR(14) = NULL,
2123
@OnlyLogsAfter NVARCHAR(14) = NULL,
2224
@Debug INT = 0,
@@ -136,6 +138,16 @@ IF @Help = 1
136138
@TestRestore = 1,
137139
@RunCheckDB = 1,
138140
@Debug = 0;
141+
142+
EXEC dbo.sp_DatabaseRestore
143+
@Database = ''LogShipMe'',
144+
@BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'',
145+
@BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'',
146+
@StandbyMode = 1,
147+
@StandbyUndoPath = ''D:\Data\'',
148+
@ContinueLogs = 1,
149+
@RunRecovery = 0,
150+
@Debug = 0;
139151
140152
--This example will restore the latest differential backup, and stop transaction logs at the specified date time. It will also only print the commands.
141153
EXEC dbo.sp_DatabaseRestore
@@ -192,7 +204,8 @@ DECLARE @cmd NVARCHAR(4000) = N'', --Holds xp_cmdshell command
192204
@FullLastLSN NUMERIC(25, 0), --LSN for full
193205
@DiffLastLSN NUMERIC(25, 0), --LSN for diff
194206
@HeadersSQL AS NVARCHAR(4000) = N'', --Dynamic insert into #Headers table (deals with varying results from RESTORE FILELISTONLY across different versions)
195-
@MoveOption AS NVARCHAR(MAX)= N'', --If you need to move restored files to a different directory
207+
@MoveOption AS NVARCHAR(MAX) = N'', --If you need to move restored files to a different directory
208+
@LogRecoveryOption AS NVARCHAR(MAX) = N'', --Holds the option to cause logs to be restored in standby mode or with no recovery
196209
@DatabaseLastLSN NUMERIC(25, 0), --redo_start_lsn of the current database
197210
@i TINYINT = 1, --Maintains loop to continue logs
198211
@LogFirstLSN NUMERIC(25, 0), --Holds first LSN in log backup headers
@@ -350,6 +363,12 @@ IF (SELECT RIGHT(@MoveLogDrive, 1)) <> '\' --Has to end in a '\'
350363
SET @MoveLogDrive += N'\';
351364
END;
352365

366+
/*Standby Undo File*/
367+
IF (SELECT RIGHT(@StandbyUndoPath, 1)) <> '\' --Has to end in a '\'
368+
BEGIN
369+
RAISERROR('Fixing @StandbyUndoPath to add a "\"', 0, 1) WITH NOWAIT;
370+
SET @StandbyUndoPath += N'\';
371+
END;
353372

354373

355374
IF @RestoreDatabaseName IS NULL
@@ -366,7 +385,7 @@ IF @BackupPathFull IS NOT NULL
366385

367386
BEGIN
368387

369-
-- get list of files
388+
-- Get list of files
370389
SET @cmd = N'DIR /b "' + @BackupPathFull + N'"';
371390

372391
IF @Debug = 1
@@ -470,7 +489,7 @@ SET @FileListParamSQL += N')' + NCHAR(13) + NCHAR(10);
470489
SET @FileListParamSQL += N'EXEC (''RESTORE FILELISTONLY FROM DISK=''''{Path}'''''')';
471490

472491
SET @sql = REPLACE(@FileListParamSQL, N'{Path}', @BackupPathFull + @LastFullBackup);
473-
492+
474493
IF @Debug = 1
475494
BEGIN
476495
IF @sql IS NULL PRINT '@sql is NULL for INSERT to #FileListParameters: @BackupPathFull + @LastFullBackup';
@@ -521,7 +540,7 @@ IF @MoveFiles = 1
521540
ELSE REPLACE(REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)), @Database, SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) -2)) + ''''
522541
END AS logicalcmds
523542
FROM #FileListParameters)
524-
543+
525544
SELECT @MoveOption = @MoveOption + Files.logicalcmds
526545
FROM Files;
527546

@@ -536,13 +555,13 @@ IF @ContinueLogs = 0
536555
RAISERROR('@ContinueLogs set to 0', 0, 1) WITH NOWAIT;
537556

538557
SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathFull + @LastFullBackup + N''' WITH NORECOVERY, REPLACE' + @MoveOption + NCHAR(13);
539-
558+
540559
IF @Debug = 1
541560
BEGIN
542561
IF @sql IS NULL PRINT '@sql is NULL for RESTORE DATABASE: @BackupPathFull, @LastFullBackup, @MoveOption';
543562
PRINT @sql;
544563
END;
545-
564+
546565
IF @Debug IN (0, 1)
547566
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @Database, @LogToTable = 'Y', @Execute = 'Y';
548567

@@ -581,7 +600,7 @@ ELSE
581600

582601
END;
583602

584-
--Clear out table variables for differential
603+
--Clear out table variables for differential
585604
DELETE FROM @FileList;
586605

587606
END
@@ -591,7 +610,7 @@ IF @BackupPathDiff IS NOT NULL
591610

592611
BEGIN
593612

594-
-- get list of files
613+
-- Get list of files
595614
SET @cmd = N'DIR /b "'+ @BackupPathDiff + N'"';
596615

597616
IF @Debug = 1
@@ -682,7 +701,6 @@ IF @RestoreDiff = 1 AND @BackupDateTime < @LastDiffBackupDateTime
682701
PRINT @sql;
683702
END;
684703

685-
686704
IF @Debug IN (0, 1)
687705
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @Database, @LogToTable = 'Y', @Execute = 'Y';
688706

@@ -710,7 +728,7 @@ IF @RestoreDiff = 1 AND @BackupDateTime < @LastDiffBackupDateTime
710728
WHERE BackupType = 5;
711729
END;
712730

713-
--Clear out table variables for translogs
731+
--Clear out table variables for translogs
714732
DELETE FROM @FileList;
715733

716734
END
@@ -799,7 +817,7 @@ END
799817

800818

801819

802-
--check for log backups
820+
-- Check for log backups
803821
IF(@StopAt IS NULL AND @OnlyLogsAfter IS NULL)
804822
BEGIN
805823
DECLARE BackupFiles CURSOR FOR
@@ -841,6 +859,18 @@ IF (@StopAt IS NOT NULL AND @OnlyLogsAfter IS NULL)
841859
OPEN BackupFiles;
842860
END;
843861

862+
IF (@StandbyMode = 1)
863+
BEGIN
864+
IF (@StandbyUndoPath IS NULL)
865+
RAISERROR('The file path of the undo file for standby mode was not specified. Logs will not be restored in standby mode.', 0, 1) WITH NOWAIT;
866+
ELSE
867+
SET @LogRecoveryOption = N'STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''';
868+
END;
869+
870+
IF (@LogRecoveryOption = N'')
871+
BEGIN
872+
SET @LogRecoveryOption = N'NORECOVERY';
873+
END;
844874

845875
-- Loop through all the files for the database
846876
FETCH NEXT FROM BackupFiles INTO @BackupFile;
@@ -887,7 +917,7 @@ FETCH NEXT FROM BackupFiles INTO @BackupFile;
887917

888918
RAISERROR('@i set to 2, restoring logs', 0, 1) WITH NOWAIT;
889919

890-
SET @sql = N'RESTORE LOG ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathLog + @BackupFile + N''' WITH NORECOVERY' + NCHAR(13);
920+
SET @sql = N'RESTORE LOG ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathLog + @BackupFile + N''' WITH '+ @LogRecoveryOption + NCHAR(13);
891921

892922
IF @Debug = 1
893923
BEGIN
@@ -914,7 +944,7 @@ DEALLOCATE BackupFiles;
914944

915945
END
916946

917-
-- put database in a useable state
947+
-- Put database in a useable state
918948
IF @RunRecovery = 1
919949
BEGIN
920950
SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' WITH RECOVERY' + NCHAR(13);
@@ -929,7 +959,7 @@ IF @RunRecovery = 1
929959
EXECUTE sp_executesql @sql;
930960
END;
931961

932-
-- ensure simple recovery model
962+
-- Ensure simple recovery model
933963
IF @ForceSimpleRecovery = 1
934964
BEGIN
935965
SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + N' SET RECOVERY SIMPLE' + NCHAR(13);
@@ -942,9 +972,9 @@ IF @ForceSimpleRecovery = 1
942972

943973
IF @Debug IN (0, 1)
944974
EXECUTE sp_executesql @sql;
945-
END;
975+
END;
946976

947-
--Run checkdb against this database
977+
--Run checkdb against this database
948978
IF @RunCheckDB = 1
949979
BEGIN
950980
SET @sql = N'EXECUTE [dbo].[DatabaseIntegrityCheck] @Databases = ' + @RestoreDatabaseName + N', @LogToTable = ''Y''' + NCHAR(13);
@@ -959,7 +989,7 @@ IF @RunCheckDB = 1
959989
EXECUTE sys.sp_executesql @sql;
960990
END;
961991

962-
--If test restore then blow the database away (be careful)
992+
--If test restore then blow the database away (be careful)
963993
IF @TestRestore = 1
964994
BEGIN
965995
SET @sql = N'DROP DATABASE ' + @RestoreDatabaseName + NCHAR(13);
@@ -975,4 +1005,5 @@ IF @TestRestore = 1
9751005

9761006
END;
9771007

978-
GO
1008+
GO
1009+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /