Skip to content

Commit

Permalink
Simplified backup query.
Browse files Browse the repository at this point in the history
Removed the filter to extract backups only from the primary replica.
Removed datediff used to trim milliseconds: the convert operation already takes care of this.
  • Loading branch information
fabiozanella committed Nov 6, 2023
1 parent af34bca commit 0948991
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions agents/windows/plugins/mssql.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -709,27 +709,11 @@ For Each instance_id In instances.Keys: Do ' Continue trick
' which have at least one backup
Dim lastBackupDate, backup_type, is_primary_replica, replica_id, backup_machine_name, backup_database, found_db_backups
addOutput(sections("backup"))
sqlString = "DECLARE @HADRStatus sql_variant; DECLARE @SQLCommand nvarchar(max); " & _
"SET @HADRStatus = (SELECT SERVERPROPERTY ('IsHadrEnabled')); " & _
"IF (@HADRStatus IS NULL or @HADRStatus <> 1) " & _
"BEGIN " & _
"SET @SQLCommand = 'SELECT CONVERT(VARCHAR, DATEADD(s, DATEDIFF(s, ''19700101'', MAX(backup_finish_date)), ''19700101''), 120) AS last_backup_date, " & _
"type, machine_name, ''True'' as is_primary_replica, ''1'' as is_local, '''' as replica_id,database_name FROM msdb.dbo.backupset " & _
"WHERE UPPER(machine_name) = UPPER(CAST(SERVERPROPERTY(''Machinename'') AS VARCHAR)) " & _
"GROUP BY type, machine_name,database_name ' " & _
"END " & _
"ELSE " & _
"BEGIN " & _
"SET @SQLCommand = 'SELECT CONVERT(VARCHAR, DATEADD(s, DATEDIFF(s, ''19700101'', MAX(b.backup_finish_date)), ''19700101''), 120) AS last_backup_date, " & _
"b.type, b.machine_name, isnull(rep.is_primary_replica,0) as is_primary_replica, rep.is_local, isnull(convert(varchar(40), rep.replica_id), '''') AS replica_id,database_name " & _
"FROM msdb.dbo.backupset b " & _
"LEFT OUTER JOIN sys.databases db ON b.database_name = db.name " & _
"LEFT OUTER JOIN sys.dm_hadr_database_replica_states rep ON db.database_id = rep.database_id " & _
"WHERE (rep.is_local is null or rep.is_local = 1) " & _
"AND (rep.is_primary_replica is null or rep.is_primary_replica = ''True'') and UPPER(machine_name) = UPPER(CAST(SERVERPROPERTY(''Machinename'') AS VARCHAR)) " & _
"GROUP BY type, rep.replica_id, rep.is_primary_replica, rep.is_local, b.database_name, b.machine_name, rep.synchronization_state, rep.synchronization_health' " & _
"END " & _
"EXEC (@SQLCommand)"
sqlString = "SELECT CONVERT(VARCHAR, MAX(backup_finish_date), 120) AS last_backup_date, type, database_name " & _
"FROM msdb.dbo.backupset " & _
"WHERE UPPER(machine_name) = UPPER(CAST(SERVERPROPERTY('Machinename') AS VARCHAR)) " & _
"GROUP BY type, database_name "

Set databaseResponse = databaseSession.queryDatabase("master", sqlString)

Set found_db_backups = CreateObject("Scripting.Dictionary")
Expand All @@ -750,11 +734,7 @@ For Each instance_id In instances.Keys: Do ' Continue trick
backup_type = "-"
End If

replica_id = Trim(record("replica_id"))
is_primary_replica = Trim(record("is_primary_replica"))
backup_machine_name = Trim(record("machine_name"))

If lastBackupDate <> "" and (replica_id = "" or is_primary_replica = "True") Then
If lastBackupDate <> "" Then
addOutput("MSSQL_" & instance_id & "|" & backup_database & _
"|" & Replace(lastBackupDate, " ", "|") & "|" & backup_type)
End If
Expand Down

0 comments on commit 0948991

Please sign in to comment.