You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
173
172
DECLARE @job_command_restores NVARCHAR(MAX) = N'''EXEC sp_AllNightLog @Restore = 1'''; --Command the Agent job will run
174
173
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
-
189
174
190
175
/*
191
176
@@ -195,41 +180,17 @@ Sanity check some variables
195
180
196
181
197
182
198
-
IF ((@RunSetup =0OR @RunSetup IS NULL) AND (@UpdateSetup =0OR @UpdateSetup IS NULL) AND (@Cleanup =0OR @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;
183
+
IF ((@RunSetup =0OR @RunSetup IS NULL) AND (@UpdateSetup =0OR @UpdateSetup IS NULL))
207
184
208
-
IF (
209
-
(@Cleanup & @RunSetup) =1
210
-
OR
211
-
(@Cleanup & @UpdateSetup) =1
212
-
OR
213
-
(@RunSetup & @UpdateSetup) =1
214
-
)
215
185
BEGIN
216
186
217
-
RAISERROR('You can''t run more than 1 operation at once. Please choose between setup, update, and cleanup.', 0, 1) WITH NOWAIT;
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;
218
188
219
189
RETURN;
220
190
221
191
END;
222
192
223
193
224
-
IF (@RunSetup =1AND (@BackupPath IS NULLOR @RestorePath IS NULLOR @RPOSeconds IS NULLOR @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
-
233
194
/*
234
195
235
196
Should be a positive number
@@ -360,10 +321,6 @@ IF @UpdateSetup = 1
360
321
IF @UpdateSetup =1
361
322
GOTO UpdateConfigs;
362
323
363
-
364
-
IF @Cleanup =1
365
-
GOTO Cleanup;
366
-
367
324
IF @RunSetup =1
368
325
BEGIN
369
326
BEGIN TRY
@@ -1309,161 +1266,6 @@ IF @UpdateSetup = 1
1309
1266
END; --End updates to configuration table
1310
1267
1311
1268
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
-
FROMmsdb.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
-
FROMmsdb.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
-
FROMmsdb.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
-
FROMmsdb.dbo.sysschedules AS s
1382
-
WHEREs.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;
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 =1AND @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 =1AND @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;
0 commit comments