Availability Group – SQL Server Agent Job to run only on Always On Primary Replica

-> I had a requirement to execute jobs only in primary replica.

-> To all jobs that should only be executed on Always On primary replica. I added 1 step as the first step of the job and another step as last step of that job.

-> 1st step of these jobs should execute below TSQL Query,

if (select
ars.role_desc
from sys.dm_hadr_availability_replica_states ars
inner join sys.availability_groups ag
on ars.group_id = ag.group_id
where ag.name = ''
and ars.is_local = 1) = 'PRIMARY'
begin
Print 'This is Primary Replica'
end
else
begin
RAISERROR('This job should not run in Secondary',16,1)
end

-> For 1st step,

On success action : Go to the next step
On failure action: Go to step: [n] Last_Step ( It should basically go to the last step)

-> Last step of the job should execute the below TSQL,

print 'Job_Completion'

-> For Last step,

On success action : Quit the job reporting Success
On failure action: Quit the job reporting failure

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

RESTORE detected an error on page (0:0) in database “Database” as read from the backup set.

-> I was restoring a database backup using SSMS.

-> I got below error,

1.Restore_Error.PNG

TITLE: Microsoft SQL Server Management Studio
——————————
Restore of database ‘JBS_DB’ failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
——————————
ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: RESTORE detected an error on page (0:0) in database “JBS_DB” as read from the backup set. (Microsoft.SqlServer.SmoExtended)
For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=15.0.18131.0+((SSMS_Rel).190606-1032)&LinkId=20476
——————————
BUTTONS:
OK
——————————

-> Executing a restore verify only command as below,

restore verifyonly from disk ='C:\Temp\JBS_DB_10July2019.bak'

-> Received below message,

 

2.Restore_Verify.PNG
Msg 3203, Level 16, State 1, Line 1
Read on “C:\Temp\JBS_DB_10July2019.bak” failed: 13(The data is invalid.)
Msg 3013, Level 16, State 1, Line 1
VERIFY DATABASE is terminating abnormally.

-> It seems like the backup file is corrupted. Performed another backup on the database and tried a restore again and it failed with same error.

-> May be the database itself is corrupted. Executed a DBCC Checkdb on the database where backup was performed. It came out clean. So this is not a corruption issue.

-> Looking closely at the error message. The error is on page 0:0. In reality, there is no page 0:0 in a database.

-> Spoke to the application team handling the database and understood that they are using a Third party tool to encrypt the database. After discussion, I advised them to decrypt the database using the tool.

-> I performed a backup and performed a restore. This time restore worked fine.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

 

Restore Database – Property BackupDirectory is not available for Settings ‘Microsoft.SqlServer.Management.Smo.Settings’

-> I was trying to restore a database using SQL Server management studio. Right Click “Database” Folder -> Restore Database -> select “Device” -> Navigate the backup file and Click “OK”.

-> Received below error,

TITLE: Microsoft SQL Server Management Studio
——————————
Property BackupDirectory is not available for Settings ‘Microsoft.SqlServer.Management.Smo.Settings’. This property may not exist for this object, or may not be retrievable due to insufficient access rights. (Microsoft.SqlServer.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=14.0.17289.0+((SSMS_Rel_17_4).181117-0805)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.PropertyCannotBeRetrievedExceptionText&EvtID=BackupDirectory&LinkId=20476
——————————
BUTTONS:
OK
——————————

Error

-> The error states that Property BackupDirectory is not available.

-> Right Click “SQL Server Instance” -> Click properties -> Database Settings -> Check “Database Default Locations”.

-> In my case “Backup: ” was empty,

Settings.PNG

-> Changed the default backup location to a valid path and tried the restore again. It worked fine this time!

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.