Skip to content

Commit 38665ce

Browse files
committed
Adds some elements of cleanup
Might as well get this in now.
1 parent e9fb77b commit 38665ce

File tree

1 file changed

+200
-2
lines changed

1 file changed

+200
-2
lines changed

sp_AllNightLog_Setup.sql

Lines changed: 200 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ALTER PROCEDURE dbo.sp_AllNightLog_Setup
2323
@UpdateSetup BIT = 0,
2424
@EnableBackupJobs INT = NULL,
2525
@EnableRestoreJobs INT = NULL,
26+
@Cleanup BIT = 0,
2627
@Debug BIT = 0,
2728
@Help BIT = 0,
2829
@VersionDate DATETIME = NULL OUTPUT
@@ -171,6 +172,20 @@ DECLARE @job_name_restores NVARCHAR(MAX) = N'''sp_AllNightLog_Restore_Job_'''; -
171172
DECLARE @job_description_restores NVARCHAR(MAX) = N'''This is a worker for the purposes of restoring log backups from msdb.dbo.restore_worker queue table.'''; --Job description
172173
DECLARE @job_command_restores NVARCHAR(MAX) = N'''EXEC sp_AllNightLog @Restore = 1'''; --Command the Agent job will run
173174

175+
/*Cleanup variables*/
176+
--Agent stuff
177+
DECLARE @ten_second_schedule_check INT = 0 --Looks for ten_second schedule. I know it looks weird to have this as an INT, but you can create multiple schedules with the same name.
178+
DECLARE @backup_jobs_count INT = 0 --Gets a count of backup jobs
179+
DECLARE @restore_jobs_count INT = 0 --Gets a count of restore jobs
180+
DECLARE @poll_jobs_count INT = 0 --Gets a count of polling jobs
181+
--Table and database stuff
182+
DECLARE @drop_db_sql NVARCHAR(MAX) = N'' --Dynamic SQL used to look for our centralized database
183+
DECLARE @drop_db_exists NVARCHAR(100) = NULL -- Holds the status of our database (will be ONLINE if exists and accessible)
184+
DECLARE @drop_db_table_count_sql NVARCHAR(MAX) = N'' --Dynamic SQL to get a count of user tables in centralized database
185+
DECLARE @drop_db_table_count INT = 0 --Count of user tables in centralized database. Used to determine if we drop the database or just our two tables
186+
DECLARE @drop_restore_worker BIT = 0 --Flips to 1 if we need to drop our restore worker table
187+
DECLARE @drop_restore_config BIT = 0 --Flips to 1 if we need to drop our restore config table
188+
174189

175190
/*
176191
@@ -180,17 +195,41 @@ Sanity check some variables
180195

181196

182197

183-
IF ((@RunSetup = 0 OR @RunSetup IS NULL) AND (@UpdateSetup = 0 OR @UpdateSetup IS NULL))
198+
IF ((@RunSetup = 0 OR @RunSetup IS NULL) AND (@UpdateSetup = 0 OR @UpdateSetup IS NULL) AND (@Cleanup = 0 OR @Cleanup IS NULL))
199+
200+
BEGIN
201+
202+
RAISERROR('You have to either run setup or update setup or cleanup setup. You can''t not do neither nor, if you follow. Or not.', 0, 1) WITH NOWAIT;
203+
204+
RETURN;
205+
206+
END;
184207

208+
IF (
209+
(@Cleanup & @RunSetup) = 1
210+
OR
211+
(@Cleanup & @UpdateSetup) = 1
212+
OR
213+
(@RunSetup & @UpdateSetup) = 1
214+
)
185215
BEGIN
186216

187-
RAISERROR('You have to either run setup or update setup. You can''t not do neither nor, if you follow. Or not.', 0, 1) WITH NOWAIT;
217+
RAISERROR('You can''t run more than 1 operation at once. Please choose between setup, update, and cleanup.', 0, 1) WITH NOWAIT;
188218

189219
RETURN;
190220

191221
END;
192222

193223

224+
IF (@RunSetup = 1 AND (@BackupPath IS NULL OR @RestorePath IS NULL OR @RPOSeconds IS NULL OR @RTOSeconds IS NULL) )
225+
BEGIN
226+
227+
RAISERROR('When running setup, you need to specify a backup path, a restore path, and values for RPO and RTO.', 0, 1) WITH NOWAIT;
228+
229+
RETURN;
230+
231+
END;
232+
194233
/*
195234
196235
Should be a positive number
@@ -321,6 +360,10 @@ IF @UpdateSetup = 1
321360
IF @UpdateSetup = 1
322361
GOTO UpdateConfigs;
323362

363+
364+
IF @Cleanup = 1
365+
GOTO Cleanup;
366+
324367
IF @RunSetup = 1
325368
BEGIN
326369
BEGIN TRY
@@ -1266,6 +1309,161 @@ IF @UpdateSetup = 1
12661309
END; --End updates to configuration table
12671310

12681311

1312+
/*
1313+
1314+
Cleanup section to cleanup, you filthy animal
1315+
1316+
*/
1317+
1318+
1319+
Cleanup:
1320+
1321+
IF @Cleanup = 1
1322+
1323+
BEGIN
1324+
1325+
RAISERROR('Starting cleanup', 0, 1) WITH NOWAIT;
1326+
RAISERROR('Assessing the damage', 0, 1) WITH NOWAIT;
1327+
1328+
1329+
/*
1330+
1331+
Start by looking for backup jobs
1332+
1333+
*/
1334+
1335+
RAISERROR('Checking for backup jobs', 0, 1) WITH NOWAIT;
1336+
1337+
SELECT @backup_jobs_count = COUNT(*)
1338+
FROM msdb.dbo.sysjobs
1339+
WHERE name LIKE 'sp[_]AllNightLog[_]Backup[_]%';
1340+
1341+
RAISERROR('Found [%d] backup jobs', 0, 1, @backup_jobs_count) WITH NOWAIT;
1342+
1343+
/*
1344+
1345+
Look for restore jobs next
1346+
1347+
*/
1348+
1349+
RAISERROR('Checking for restore jobs', 0, 1) WITH NOWAIT;
1350+
1351+
SELECT @restore_jobs_count = COUNT(*)
1352+
FROM msdb.dbo.sysjobs
1353+
WHERE name LIKE 'sp[_]AllNightLog[_]Restore[_]%';
1354+
1355+
RAISERROR('Found [%d] restore jobs', 0, 1, @restore_jobs_count) WITH NOWAIT;
1356+
1357+
/*
1358+
1359+
Now look for polling jobs
1360+
1361+
*/
1362+
1363+
IF @Debug = 1 RAISERROR('Checking for polling jobs', 0, 1) WITH NOWAIT;
1364+
1365+
SELECT @restore_jobs_count = COUNT(*)
1366+
FROM msdb.dbo.sysjobs
1367+
WHERE name LIKE 'sp[_]AllNightLog[_]Poll%';
1368+
1369+
IF @Debug = 1 RAISERROR('Found [%d] polling jobs', 0, 1, @restore_jobs_count) WITH NOWAIT;
1370+
1371+
1372+
/*
1373+
1374+
Now look for ten_second schedule
1375+
1376+
*/
1377+
1378+
IF @Debug = 1 RAISERROR('Checking for ten_second schedule', 0, 1) WITH NOWAIT;
1379+
1380+
SELECT @ten_second_schedule_check = COUNT(*)
1381+
FROM msdb.dbo.sysschedules AS s
1382+
WHERE s.name = 'ten_seconds'
1383+
1384+
IF @Debug = 1 RAISERROR('Found [%d] schedules named ten_seconds ', 0, 1, @ten_second_schedule_check) WITH NOWAIT;
1385+
1386+
/*
1387+
1388+
Checking for centralized database
1389+
1390+
*/
1391+
1392+
IF @Debug = 1 RAISERROR('Checking for [%s]', 0, 1, @database_name) WITH NOWAIT;
1393+
1394+
SET @drop_db_sql += N'SELECT @sp_drop_db_exists = CONVERT(NVARCHAR(100), DATABASEPROPERTYEX(' + QUOTENAME(@database_name, '''') + ', ''Status'') )'
1395+
1396+
IF @Debug = 1 PRINT @drop_db_sql
1397+
1398+
EXEC sys.sp_executesql @drop_db_sql, N'@sp_drop_db_exists NVARCHAR(100) OUTPUT', @sp_drop_db_exists = @drop_db_exists OUTPUT
1399+
1400+
IF @Debug = 1 RAISERROR('Database [%s] has a status of [%s]', 0, 1, @database_name, @drop_db_exists) WITH NOWAIT;
1401+
1402+
IF @drop_db_exists = N'ONLINE' AND @Debug = 1
1403+
BEGIN
1404+
RAISERROR('Found [%s]', 0, 1, @database_name) WITH NOWAIT;
1405+
END
1406+
ELSE
1407+
BEGIN
1408+
RAISERROR('Did not find [%s]', 0, 1, @database_name) WITH NOWAIT;
1409+
END
1410+
1411+
/*
1412+
1413+
Checking for other tables in centralized database
1414+
1415+
*/
1416+
IF @Debug = 1 RAISERROR('Checking for other user tables in [%s]', 0, 1, @database_name) WITH NOWAIT;
1417+
1418+
SET @drop_db_table_count_sql = N'
1419+
SELECT @sp_drop_db_table_count = COUNT(*)
1420+
FROM msdbCentral.sys.tables AS t
1421+
WHERE t.name NOT LIKE ''backup_configuration%''
1422+
AND t.name NOT LIKE ''backup_worker%'';
1423+
' --Using LIKE here just in case someone makes a 'backup_worker_bak' table or something before making changes
1424+
1425+
IF @Debug = 1 PRINT @drop_db_table_count_sql
1426+
1427+
EXEC sys.sp_executesql @drop_db_table_count_sql, N'@sp_drop_db_table_count INT OUTPUT', @sp_drop_db_table_count = @drop_db_table_count OUTPUT
1428+
1429+
IF @Debug = 1 AND @drop_db_table_count > 0 RAISERROR('Found [%d] other tables in [%s], will only drop our tables', 0, 1, @drop_db_table_count, @database_name)
1430+
1431+
IF @Debug = 1 AND @drop_db_table_count = 0 RAISERROR('Found [%d] other tables in [%s], will drop database', 0, 1, @drop_db_table_count, @database_name)
1432+
1433+
1434+
/*
1435+
1436+
Checking restore tables
1437+
1438+
*/
1439+
1440+
IF @Debug = 1 RAISERROR('Checking for restore tables in msdb', 0, 1) WITH NOWAIT;
1441+
1442+
IF OBJECT_ID('msdb.dbo.restore_configuration', 'U') IS NOT NULL
1443+
BEGIN
1444+
SET @drop_restore_config = 1
1445+
IF @Debug = 1 RAISERROR('Found restore_configuration in msdb', 0, 1) WITH NOWAIT;
1446+
END
1447+
1448+
IF OBJECT_ID('msdb.dbo.restore_worker', 'U') IS NOT NULL
1449+
BEGIN
1450+
SET @drop_restore_worker = 1
1451+
IF @Debug = 1 RAISERROR('Found restore_worker in msdb', 0, 1) WITH NOWAIT;
1452+
END
1453+
1454+
1455+
1456+
1457+
1458+
SELECT 'poop'
1459+
1460+
1461+
1462+
1463+
1464+
END
1465+
1466+
12691467
END; -- Final END for stored proc
12701468
GO
12711469

0 commit comments

Comments
 (0)