Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log backups are not working for availability groups with backup preference set to 'Prefer Secondary' #779

Open
mitesh-p opened this issue Nov 2, 2023 · 5 comments

Comments

@mitesh-p
Copy link

mitesh-p commented Nov 2, 2023

Description of the issue
The log backups are not being taken for the databases which are in availability groups with backup preference set to "Prefer Secondary"

SQL Server version and edition
Microsoft SQL Server 2022 (RTM-CU8) (KB5029666) - 16.0.4075.1 (X64) Aug 23 2023 14:04:50 Copyright (C) 2022 Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows Server 2022 Standard 10.0 (Build 20348: ) (Hypervisor)

Version of the script
2022-12-03 17:23:44

What command are you executing?
EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = NULL, @BackupType = 'LOG', @Verify = 'Y', @CleanupTime = 72, @CheckSum = 'Y', @LogToTable = 'Y'

What output are you getting?
The log backups are not being taken at all. The backup script is executing on primary. The full and diff backups are running on the primary but those backups are working only 'LOG' backups are not working. The other databases which are not in the availability group are working fine.
I suspect something wrong in the following code block (on line 2809)

IF @CurrentDatabaseState = 'ONLINE'
AND NOT (@CurrentUserAccess = 'SINGLE_USER' AND @CurrentIsDatabaseAccessible = 0)
AND NOT (@CurrentInStandby = 1)
AND NOT (@CurrentBackupType = 'LOG' AND @CurrentRecoveryModel = 'SIMPLE')
AND NOT (@CurrentBackupType = 'LOG' AND @CurrentRecoveryModel IN('FULL','BULK_LOGGED') AND @CurrentLogLSN IS NULL)
AND NOT (@CurrentBackupType = 'DIFF' AND @CurrentDifferentialBaseLSN IS NULL)
AND NOT (@CurrentBackupType IN('DIFF','LOG') AND @CurrentDatabaseName = 'master')
AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'FULL' AND @CopyOnly = 'N' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'FULL' AND @CopyOnly = 'Y' AND (@CurrentIsPreferredBackupReplica <> 1 OR @CurrentIsPreferredBackupReplica IS NULL) AND @OverrideBackupPreference = 'N')
AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'DIFF' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'LOG' AND @CopyOnly = 'N' AND (@CurrentIsPreferredBackupReplica <> 1 OR @CurrentIsPreferredBackupReplica IS NULL) AND @OverrideBackupPreference = 'N')
AND NOT (@CurrentAvailabilityGroup IS NOT NULL AND @CurrentBackupType = 'LOG' AND @CopyOnly = 'Y' AND (@CurrentAvailabilityGroupRole <> 'PRIMARY' OR @CurrentAvailabilityGroupRole IS NULL))
AND NOT ((@CurrentLogShippingRole = 'PRIMARY' AND @CurrentLogShippingRole IS NOT NULL) AND @CurrentBackupType = 'LOG' AND @ExcludeLogShippedFromLogBackup = 'Y')
AND NOT (@CurrentIsReadOnly = 1 AND @Updateability = 'READ_WRITE')
AND NOT (@CurrentIsReadOnly = 0 AND @Updateability = 'READ_ONLY')
AND NOT (@CurrentBackupType = 'LOG' AND @LogSizeSinceLastLogBackup IS NOT NULL AND @TimeSinceLastLogBackup IS NOT NULL AND NOT(@CurrentLogSizeSinceLastLogBackup >= @LogSizeSinceLastLogBackup OR @CurrentLogSizeSinceLastLogBackup IS NULL OR DATEDIFF(SECOND,@CurrentLastLogBackup,SYSDATETIME()) >= @TimeSinceLastLogBackup OR @CurrentLastLogBackup IS NULL))
AND NOT (@CurrentBackupType = 'LOG' AND @Updateability = 'READ_ONLY' AND @BackupSoftware = 'DATA_DOMAIN_BOOST')
@griffitmatt
Copy link

" The full and diff backups are running on the primary but those backups are working only 'LOG' backups are not working."

Hi Mitesh checking are you running the backup on the primary or secondary replica?

@mitesh-p
Copy link
Author

mitesh-p commented Nov 26, 2023 via email

@griffitmatt
Copy link

refer to https://ola.hallengren.com/frequently-asked-questions.html

How should I configure DatabaseBackup to back up an Availability Group?
First you need to decide where you want the backups to be performed; on the primary replica or on the secondary replica. SQL Server has an option called backup preference that you can set on the availability group. By default it is set to Prefer Secondary, but you can change it to Primary if you prefer that.

Not all backup types are supported on the secondary replica. Here are the backup types that are supported:

Copy-only full backups
Log backups (non copy-only)
DatabaseBackup will perform these types of backups on the preferred backup replica. It is using the function sys.fn_hadr_backup_is_preferred_replica to determine if the current replica is the preferred replica.

Full backups (non copy-only) and differential backups are only supported on the primary replica. DatabaseBackup will always perform these types of backups on the primary replica.

Here are some different backup strategies that you can use:
Perform all the backups on the primary replica. With this strategy it works just like with any database. You can perform full (non copy-only), differential, and log backups. All you need to do is to set the backup preference to Primary for the availability group.

Perform all the backups on the secondary replica. With this strategy you can only perform copy-only full, and log backups (non copy-only). You need to check that the backup preference is set to Prefer Secondary for the availability group. You also need to add @copyonly = 'Y' to the full backup jobs, and disable the differential backup jobs.

Perform full (non copy-only) and differential backups on the primary replica, and log backups on the secondary replica. You need to check that the backup preference is set to Prefer Secondary for the availability group. You don't need to add any additional parameters to the full, differential, or log backup jobs.
The jobs should be configured identically and be enabled and scheduled on all the replicas.

@Boran
Copy link

Boran commented Apr 29, 2024

I have a similar experience.
using SQL 2019, standard edition, which only allows backups to be done on the primary.

With standard edition:
ALTER AVAILABILITY GROUP FooAG SET (AUTOMATED_BACKUP_PREFERENCE = PRIMARY)
"The specified command is invalid because the Always On Availability Groups backup preference feature is not supported by this edition of SQL Server."

  • full and different backups run fine (on the primary)
  • LOG: AGs are ignored, except the first (which contains the listener). There are three AGs.

Step: DatabaseBackup - USER_DATABASES - LOG
EXECUTE [dbo].[DatabaseBackup]
@databases = 'USER_DATABASES',
@Directory = NULL,
@BackupType = 'LOG',
@verify = 'Y',
@Cleanuptime = NULL,
@Checksum = 'Y',
@LogToTable = 'Y'

Also tried
@AvailabilityGroups='ALL_AVAILABILITY_GROUPS'

In the commandLog table I only ever see just one AG.

The following runs one for each AG on the primary:
select sys.fn_hadr_backup_is_preferred_replica ( 'fooAG' )

@Boran
Copy link

Boran commented Apr 29, 2024

To answer my own question, the reason was not to do with AGs, but the fact that LogShipping was also enabled on some DBs.
By setting @ExcludeLogShippedFromLogBackup='N' , this is now a LOG backup (in addition to log shipping, which is also a redundant backup of transactions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants