How to Backup/Restore an Azure App Service using Azure PowerShell

Use these Azure PowerShell cmdlets:

  • New-AzureRmWebAppBackup
  • Restore-AzureRmWebAppBackup
  • Get-AzureRmStorageAccountKey
  • New-AzureStorageContainerSASToken

Don’t forget that this is open source, so you can see what these cmdlets are doing on GitHub here.

Note that creating and restoring backups are only available in STANDARD and PREMIUM mode.

Check out how to do the same using BASH from the console here.

Login and set the Azure Subscription using these 2 cmdlets which I also discuss here.  “How to set Azure PowerShell to a specific Azure Subscription”

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId “25ec5bae-****-****-****-***********”

Make sure that you have an existing Storage Account and container for the backups to be put into and retrieved from for the restore.  See Figure 1.

image

Figure 1, how to backup an App Service using Azure PowerShell, storage container

I also upgraded to the most recent version of Azure PowerShell which I recommend and explain how to do that here.  “How to tell which Azure PowerShell version you are running”, see Figure 2

image

image

image

Figure 2, how to backup an App Service using Azure PowerShell, update the client

When I first started trying to create the backup I was simply using the New-AzureRmWebAppBackup cmdlet.  This failed with the following exception:

New-AzureRmWebAppBackup : The provided URI is not a SAS URL for a container (it needs to be https and it has to have 2 segments).
  At line:1 char:1
  + New-AzureRmWebAppBackup -ResourceGroupName $RGName -Name $WebAppName …
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo : CloseError: (:) [New-AzureRmWebAppBackup], CloudException
  + FullyQualifiedErrorId : Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps.NewAzureWebAppBackup

I ultimately found these cmdlets, Get-AzureRmStorageAccountKey and New-AzureStorageContainerSASToken which I used in combination to create the sasURL value which I passed to the cmdlet.

New-AzureRmWebAppBackup -ResourceGroupName $buRg -Name $webApp -StAcctUrl $sasUrl -BackupName “standard-backup001”

The sasURL looks something like the following.

https [:] //storageAccountName.blob.core.windows.net/containerName?sv=2017-04-17&sr=c&sig=aTOKEN&se=2…&sp=rwdl

I ran the Get-*, New-…SAS…* and the New-…Backup… cmdlets and the backup was successful, as seen in Figure 3.

image

Figure 3, how to backup an App Service using Azure PowerShell, validated

Then to execute the restore, execute the following cmdlet:

Restore-AzureRmWebAppBackup -ResourceGroupName $restoreToRg -Name $restoreToWebApp -StorageAccountUrl $sasUrl -BlobName $blobName -Overwrite

This can be used to clone an App Service and move it onto a different App Service Plan(*) and into a different Resource Group.  Very nice.