Search code examples
azurepowershellazure-web-app-servicebackup

New-AzWebAppBackup: Operation returned an invalid status code 'Bad Request'


When I'm trying to execute the Azure App Service Backup over Azure Powershell with New-AzWebAppBackup cmdlet as described here:

https://learn.microsoft.com/en-us/powershell/module/az.websites/New-AzWebAppBackup?view=azps-3.3.0

I'm getting the following error message:

PS Azure:\> New-AzWebAppBackup -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -StorageAccountUrl "https://mystorageaccount.file.core.windows.net/"
New-AzWebAppBackup : Operation returned an invalid status code 'BadRequest'
At line:1 char:1
+ New-AzWebAppBackup -ResourceGroupName "MyResourceGroup" -Name " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [New-AzWebAppBackup], DefaultErrorResponseException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps.NewAzureWebAppBackup

Azure Powershell Screenshot

Does anyone know what I'm missing? I'm thankful for any advice. Many Thanks.


Solution

  • Hello and welcome to Stack Overflow!

    Hope your webapp satisfies the prerequisites to be able to take a backup. The Backup and Restore feature requires the App Service plan to be in the Standard tier or Premium tier. Refer to Requirements and restrictions for the complete details.

    I got the same error initially when executing the cmdlet. However, passing the -debug switch with the cmdlet helped me understand the error better:

    {
     "ErrorEntity": {
     "ExtendedCode": "04205",
     "MessageTemplate": "The provided URI is not a SAS URL for a container (it needs to be https and it has to have 2 segments).",
     "Parameters": [],
     "Code": "BadRequest",
     "Message": "The provided URI is not a SAS URL for a container (it needs to be https and it has to have 2 segments)."
    }
    

    The -StorageAccountUrl parameter expects a SAS URL to be passed (refer this doc). The SAS URL for your Storage account is of the format:

    sasurl=https://$storagename.blob.core.windows.net/$container$sastoken
    

    To generate the SAS token, run the following:

    # Retrieve one of the Storage Account keys
    $key = (Get-AzStorageAccountKey -ResourceGroupName "<rg-name>" -AccountName "<storage-account-name>").Value[0]
    
    # Populate the Storage Account context
    $ctx = New-AzureStorageContext -StorageAccountName "<storage-account-name>" -StorageAccountKey $key
    
    # Generate the SAS token
    $sastoken = New-AzureStorageContainerSASToken -Name "<container-name>" -Permission rwdl -Context $ctx
    
    # Generate the SAS URL
    $sasurl = "https://<storage-account-name>.blob.core.windows.net/<container-name>$sastoken"
    
    # Create a backup
    New-AzureRmWebAppBackup -ResourceGroupName "<rg-name>" -Name "<app-name>" -StorageAccountUrl $sas -BackupName "<backup-name>"
    

    Successful execution of the above commands should let you create the backup. The response would look something like this: webapp backup

    Hope this helps!