Share via

Fail a runbook database backup

Binway 736 Reputation points
2021-05-27T01:21:30.097+00:00

I have a runbook that is creating a bacpac file for a database sending it to Blob Storage. I also have the Automation being monitored so that if it fails I get an alert. To test the alert I have configured the database to be backed up so that the creation of the backup fails which is what I see in the Import/export blade of the server.
But the runbook and automation gives a status of complete when it should be failing.
It seems that it reads the code for the backup connection and if that succeeds then it is Complete but the creation of the backup file actually fails.
The code I have is:
#create the bacpac file and land in the blob storage
$exportRequest = New-AzSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri
-AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

#if bacpac fails then fail runbook so AZ Monitor error message is generated
$ErrorActionPreference = "Stop"

But I must be missing something.
Appreciate any thoughts.

wayne

Azure Automation
Azure Automation

An Azure service that is used to automate, configure, and install updates across hybrid environments.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 42,936 Reputation points MVP Volunteer Moderator
    2023-12-27T13:01:48.1533333+00:00

    It's important to verify whether the New-AzSqlDatabaseExport cmdlet is returning a failure status when the backup fails. You might need to check the status of the $exportRequest object to see if it contains any error information or a status code that indicates a failure.he $ErrorActionPreference = "Stop" setting should halt the script execution on any terminating error, but it might not catch non-terminating errors. You might need to add additional error handling to catch these types of errors. After initiating the export with New-AzSqlDatabaseExport, you can use Get-AzSqlDatabaseImportExportStatus to monitor the status of the operation. This cmdlet can provide more detailed information about the status of the export and can help you determine if it has failed.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.