Restore a web app from a backup in another subscription using PowerShell

This sample script retrieves a previously completed backup from an existing web app and restores it to a web app in another subscription.

If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount to create a connection with Azure.

Sample script

Note

We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

# This sample script retrieves a previously completed backup from an existing web app and restores it to a web app in another subscription.
# If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount to create a connection with Azure.

$resourceGroupNameSub1 = "<replace-with-your-group-name>"
$resourceGroupNameSub2 = "<replace-with-desired-new-group-name>"
$webAppNameSub1 = "<replace-with-your-app-name>"
$webAppNameSub2 = "<replace-with-desired-new-app-name>"
$appServicePlanSub2 = "<replace-with-desired-new-plan-name>"
$locationSub2 = "West Europe"


# Log into the subscription with the backup
Add-AzAccount

# List statuses of all backups that are complete or currently executing.
Get-AzWebAppBackupList -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1

# Note the BackupID property of the backup you want to restore

# Get the backup object that you want to restore by specifying the BackupID
$backup = (Get-AzWebAppBackup -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1 -BackupId '<replace-with-BackupID>')

# Get the storage account URL of the backup configuration
$url = (Get-AzWebAppBackupConfiguration -ResourceGroupName $resourceGroupNameSub1 -Name $webAppNameSub1).StorageAccountUrl

# Log into the subscription that you want to restore the app to
Add-AzAccount

# Create a new web app
New-AzWebApp -ResourceGroupName $resourceGroupNameSub2 -AppServicePlan $appServicePlanSub2 -Name $webAppNameSub2 -Location $locationSub2

# Restore the app by overwriting it with the backup data
Restore-AzWebAppBackup -ResourceGroupName $resourceGroupNameSub2 -Name $webAppNameSub2 -StorageAccountUrl $url -BlobName $backup.BlobName -Overwrite

Clean up deployment

If you don't need the web app anymore, use the following command to remove the resource group, web app, and all related resources.

Remove-AzResourceGroup -Name $resourceGroupName -Force

Script explanation

This script uses the following commands. Each command in the table links to command specific documentation.

Command Notes
Add-AzAccount Adds an authenticated account to use for Azure Resource Manager cmdlet requests.
Get-AzWebAppBackupList Gets a list of backups for a web app.
Get-AzWebAppBackup Gets a backup for a web app using the backup ID.
Get-AzWebAppBackupConfiguration Gets the backup configuration for a web app.
New-AzWebApp Creates a web app
Restore-AzWebAppBackup Restores a web app from a previously completed backup.

Next steps

For more information on the Azure PowerShell module, see Azure PowerShell documentation.

Additional Azure PowerShell samples for Azure App Service Web Apps can be found in the Azure PowerShell samples.