From c5e27347b4782c9eea3b96260ffb710536df24b4 Mon Sep 17 00:00:00 2001 From: Greg Dodd Date: Sun, 18 Feb 2024 14:38:57 +1100 Subject: [PATCH 1/2] Update sp_DatabaseRestore.sql Add VerifyRestoreWithStoredProcedure option to sp_databaseRestore. --- sp_DatabaseRestore.sql | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/sp_DatabaseRestore.sql b/sp_DatabaseRestore.sql index ab908d2e..2cecb57c 100755 --- a/sp_DatabaseRestore.sql +++ b/sp_DatabaseRestore.sql @@ -39,7 +39,8 @@ ALTER PROCEDURE [dbo].[sp_DatabaseRestore] @Version VARCHAR(30) = NULL OUTPUT, @VersionDate DATETIME = NULL OUTPUT, @VersionCheckMode BIT = 0, - @FileNamePrefix NVARCHAR(260) = NULL + @FileNamePrefix NVARCHAR(260) = NULL, + @VerifyRestoreWithStoredProcedure NVARCHAR(260) = NULL AS SET NOCOUNT ON; SET STATISTICS XML OFF; @@ -1637,6 +1638,29 @@ END;' -- If test restore then blow the database away (be careful) IF @TestRestore = 1 BEGIN + + IF @VerifyRestoreWithStoredProcedure IS NOT NULL AND LEN(LTRIM(@VerifyRestoreWithStoredProcedure)) > 0 + BEGIN + PRINT 'Attempting to run ' + @VerifyRestoreWithStoredProcedure + SET @sql = N'EXEC ' + @RestoreDatabaseName + '.' + @VerifyRestoreWithStoredProcedure + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Verify Restore with Stored Procedure' + PRINT @sql + END + + IF @RunRecovery = 0 + BEGIN + PRINT 'Unable to run Verify Restore with Stored Procedure as database is not recovered. Run command again with @RunRecovery = 1' + END + ELSE + BEGIN + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXEC sp_executesql @sql + END + END + SET @sql = N'DROP DATABASE ' + @RestoreDatabaseName + NCHAR(13); IF @Debug = 1 OR @Execute = 'N' From 8fca53ae03beb559f0505d79eef29eb365964791 Mon Sep 17 00:00:00 2001 From: Brent Ozar Date: Sun, 18 Feb 2024 04:11:10 -0800 Subject: [PATCH 2/2] Update sp_DatabaseRestore.sql When I look at what the code does, it isn't necessarily testing the database - it's really just running a stored proc after the restore finishes. This functionality could be good for other stuff too, like obscuring data or changing permissions. I switched the parameter name from @VerifyRestoreWithStoredProcedure to @RunStoredProcAfterRestore. --- sp_DatabaseRestore.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sp_DatabaseRestore.sql b/sp_DatabaseRestore.sql index 2cecb57c..c77c4abf 100755 --- a/sp_DatabaseRestore.sql +++ b/sp_DatabaseRestore.sql @@ -40,7 +40,7 @@ ALTER PROCEDURE [dbo].[sp_DatabaseRestore] @VersionDate DATETIME = NULL OUTPUT, @VersionCheckMode BIT = 0, @FileNamePrefix NVARCHAR(260) = NULL, - @VerifyRestoreWithStoredProcedure NVARCHAR(260) = NULL + @RunStoredProcAfterRestore NVARCHAR(260) = NULL AS SET NOCOUNT ON; SET STATISTICS XML OFF; @@ -1639,20 +1639,20 @@ END;' IF @TestRestore = 1 BEGIN - IF @VerifyRestoreWithStoredProcedure IS NOT NULL AND LEN(LTRIM(@VerifyRestoreWithStoredProcedure)) > 0 + IF @RunStoredProcAfterRestore IS NOT NULL AND LEN(LTRIM(@RunStoredProcAfterRestore)) > 0 BEGIN - PRINT 'Attempting to run ' + @VerifyRestoreWithStoredProcedure - SET @sql = N'EXEC ' + @RestoreDatabaseName + '.' + @VerifyRestoreWithStoredProcedure + PRINT 'Attempting to run ' + @RunStoredProcAfterRestore + SET @sql = N'EXEC ' + @RestoreDatabaseName + '.' + @RunStoredProcAfterRestore IF @Debug = 1 OR @Execute = 'N' BEGIN - IF @sql IS NULL PRINT '@sql is NULL for Verify Restore with Stored Procedure' + IF @sql IS NULL PRINT '@sql is NULL when building for @RunStoredProcAfterRestore' PRINT @sql END IF @RunRecovery = 0 BEGIN - PRINT 'Unable to run Verify Restore with Stored Procedure as database is not recovered. Run command again with @RunRecovery = 1' + PRINT 'Unable to run Run Stored Procedure After Restore as database is not recovered. Run command again with @RunRecovery = 1' END ELSE BEGIN