AAG databases not getting backed up
I am using the following code with Ola Hallalengren's script to run copy only backups on a 3 node AAG setup. I have configured to run backups using the "prefer secondary" option . When I triggered the job from the primary replica, only db's outside the AAG are backing up and when I trigger it from one of the secondary only AAG db's are backing up.
The priority for backups has been given like so
Primary - 50%
Secondary - 50%
Secondary1 - 40%
What am I missing?Please suggest. Thanks!
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'\SQL02Backups', @CopyOnly = 'Y', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = NULL, @CheckSum = 'Y', @LogToTable = 'Y'" -b
sql-server backup availability-groups
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I am using the following code with Ola Hallalengren's script to run copy only backups on a 3 node AAG setup. I have configured to run backups using the "prefer secondary" option . When I triggered the job from the primary replica, only db's outside the AAG are backing up and when I trigger it from one of the secondary only AAG db's are backing up.
The priority for backups has been given like so
Primary - 50%
Secondary - 50%
Secondary1 - 40%
What am I missing?Please suggest. Thanks!
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'\SQL02Backups', @CopyOnly = 'Y', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = NULL, @CheckSum = 'Y', @LogToTable = 'Y'" -b
sql-server backup availability-groups
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08
add a comment |
I am using the following code with Ola Hallalengren's script to run copy only backups on a 3 node AAG setup. I have configured to run backups using the "prefer secondary" option . When I triggered the job from the primary replica, only db's outside the AAG are backing up and when I trigger it from one of the secondary only AAG db's are backing up.
The priority for backups has been given like so
Primary - 50%
Secondary - 50%
Secondary1 - 40%
What am I missing?Please suggest. Thanks!
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'\SQL02Backups', @CopyOnly = 'Y', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = NULL, @CheckSum = 'Y', @LogToTable = 'Y'" -b
sql-server backup availability-groups
I am using the following code with Ola Hallalengren's script to run copy only backups on a 3 node AAG setup. I have configured to run backups using the "prefer secondary" option . When I triggered the job from the primary replica, only db's outside the AAG are backing up and when I trigger it from one of the secondary only AAG db's are backing up.
The priority for backups has been given like so
Primary - 50%
Secondary - 50%
Secondary1 - 40%
What am I missing?Please suggest. Thanks!
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'\SQL02Backups', @CopyOnly = 'Y', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = NULL, @CheckSum = 'Y', @LogToTable = 'Y'" -b
sql-server backup availability-groups
sql-server backup availability-groups
edited Oct 18 '16 at 3:07
Shawn Melton
14.3k43780
14.3k43780
asked Oct 17 '16 at 23:18
user2923332user2923332
361112
361112
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08
add a comment |
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08
add a comment |
1 Answer
1
active
oldest
votes
I have configured to run backups using the "prefer secondary" option.
Prefer Secondary - Will backup on secondary with highest priority. If no secondary's are available, it will backup on primary.
When I triggered the job from the primary replica, only db's outside
the AAG are backing up and when I trigger it from one of the secondary
only AAG db's are backing up.
First of all i would like to say that i am try to explain in concise way but due to function logic , elaborate a bit more.
Once you have your backup preferences and priority established, then it is time to set up a SQL Server Agent Job to perform the backups. Thankfully Microsoft has provided us with a system function called sys.fn_hadr_backup_is_preferred_replica
. This function will return 0
if the current instance is not the preferred backup location or 1
if it is the preferred replica for backups according to the preference and priority settings.
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;
As per MSDB BOL Here To take the automated backup preference into account for a given availability group, on each server instance that hosts an availability replica whose backup priority is greater than zero (>0), you need to script backup jobs for the databases in the availability group. To determine whether the current replica is the preferred backup replica, use the sys.fn_hadr_backup_is_preferred_replica
function in your backup script. If the availability replica that is hosted by the current server instance is the preferred replica for backups, this function returns 1
. If not, the function returns 0
. By running a simple script on each availability replica that queries this function, you can determine which replica should run a given backup job.
Scripting a backup job with this logic enables you to schedule the job to run on every availability replica on the same schedule. Each of these jobs looks at the same data to determine which job should run, so only one of the scheduled job actually proceeds to the backup stage.
Note:-There is no enforcement of AutomatedBackupPreference. The interpretation of this preference depends on the logic, if any, that you script into backup jobs for the databases in a given availability group. The automated backup preference setting has no impact on ad-hoc backups.
As,i would like to say that as when you are configuring auto backup of
AAG through maintenance Plan in 'Primary Replica' & when your Backup
preferrence will be 'Preferred Secondary Replica' in that case also
the Non AAG databases will be backup in primary replica backup
location, after the execution of Maintenance plan schedule backup. And
Availability databases you shall not find out in the backup location
in Primary Replica . But When you shall check the 'Synchronization
state' through ' Show Dashboard' you shall find out, the both Replica
in 'Synchronized state'.
For reference i am writing the last script, which had ran in my server (as on dated '15-10-2016') through 'maintenance plan backup'
. Which script was as follows:-
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database1''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database1] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase1_backup_2016_10_15_000001_6209992.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database1_backup_2016_10_15_000001_6209992'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database2''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database2] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase2_backup_2016_10_15_000001_7520058.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database2_backup_2016_10_15_000001_7520058'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database3''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database3] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase3_backup_2016_10_15_000001_7770006.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database3_backup_2016_10_15_000001_7770006'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
BACKUP DATABASE [Database4] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase4_backup_2016_10_15_000001_8030022.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database4_backup_2016_10_15_000001_8030022'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
BACKUP DATABASE [Database5] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase5_backup_2016_10_15_000001_8280125.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database5_backup_2016_10_15_000001_8280125'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
Here I have only changed the database name otherwise everything is as it's the script.
As you can see the above code Database1,Database2 & Database3 are AAG Databases. That's why
automatically it check through 'Scalar-value Functions' (i,e; sys.fn_hadr_backup_is_preferred_replica
).
And For the database4 & database5 , it does not check through that function, because these databases are Non AAG Databases.
So, after execution of my 'primary server replica''maintenance plan schedular backup'' only i have found out the Non AAG Databases in specified backup location.
So, I can say that you are not missing anything in Ola Hallalengren's script.
For your Ref Backing Up SQL Server Databases Participating In AlwaysOn Availability Groups and Active Secondaries: Backup on Secondary Replicas (Always On Availability Groups)
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f152537%2faag-databases-not-getting-backed-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have configured to run backups using the "prefer secondary" option.
Prefer Secondary - Will backup on secondary with highest priority. If no secondary's are available, it will backup on primary.
When I triggered the job from the primary replica, only db's outside
the AAG are backing up and when I trigger it from one of the secondary
only AAG db's are backing up.
First of all i would like to say that i am try to explain in concise way but due to function logic , elaborate a bit more.
Once you have your backup preferences and priority established, then it is time to set up a SQL Server Agent Job to perform the backups. Thankfully Microsoft has provided us with a system function called sys.fn_hadr_backup_is_preferred_replica
. This function will return 0
if the current instance is not the preferred backup location or 1
if it is the preferred replica for backups according to the preference and priority settings.
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;
As per MSDB BOL Here To take the automated backup preference into account for a given availability group, on each server instance that hosts an availability replica whose backup priority is greater than zero (>0), you need to script backup jobs for the databases in the availability group. To determine whether the current replica is the preferred backup replica, use the sys.fn_hadr_backup_is_preferred_replica
function in your backup script. If the availability replica that is hosted by the current server instance is the preferred replica for backups, this function returns 1
. If not, the function returns 0
. By running a simple script on each availability replica that queries this function, you can determine which replica should run a given backup job.
Scripting a backup job with this logic enables you to schedule the job to run on every availability replica on the same schedule. Each of these jobs looks at the same data to determine which job should run, so only one of the scheduled job actually proceeds to the backup stage.
Note:-There is no enforcement of AutomatedBackupPreference. The interpretation of this preference depends on the logic, if any, that you script into backup jobs for the databases in a given availability group. The automated backup preference setting has no impact on ad-hoc backups.
As,i would like to say that as when you are configuring auto backup of
AAG through maintenance Plan in 'Primary Replica' & when your Backup
preferrence will be 'Preferred Secondary Replica' in that case also
the Non AAG databases will be backup in primary replica backup
location, after the execution of Maintenance plan schedule backup. And
Availability databases you shall not find out in the backup location
in Primary Replica . But When you shall check the 'Synchronization
state' through ' Show Dashboard' you shall find out, the both Replica
in 'Synchronized state'.
For reference i am writing the last script, which had ran in my server (as on dated '15-10-2016') through 'maintenance plan backup'
. Which script was as follows:-
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database1''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database1] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase1_backup_2016_10_15_000001_6209992.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database1_backup_2016_10_15_000001_6209992'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database2''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database2] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase2_backup_2016_10_15_000001_7520058.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database2_backup_2016_10_15_000001_7520058'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database3''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database3] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase3_backup_2016_10_15_000001_7770006.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database3_backup_2016_10_15_000001_7770006'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
BACKUP DATABASE [Database4] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase4_backup_2016_10_15_000001_8030022.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database4_backup_2016_10_15_000001_8030022'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
BACKUP DATABASE [Database5] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase5_backup_2016_10_15_000001_8280125.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database5_backup_2016_10_15_000001_8280125'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
Here I have only changed the database name otherwise everything is as it's the script.
As you can see the above code Database1,Database2 & Database3 are AAG Databases. That's why
automatically it check through 'Scalar-value Functions' (i,e; sys.fn_hadr_backup_is_preferred_replica
).
And For the database4 & database5 , it does not check through that function, because these databases are Non AAG Databases.
So, after execution of my 'primary server replica''maintenance plan schedular backup'' only i have found out the Non AAG Databases in specified backup location.
So, I can say that you are not missing anything in Ola Hallalengren's script.
For your Ref Backing Up SQL Server Databases Participating In AlwaysOn Availability Groups and Active Secondaries: Backup on Secondary Replicas (Always On Availability Groups)
add a comment |
I have configured to run backups using the "prefer secondary" option.
Prefer Secondary - Will backup on secondary with highest priority. If no secondary's are available, it will backup on primary.
When I triggered the job from the primary replica, only db's outside
the AAG are backing up and when I trigger it from one of the secondary
only AAG db's are backing up.
First of all i would like to say that i am try to explain in concise way but due to function logic , elaborate a bit more.
Once you have your backup preferences and priority established, then it is time to set up a SQL Server Agent Job to perform the backups. Thankfully Microsoft has provided us with a system function called sys.fn_hadr_backup_is_preferred_replica
. This function will return 0
if the current instance is not the preferred backup location or 1
if it is the preferred replica for backups according to the preference and priority settings.
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;
As per MSDB BOL Here To take the automated backup preference into account for a given availability group, on each server instance that hosts an availability replica whose backup priority is greater than zero (>0), you need to script backup jobs for the databases in the availability group. To determine whether the current replica is the preferred backup replica, use the sys.fn_hadr_backup_is_preferred_replica
function in your backup script. If the availability replica that is hosted by the current server instance is the preferred replica for backups, this function returns 1
. If not, the function returns 0
. By running a simple script on each availability replica that queries this function, you can determine which replica should run a given backup job.
Scripting a backup job with this logic enables you to schedule the job to run on every availability replica on the same schedule. Each of these jobs looks at the same data to determine which job should run, so only one of the scheduled job actually proceeds to the backup stage.
Note:-There is no enforcement of AutomatedBackupPreference. The interpretation of this preference depends on the logic, if any, that you script into backup jobs for the databases in a given availability group. The automated backup preference setting has no impact on ad-hoc backups.
As,i would like to say that as when you are configuring auto backup of
AAG through maintenance Plan in 'Primary Replica' & when your Backup
preferrence will be 'Preferred Secondary Replica' in that case also
the Non AAG databases will be backup in primary replica backup
location, after the execution of Maintenance plan schedule backup. And
Availability databases you shall not find out in the backup location
in Primary Replica . But When you shall check the 'Synchronization
state' through ' Show Dashboard' you shall find out, the both Replica
in 'Synchronized state'.
For reference i am writing the last script, which had ran in my server (as on dated '15-10-2016') through 'maintenance plan backup'
. Which script was as follows:-
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database1''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database1] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase1_backup_2016_10_15_000001_6209992.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database1_backup_2016_10_15_000001_6209992'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database2''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database2] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase2_backup_2016_10_15_000001_7520058.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database2_backup_2016_10_15_000001_7520058'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database3''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database3] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase3_backup_2016_10_15_000001_7770006.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database3_backup_2016_10_15_000001_7770006'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
BACKUP DATABASE [Database4] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase4_backup_2016_10_15_000001_8030022.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database4_backup_2016_10_15_000001_8030022'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
BACKUP DATABASE [Database5] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase5_backup_2016_10_15_000001_8280125.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database5_backup_2016_10_15_000001_8280125'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
Here I have only changed the database name otherwise everything is as it's the script.
As you can see the above code Database1,Database2 & Database3 are AAG Databases. That's why
automatically it check through 'Scalar-value Functions' (i,e; sys.fn_hadr_backup_is_preferred_replica
).
And For the database4 & database5 , it does not check through that function, because these databases are Non AAG Databases.
So, after execution of my 'primary server replica''maintenance plan schedular backup'' only i have found out the Non AAG Databases in specified backup location.
So, I can say that you are not missing anything in Ola Hallalengren's script.
For your Ref Backing Up SQL Server Databases Participating In AlwaysOn Availability Groups and Active Secondaries: Backup on Secondary Replicas (Always On Availability Groups)
add a comment |
I have configured to run backups using the "prefer secondary" option.
Prefer Secondary - Will backup on secondary with highest priority. If no secondary's are available, it will backup on primary.
When I triggered the job from the primary replica, only db's outside
the AAG are backing up and when I trigger it from one of the secondary
only AAG db's are backing up.
First of all i would like to say that i am try to explain in concise way but due to function logic , elaborate a bit more.
Once you have your backup preferences and priority established, then it is time to set up a SQL Server Agent Job to perform the backups. Thankfully Microsoft has provided us with a system function called sys.fn_hadr_backup_is_preferred_replica
. This function will return 0
if the current instance is not the preferred backup location or 1
if it is the preferred replica for backups according to the preference and priority settings.
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;
As per MSDB BOL Here To take the automated backup preference into account for a given availability group, on each server instance that hosts an availability replica whose backup priority is greater than zero (>0), you need to script backup jobs for the databases in the availability group. To determine whether the current replica is the preferred backup replica, use the sys.fn_hadr_backup_is_preferred_replica
function in your backup script. If the availability replica that is hosted by the current server instance is the preferred replica for backups, this function returns 1
. If not, the function returns 0
. By running a simple script on each availability replica that queries this function, you can determine which replica should run a given backup job.
Scripting a backup job with this logic enables you to schedule the job to run on every availability replica on the same schedule. Each of these jobs looks at the same data to determine which job should run, so only one of the scheduled job actually proceeds to the backup stage.
Note:-There is no enforcement of AutomatedBackupPreference. The interpretation of this preference depends on the logic, if any, that you script into backup jobs for the databases in a given availability group. The automated backup preference setting has no impact on ad-hoc backups.
As,i would like to say that as when you are configuring auto backup of
AAG through maintenance Plan in 'Primary Replica' & when your Backup
preferrence will be 'Preferred Secondary Replica' in that case also
the Non AAG databases will be backup in primary replica backup
location, after the execution of Maintenance plan schedule backup. And
Availability databases you shall not find out in the backup location
in Primary Replica . But When you shall check the 'Synchronization
state' through ' Show Dashboard' you shall find out, the both Replica
in 'Synchronized state'.
For reference i am writing the last script, which had ran in my server (as on dated '15-10-2016') through 'maintenance plan backup'
. Which script was as follows:-
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database1''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database1] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase1_backup_2016_10_15_000001_6209992.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database1_backup_2016_10_15_000001_6209992'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database2''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database2] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase2_backup_2016_10_15_000001_7520058.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database2_backup_2016_10_15_000001_7520058'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database3''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database3] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase3_backup_2016_10_15_000001_7770006.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database3_backup_2016_10_15_000001_7770006'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
BACKUP DATABASE [Database4] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase4_backup_2016_10_15_000001_8030022.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database4_backup_2016_10_15_000001_8030022'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
BACKUP DATABASE [Database5] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase5_backup_2016_10_15_000001_8280125.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database5_backup_2016_10_15_000001_8280125'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
Here I have only changed the database name otherwise everything is as it's the script.
As you can see the above code Database1,Database2 & Database3 are AAG Databases. That's why
automatically it check through 'Scalar-value Functions' (i,e; sys.fn_hadr_backup_is_preferred_replica
).
And For the database4 & database5 , it does not check through that function, because these databases are Non AAG Databases.
So, after execution of my 'primary server replica''maintenance plan schedular backup'' only i have found out the Non AAG Databases in specified backup location.
So, I can say that you are not missing anything in Ola Hallalengren's script.
For your Ref Backing Up SQL Server Databases Participating In AlwaysOn Availability Groups and Active Secondaries: Backup on Secondary Replicas (Always On Availability Groups)
I have configured to run backups using the "prefer secondary" option.
Prefer Secondary - Will backup on secondary with highest priority. If no secondary's are available, it will backup on primary.
When I triggered the job from the primary replica, only db's outside
the AAG are backing up and when I trigger it from one of the secondary
only AAG db's are backing up.
First of all i would like to say that i am try to explain in concise way but due to function logic , elaborate a bit more.
Once you have your backup preferences and priority established, then it is time to set up a SQL Server Agent Job to perform the backups. Thankfully Microsoft has provided us with a system function called sys.fn_hadr_backup_is_preferred_replica
. This function will return 0
if the current instance is not the preferred backup location or 1
if it is the preferred replica for backups according to the preference and priority settings.
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;
As per MSDB BOL Here To take the automated backup preference into account for a given availability group, on each server instance that hosts an availability replica whose backup priority is greater than zero (>0), you need to script backup jobs for the databases in the availability group. To determine whether the current replica is the preferred backup replica, use the sys.fn_hadr_backup_is_preferred_replica
function in your backup script. If the availability replica that is hosted by the current server instance is the preferred replica for backups, this function returns 1
. If not, the function returns 0
. By running a simple script on each availability replica that queries this function, you can determine which replica should run a given backup job.
Scripting a backup job with this logic enables you to schedule the job to run on every availability replica on the same schedule. Each of these jobs looks at the same data to determine which job should run, so only one of the scheduled job actually proceeds to the backup stage.
Note:-There is no enforcement of AutomatedBackupPreference. The interpretation of this preference depends on the logic, if any, that you script into backup jobs for the databases in a given availability group. The automated backup preference setting has no impact on ad-hoc backups.
As,i would like to say that as when you are configuring auto backup of
AAG through maintenance Plan in 'Primary Replica' & when your Backup
preferrence will be 'Preferred Secondary Replica' in that case also
the Non AAG databases will be backup in primary replica backup
location, after the execution of Maintenance plan schedule backup. And
Availability databases you shall not find out in the backup location
in Primary Replica . But When you shall check the 'Synchronization
state' through ' Show Dashboard' you shall find out, the both Replica
in 'Synchronized state'.
For reference i am writing the last script, which had ran in my server (as on dated '15-10-2016') through 'maintenance plan backup'
. Which script was as follows:-
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database1''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database1] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase1_backup_2016_10_15_000001_6209992.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database1_backup_2016_10_15_000001_6209992'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database2''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database2] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase2_backup_2016_10_15_000001_7520058.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database2_backup_2016_10_15_000001_7520058'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
DECLARE @preferredReplica int
SET @preferredReplica = (SELECT [master].sys.fn_hadr_backup_is_preferred_replica(''Database3''))
IF (@preferredReplica = 1)
BEGIN
BACKUP DATABASE [Database3] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase3_backup_2016_10_15_000001_7770006.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database3_backup_2016_10_15_000001_7770006'', SKIP, REWIND, NOUNLOAD, STATS = 10
END
GO
BACKUP DATABASE [Database4] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase4_backup_2016_10_15_000001_8030022.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database4_backup_2016_10_15_000001_8030022'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
BACKUP DATABASE [Database5] TO DISK = N''H:SQLSRV-N01_Backup_FilesAutomatic BackupDatabase5_backup_2016_10_15_000001_8280125.bak'' WITH COPY_ONLY, NOFORMAT, NOINIT, NAME = N''Database5_backup_2016_10_15_000001_8280125'', SKIP, REWIND, NOUNLOAD, STATS = 10
GO
Here I have only changed the database name otherwise everything is as it's the script.
As you can see the above code Database1,Database2 & Database3 are AAG Databases. That's why
automatically it check through 'Scalar-value Functions' (i,e; sys.fn_hadr_backup_is_preferred_replica
).
And For the database4 & database5 , it does not check through that function, because these databases are Non AAG Databases.
So, after execution of my 'primary server replica''maintenance plan schedular backup'' only i have found out the Non AAG Databases in specified backup location.
So, I can say that you are not missing anything in Ola Hallalengren's script.
For your Ref Backing Up SQL Server Databases Participating In AlwaysOn Availability Groups and Active Secondaries: Backup on Secondary Replicas (Always On Availability Groups)
answered Oct 20 '16 at 11:06
Md Haidar Ali KhanMd Haidar Ali Khan
3,57762340
3,57762340
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f152537%2faag-databases-not-getting-backed-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are there non-AG user databases on that secondary replica? Bear in mind that the system (master/model/msdb) databases would not be picked up by that script.
– Nic
Oct 18 '16 at 14:22
Yes I am not bothered about the System Databases. I was referring to user databases which are not in the AAG.
– user2923332
Oct 20 '16 at 14:08