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 859db08

Browse files
Add standby mode to sp_DatabaseRestore.sql
1 parent 77b40c3 commit 859db08

File tree

1 file changed

+43
-55
lines changed

1 file changed

+43
-55
lines changed

‎sp_DatabaseRestore.sql‎

Lines changed: 43 additions & 55 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,9 @@ 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,
2021
@StopAt NVARCHAR(14) = NULL,
2122
@OnlyLogsAfter NVARCHAR(14) = NULL,
2223
@Debug INT = 0,
@@ -27,8 +28,8 @@ SET NOCOUNT ON;
2728

2829
/*Versioning details*/
2930
DECLARE @Version NVARCHAR(30);
30-
SET @Version = '5.9.5';
31-
SET @VersionDate = '20171115';
31+
SET @Version = '5.8';
32+
SET @VersionDate = '20171001';
3233

3334

3435
IF @Help = 1
@@ -136,6 +137,16 @@ IF @Help = 1
136137
@TestRestore = 1,
137138
@RunCheckDB = 1,
138139
@Debug = 0;
140+
141+
EXEC dbo.sp_DatabaseRestore
142+
@Database = ''LogShipMe'',
143+
@BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'',
144+
@BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'',
145+
@StandbyMode = 1,
146+
@StandbyUndoPath = ''D:\Data\'',
147+
@ContinueLogs = 1,
148+
@RunRecovery = 0,
149+
@Debug = 0;
139150
140151
--This example will restore the latest differential backup, and stop transaction logs at the specified date time. It will also only print the commands.
141152
EXEC dbo.sp_DatabaseRestore
@@ -192,7 +203,8 @@ DECLARE @cmd NVARCHAR(4000) = N'', --Holds xp_cmdshell command
192203
@FullLastLSN NUMERIC(25, 0), --LSN for full
193204
@DiffLastLSN NUMERIC(25, 0), --LSN for diff
194205
@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
206+
@MoveOption AS NVARCHAR(MAX) = N'', --If you need to move restored files to a different directory
207+
@LogRecoveryOption AS NVARCHAR(MAX) = N'', --Holds the option to cause logs to be restored in standby mode or with no recovery
196208
@DatabaseLastLSN NUMERIC(25, 0), --redo_start_lsn of the current database
197209
@i TINYINT = 1, --Maintains loop to continue logs
198210
@LogFirstLSN NUMERIC(25, 0), --Holds first LSN in log backup headers
@@ -327,29 +339,25 @@ IF (SELECT RIGHT(@BackupPathLog, 1)) <> '\' --Has to end in a '\'
327339
END;
328340

329341
/*Move Data File*/
330-
IF NULLIF(@MoveDataDrive, '') IS NULL
331-
BEGIN
332-
RAISERROR('Getting default data drive for @MoveDataDrive', 0, 1) WITH NOWAIT;
333-
SET @MoveDataDrive = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS nvarchar(260));
334-
END;
335342
IF (SELECT RIGHT(@MoveDataDrive, 1)) <> '\' --Has to end in a '\'
336343
BEGIN
337344
RAISERROR('Fixing @MoveDataDrive to add a "\"', 0, 1) WITH NOWAIT;
338345
SET @MoveDataDrive += N'\';
339346
END;
340347

341348
/*Move Log File*/
342-
IF NULLIF(@MoveLogDrive, '') IS NULL
343-
BEGIN
344-
RAISERROR('Getting default log drive for @@MoveLogDrive', 0, 1) WITH NOWAIT;
345-
SET @MoveLogDrive = CAST(SERVERPROPERTY('InstanceDefaultLogPath') AS nvarchar(260));
346-
END;
347349
IF (SELECT RIGHT(@MoveLogDrive, 1)) <> '\' --Has to end in a '\'
348350
BEGIN
349351
RAISERROR('Fixing @MoveDataDrive to add a "\"', 0, 1) WITH NOWAIT;
350352
SET @MoveLogDrive += N'\';
351353
END;
352354

355+
/*Standby Undo File*/
356+
IF (SELECT RIGHT(@StandbyUndoPath, 1)) <> '\' --Has to end in a '\'
357+
BEGIN
358+
RAISERROR('Fixing @StandbyUndoPath to add a "\"', 0, 1) WITH NOWAIT;
359+
SET @StandbyUndoPath += N'\';
360+
END;
353361

354362

355363
IF @RestoreDatabaseName IS NULL
@@ -424,28 +432,14 @@ EXEC master.sys.xp_cmdshell @cmd;
424432

425433
END
426434

427-
IF (
428-
SELECT COUNT(*)
429-
FROM @FileList AS fl
430-
WHERE fl.BackupFile = 'The user name or password is incorrect.'
431-
) = 1
432-
433-
BEGIN
434-
435-
RAISERROR('Incorrect user name or password for %s', 16, 1, @BackupPathFull) WITH NOWAIT;
436-
437-
END;
438-
439435
/*End folder sanity check*/
440436

441437
-- Find latest full backup
442438
SELECT @LastFullBackup = MAX(BackupFile)
443439
FROM @FileList
444440
WHERE BackupFile LIKE N'%.bak'
445441
AND
446-
BackupFile LIKE N'%' + @Database + N'%'
447-
AND
448-
(@StopAt IS NULL OR REPLACE(LEFT(RIGHT(BackupFile, 19), 15),'_','') <= @StopAt);
442+
BackupFile LIKE N'%' + @Database + N'%';
449443

450444
IF @Debug = 1
451445
BEGIN
@@ -470,7 +464,7 @@ SET @FileListParamSQL += N')' + NCHAR(13) + NCHAR(10);
470464
SET @FileListParamSQL += N'EXEC (''RESTORE FILELISTONLY FROM DISK=''''{Path}'''''')';
471465

472466
SET @sql = REPLACE(@FileListParamSQL, N'{Path}', @BackupPathFull + @LastFullBackup);
473-
467+
474468
IF @Debug = 1
475469
BEGIN
476470
IF @sql IS NULL PRINT '@sql is NULL for INSERT to #FileListParameters: @BackupPathFull + @LastFullBackup';
@@ -521,7 +515,7 @@ IF @MoveFiles = 1
521515
ELSE REPLACE(REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)), @Database, SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) -2)) + ''''
522516
END AS logicalcmds
523517
FROM #FileListParameters)
524-
518+
525519
SELECT @MoveOption = @MoveOption + Files.logicalcmds
526520
FROM Files;
527521

@@ -536,13 +530,13 @@ IF @ContinueLogs = 0
536530
RAISERROR('@ContinueLogs set to 0', 0, 1) WITH NOWAIT;
537531

538532
SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathFull + @LastFullBackup + N''' WITH NORECOVERY, REPLACE' + @MoveOption + NCHAR(13);
539-
533+
540534
IF @Debug = 1
541535
BEGIN
542536
IF @sql IS NULL PRINT '@sql is NULL for RESTORE DATABASE: @BackupPathFull, @LastFullBackup, @MoveOption';
543537
PRINT @sql;
544538
END;
545-
539+
546540
IF @Debug IN (0, 1)
547541
EXECUTE @sql = [dbo].[CommandExecute] @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @Database, @LogToTable = 'Y', @Execute = 'Y';
548542

@@ -663,9 +657,7 @@ SELECT @LastDiffBackup = MAX(BackupFile)
663657
FROM @FileList
664658
WHERE BackupFile LIKE N'%.bak'
665659
AND
666-
BackupFile LIKE N'%' + @Database + '%'
667-
AND
668-
(@StopAt IS NULL OR REPLACE(LEFT(RIGHT(BackupFile, 19), 15),'_','') <= @StopAt);
660+
BackupFile LIKE N'%' + @Database + '%';
669661

670662
--set the @BackupDateTime so that it can be used for comparisons
671663
SET @BackupDateTime = REPLACE(@BackupDateTime, '_', '');
@@ -682,7 +674,6 @@ IF @RestoreDiff = 1 AND @BackupDateTime < @LastDiffBackupDateTime
682674
PRINT @sql;
683675
END;
684676

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

@@ -841,6 +832,18 @@ IF (@StopAt IS NOT NULL AND @OnlyLogsAfter IS NULL)
841832
OPEN BackupFiles;
842833
END;
843834

835+
IF (@StandbyMode = 1)
836+
BEGIN
837+
IF (@StandbyUndoPath IS NULL)
838+
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;
839+
ELSE
840+
SET @LogRecoveryOption = N'STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''';
841+
END;
842+
843+
IF (@LogRecoveryOption = N'')
844+
BEGIN
845+
SET @LogRecoveryOption = N'NORECOVERY';
846+
END;
844847

845848
-- Loop through all the files for the database
846849
FETCH NEXT FROM BackupFiles INTO @BackupFile;
@@ -887,7 +890,7 @@ FETCH NEXT FROM BackupFiles INTO @BackupFile;
887890

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

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

892895
IF @Debug = 1
893896
BEGIN
@@ -928,21 +931,7 @@ IF @RunRecovery = 1
928931
IF @Debug IN (0, 1)
929932
EXECUTE sp_executesql @sql;
930933
END;
931-
932-
-- ensure simple recovery model
933-
IF @ForceSimpleRecovery = 1
934-
BEGIN
935-
SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + N' SET RECOVERY SIMPLE' + NCHAR(13);
936-
937-
IF @Debug = 1
938-
BEGIN
939-
IF @sql IS NULL PRINT '@sql is NULL for SET RECOVERY SIMPLE: @RestoreDatabaseName';
940-
PRINT @sql;
941-
END;
942-
943-
IF @Debug IN (0, 1)
944-
EXECUTE sp_executesql @sql;
945-
END;
934+
946935

947936
--Run checkdb against this database
948937
IF @RunCheckDB = 1
@@ -975,4 +964,3 @@ IF @TestRestore = 1
975964

976965
END;
977966

978-
GO

0 commit comments

Comments
(0)

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