· Uwe Janke
Commands / Test-sqmBackupStatus

Test-sqmBackupStatus

BackupsqmSQLTool v1.9.172+ · New🔓 read on msdb.dbo.backupset, VIEW SERVER STATE for the AG role
The backup control. One status per database, measured against thresholds: the newest full or diff (-MaxFullAgeHours, default 26), the newest full alone (-MaxFullOnlyAgeDays, default 8) and the newest log backup for FULL and BULK_LOGGED databases (-MaxLogAgeMinutes, default 60).

Exclusions: databases marked IsActive = 0 in master.dbo.sqm_BackupExclude are reported as Excluded with reason, who and since when, not as an alarm. It is the same condition the backup jobs of New-sqmOlaUsrDbBackupJob and New-sqmBackupMaintenanceJob use with -UseExcludeTable, so the check and the job agree. The table itself is listed by Get-sqmBackupExcludeReport.

New databases: a database created after the last backup run (the newest regular full of any user database) is flagged and listed in the report header. While it is younger than -MaxFullAgeHours it is a Warning, the next run should pick it up; after that it is Critical.

Only backups taken after the database's create_date count, so a database that was recreated or restored under the same name does not inherit the history of its predecessor. COPY_ONLY fulls do not count. AG databases on a secondary are checked against the primary's msdb, because the backup jobs only back them up there. All ages use the server clock.

Statuses: OK, Warning, Critical, Excluded, Skipped (offline, restoring, AG primary not reachable). The same check runs as section Backups in Invoke-sqmHealthReport.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestring[]Optional$env:COMPUTERNAMETarget instance(s), pipeline-capable.
-SqlCredentialPSCredentialOptional, Also used for the connection to an AG primary.
-MaxFullAgeHoursintOptional26Maximum age of the newest full or diff.
-MaxFullOnlyAgeDaysintOptional8Maximum age of the newest full alone.
-MaxLogAgeMinutesintOptional60Maximum log backup age. 0 = not checked.
-ExcludeDatabasestring[]Optional, Databases not to check (wildcards). Does not shift the last backup run.
-IgnoreExcludeTableswitchSwitch$falseEvaluate databases excluded in sqm_BackupExclude like all others.
-IncludeSystemDatabasesswitchSwitch$falseAlso check master, model, msdb.
-OutputPathstringOptional<config>\BackupStatusDirectory for TXT, CSV and HTML.
-NoOpenswitchSwitch$falseDo not open the report after creation.
-ContinueOnErrorswitchSwitch$falseContinue with the next instance on error.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately.

Execution Flow

START One query: databases, AG role, last Full/Diff/Log since create_date msdb.backupset read once, server clock via GETDATE() Read master.dbo.sqm_BackupExclude IsActive = 0 AND IsOrphaned = 0 = excluded (skipped with -IgnoreExcludeTable) AG database, local replica not primary? read its history from the current primary (unreachable: Skipped) Last backup run = newest full of any non-excluded user database created later and no full yet: flag NewSinceLastBackup Status per database against the thresholds OK, Warning, Critical, Excluded, Skipped, with notes TXT + CSV + HTML, result object DONE

Return Value

PSCustomObject per instance: SqlInstance, Status (Critical, Warning or OK), LastBackupRun, ExcludeTablePresent, OkCount, WarningCount, CriticalCount, ExcludedCount, SkippedCount, NewSinceLastBackup (database names), DetailRows (Database, Status, RecoveryModel, CreateDate, NewSinceLastBackup, LastFullBackup, LastDiffBackup, LastLogBackup, LastCopyOnlyFull, FullAgeHours, LogAgeMinutes, ExcludedFromBackup, ExcludeReason, AvailabilityGroup, ReplicaRole, HistorySource, Notes), TxtFile, CsvFile, HtmlFile.

Examples

Daily full, log every 15 minutes (defaults)
Test-sqmBackupStatus -SqlInstance "SQL01"
Weekly full, daily diff, log every 15 minutes
Test-sqmBackupStatus -SqlInstance "SQL01" -MaxFullOnlyAgeDays 8 -MaxFullAgeHours 26 -MaxLogAgeMinutes 30
Only the problem cases
(Test-sqmBackupStatus -SqlInstance "SQL01" -NoOpen).DetailRows | Where-Object Status -in 'Warning','Critical'
Which databases were created after the last backup run?
(Test-sqmBackupStatus -SqlInstance "SQL01" -NoOpen).NewSinceLastBackup