Skip to content

Commit 6084ecb

Browse files
Get-DbaLastBackup - Add -ExcludeReplica switch for AlwaysOn preferred backup replica filtering (#10240)
1 parent 8099963 commit 6084ecb

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

public/Get-DbaLastBackup.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function Get-DbaLastBackup {
2525
Excludes specific databases from the backup history check. Commonly used to skip system databases or test databases.
2626
Use this when you want to check most databases but exclude certain ones like tempdb, development databases, or databases with known backup exemptions.
2727
28+
.PARAMETER ExcludeReplica
29+
When this switch is enabled, databases in an AlwaysOn Availability Group where the current SQL Server instance
30+
is not the preferred backup replica are excluded from the results.
31+
This is useful when running Get-DbaLastBackup against multiple servers in an availability group to avoid
32+
false positives where secondary replicas appear to have missing backups because backups are taken on the preferred replica only.
33+
2834
.PARAMETER EnableException
2935
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
3036
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
@@ -94,6 +100,13 @@ function Get-DbaLastBackup {
94100
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Where-Object { $_.LastFullBackupIsCopyOnly -eq $true }
95101
96102
Filters for the databases that had a copy_only full backup done as the last backup.
103+
104+
.EXAMPLE
105+
PS C:\> Get-DbaLastBackup -SqlInstance sql2019, sql2019b -ExcludeReplica
106+
107+
Returns last backup information for all databases on both instances, excluding availability group databases
108+
where the current instance is not the preferred backup replica. This prevents false alerts when secondary
109+
replicas appear to have no recent backups because backups are only performed on the preferred replica.
97110
#>
98111
[CmdletBinding()]
99112
param (
@@ -103,6 +116,7 @@ function Get-DbaLastBackup {
103116
$SqlCredential,
104117
[object[]]$Database,
105118
[object[]]$ExcludeDatabase,
119+
[switch]$ExcludeReplica,
106120
[switch]$EnableException
107121
)
108122
begin {
@@ -131,6 +145,16 @@ function Get-DbaLastBackup {
131145
if ($ExcludeDatabase) {
132146
$dbs = $dbs | Where-Object Name -NotIn $ExcludeDatabase
133147
}
148+
149+
if ($ExcludeReplica -and $server.IsHadrEnabled) {
150+
Write-Message -Level Verbose -Message "Excluding non-preferred backup replicas for $instance"
151+
$notPreferredQuery = "SELECT DB_NAME(database_id) AS DatabaseName FROM sys.databases WHERE sys.fn_hadr_backup_is_preferred_replica(DB_NAME(database_id)) = 0"
152+
$notPreferredDbs = ($server.Query($notPreferredQuery)).DatabaseName
153+
if ($notPreferredDbs) {
154+
$dbs = $dbs | Where-Object { $_.Name -notin $notPreferredDbs }
155+
}
156+
}
157+
134158
# Get-DbaDbBackupHistory -Last would make the job in one query but SMO's (and this) report the last backup of this type regardless of the chain
135159
$FullHistory = Get-DbaDbBackupHistory -SqlInstance $server -Database $dbs.Name -LastFull -IncludeCopyOnly -Raw
136160
$DiffHistory = Get-DbaDbBackupHistory -SqlInstance $server -Database $dbs.Name -LastDiff -IncludeCopyOnly -Raw

tests/Get-DbaLastBackup.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Describe $CommandName -Tag UnitTests {
1515
"SqlCredential",
1616
"Database",
1717
"ExcludeDatabase",
18+
"ExcludeReplica",
1819
"EnableException"
1920
)
2021
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty

0 commit comments

Comments
 (0)