SQL Server – Impact of Backup Preference on Availability Groups

availability-groupssql server

I am using Availability Groups (database level) SQL Server 2017:

I have set my availability group to "prefer secondary". I have allowed read access on replica.
I have created a maintenance plan with full backups on all databases on replica using "copy only" and then also on primary.

But now I'm wondering does AG actually make any decisions? Both Mainenance plans will run and from what I can tell won't change "copy only" to primary's maintenance plan if there's a failover.

Besides just preventing me from running backups if I change the setting….. besides enforcing rules for what [another] dba may or may not be able to do: what is the point of this?

Or, did I setup my backups totally wrong?

Best Answer

No, the availability group does not do anything to control where backups occur. The backup preference settings on an availability group are there so that the backup software can reference it to determine where to run the backup. If the backup software in use doesn't have any code to reference these settings, they will have no impact on that backup software and it will just run the backups on the server it connects to.

Most backup software that supports availability groups will reference the backup preference configuration and take backups accordingly. Maintenance Plan backups will adhere to the backup preference configured on the availability group.

One option I would recommend for the copy-only backup setup--create your SQL Agent job to run a copy-only backup. The first step in the job would be to check if the server is the primary, and if so, end the job. If it is not the primary, then run the copy-only backup.

The first step of the job can use the fn_hadr_is_primary_replica function to determine if the server is the primary of a given database:

sys.fn_hadr_is_primary_replica ( 'dbname' )

See sys.fn_hadr_is_primary_replica (Transact-SQL).