Home

High-availability group support

Prev Page Next Page
Introduction
Recovery models
Main backup types
Backing up the database files by copying
The transaction log
Transaction log restore sequence
Log sequence numbers
Truncating and shrinking the transaction log
Backing up the tail
Inside the transaction log
So, what's in a backup file?
Test: A full backup does not contain deleted data
Verifying backup files
Verifying backup files on a budget
Cumulative backups
Recovering individual tables
Backup and restore history details
Backup reads and writes
Speeding up backups
Backup speed details
Speeding up restores
Restore state affects speed too
Backup and restore rights
Log shipping
Log shipping in SQL Server 2000
Setting up log shipping using Enterprise Manager
Checking the set up
Failover
Log shipping in SQL Server 2005
Setting up log shipping using Management Studio
Checking the set up
Log shipping status report
Failover
Log shipping in SQL Backup
Using the CopyTool utility
Failover
3rd party backup applications
VDI
VDI versions
VDI errors
SQL Backup - beyond compression
Restoring a chain of transaction log backups
Restoring to the latest possible state
Backing up multiple databases
Backup retention
Making a copy of the backup file
Backup file naming conventions
Restoring the latest backup set
Network resilience
Encryption
Integrated database verification
Database file relocation
Improved backup retention
RESTORE HELP
High-availability group support
Common SQL Backup issues
Installation checklist
Setting up rights
Configuring service rights
Backup data
Hanging issues
Common backup and restore errors
Error 3201 - when performing a backup to a network share
Full database backup file is larger than database size
Error 3205 - Too many backup devices specified for backup or restore
Error 4305 - an earlier transaction log backup is required
Bringing a database that is in recovery or read-only mode online
Using bulk-logged recovery model but transaction log backup is still large
Error 14274 - unable to delete SQL Server Agent job
Error messages when restoring from different versions of SQL Server.
Pending
vdi error codes
Restore speed details
Help, my transaction log file is huge!
Mirror or log ship



# available only in SQL Backup 8.0 and newer

SQL Backup 8 adds support for high-availability (HA) groups with the introduction of 2 new keywords: CHECK_PREFERRED_AG_REPLICA and SECONDARY_REPLICA_COPY_ONLY.

CHECK_PREFERRED_AG_REPLICA

When you want to back up a database only from a replica that's the preferred instance in SQL Server, you would typically use the sys.fn_hadr_backup_is_preferred_replica function e.g.

IF (NOT sys.fn_hadr_backup_is_preferred_replica(@DBNAME))
BEGIN
     Select ‘This is not the preferred replica, exiting with success’;
     RETURN 0 – This is a normal, expected condition, so the script returns success
END
BACKUP DATABASE @DBNAME TO DISK=<disk>  WITH COPY_ONLY;

In SQL Backup 8, you can use the CHECK_PREFERRED_AG_REPLICA option to perform the same checks e.g.

EXEC master..sqlbackup '-sql " BACKUP DATABASE db_hatest05 TO DISK = [<AUTO>] WITH CHECK_PREFERRED_AG_REPLICA"'

Depending on your backup replica settings,

options

the backup will either run,

backup_do

or not run and raise a message indicating that the current instance is not the preferred backup replica.

backup_skip

This option also works for multi-database backups.  This means you no longer need to create separate backup jobs for regular databases and HA replica databases.  E.g.

EXEC master..sqlbackup '-sql " BACKUP LOGS EXCLUDE [model, tempdb] TO DISK = [<AUTO>] WITH CHECK_PREFERRED_AG_REPLICA"'

In the command above, the transaction log for the regular databases will be backed up normally, and transaction logs for replica databases will only be backed up if the instance is the preferred backup replica.

SECONDARY_REPLICA_COPY_ONLY

For a HA replica database, SQL Server allows you to perform only a copy-only full database backup e.g.

BACKUP DATABASE db1 TO DISK=<disk>  WITH COPY_ONLY;

You can still do the same thing in SQL Backup, but on an instance where you have a mix of regular databases and HA replica databases, it would be tedious to create and maintain multiple jobs for each database as the regular databases would not require the COPY_ONLY option.

In SQL Backup, you can use the SECONDARY_REPLICA_COPY_ONLY to automatically add the COPY_ONLY option if the database that is being backed up is a secondary replica.  This is useful in multi-database backups e.g.

EXEC master..sqlbackup '-sql " BACKUP DATABASES EXCLUDE [model, tempdb] TO DISK = [<AUTO>] WITH SECONDARY_REPLICA_COPY_ONLY"'

Regular databases will be backed up without the COPY_ONLY option, while secondary replicas will have the COPY_ONLY option added automatically.

Putting it all together

In a HA group, to take a full backup of all user databases on each instance, you will need to create a job on each instance and use the following command:

EXEC master..sqlbackup '-sql "BACKUP USER DATABASES TO DISK = [<AUTO>]"'

This will fail for secondary replicas, because they require the COPY_ONLY option.  Thus, you would need to amend the above command to this:

EXEC master..sqlbackup '-sql "BACKUP USER DATABASES TO DISK = [<AUTO>] WITH SECONDARY_REPLICA_COPY_ONLY"'

Now you may want to restrict backups to be taken only on the preferred instances, so you need to add the CHECK_PREFERRED_AG_REPLICA option:

EXEC master..sqlbackup '-sql "BACKUP USER DATABASES TO DISK = [<AUTO>] WITH SECONDARY_REPLICA_COPY_ONLY, CHECK_PREFERRED_AG_REPLICA"'

To back up the transaction log for all databases, your job command would look like the following:

EXEC master..sqlbackup '-sql "BACKUP LOGS [*] TO DISK = [<AUTO>]"'

However, this would cause transaction log backups to be taken from any secondary replica.  To back up the transaction logs only if the database is the preferred replica, add the CHECK_PREFERRED_AG_REPLICA option i.e.

EXEC master..sqlbackup '-sql "BACKUP LOGS [*] TO DISK = [<AUTO>] WITH CHECK_PREFERRED_AG_REPLICA"'




Document history
8/6/2016    Initial release.    
 
Copyright 2008 - 2017 Yohz Ventures Sdn Bhd. All rights reserved.
All product and company names are trademarks or registered trademarks of their respective owners.