Azure App Service
- By Avinash Valiramani
- 9/24/2022
Backup and restore
Azure App Service has a built-in backup and restore feature available for the Standard, Premium, and Isolated tiers. This feature allows you to create backups manually or on an automated schedule. Backups can be retained indefinitely, which is useful for organizations with long-term data-retention requirements.
The Azure App Service backup feature can currently back up the following:
File contents
The application configuration
Databases interconnected for use with the app, including Azure SQL Database, Azure Database for MySQL, and Azure Database for PostgreSQL
Some key features of this built-in backup service are as follows:
Each backup is triggered as a full backup rather than an incremental backup. This makes restore operations fast and reliable, as dependencies on earlier backups are reduced.
Restore operations can restore to the existing app or to a new app. This is useful in scenarios in which you need to create a new test or dev environment from a recent backup.
You can schedule backups using the Azure Portal, Azure PowerShell, or the Azure CLI.
It’s important to be aware of a few limitations when setting up the backup feature:
Backups are supported for a maximum of 10 GB of app and database content. In addition, backups of individual databases can have a maximum size of 4 GB. Backups exceeding this size will fail.
This feature is supported only for the Standard and Premium Dedicated tiers and the Isolated tier.
If VNet integration is in place, the backup and restore features cannot be used at the same time.
The Azure storage required to store the backups should be in the same subscription, and preferably the same region, as the App Service.
The firewall service for the storage account should not be configured or online.
Backups for in-app MySQL databases are automatic, without the need for any configuration steps. However, if manual configuration settings, such as connection strings, are put in place, automated backups will not work correctly.
TLS encryption for MySQL or PostgreSQL will cause backups to fail. If that is a requirement for the environment, consider a third-party backup solution.
Backup walkthrough
The following sections step you through the process of setting up backups for your web app using the Azure Portal, Azure PowerShell, and the Azure CLI.
Using the Azure Portal
To set up backups using the Azure Portal, follow these steps:
In the left pane of the web app’s configuration page, under Settings, click Backup. (See Figure 3-48.) By default, no backups are configured.
FIGURE 3-48 Backup configuration for web apps.
Under Backup, click the Backup Is Not Configured. Click Here to Configure Backup for Your App link. (See Figure 3-49.)
FIGURE 3-49 Backup is not configured.
In the Backup Configuration page, enter the following information (see Figure 3-50):
Backup Storage Click the container in which you want to store the backup.
Scheduled Backup Click On.
Backup Every Specify how frequently backups should occur, in days or hours.
Start Backup Schedule From Specify the date, time, and time zone when the backup schedule should start.
Retention (Days) Specify how long (in days) the backup should be retained.
Keep At Least One Backup Click Yes to ensure that at least one backup is always retained.
FIGURE 3-50 Configuring backup settings.
Using Azure PowerShell
You can set up backups for a web app with Azure PowerShell using the New-AzWebAppBackup command. This command triggers a one-time backup. To schedule a backup, you must use the Edit-AzWebAppBackupConfiguration command.
To trigger a one-time backup, use the following code:
#Set variables $resourcegroup="AppServiceRG01" $appname="mywebapp$(Get-Random -Minimum 100000 -Maximum 999999)" $storageaccountname="backupsstorage" $storagecontainer="backups" $location="East US 2" $backupname="BackupOnce" # Generate an SAS token for the storage container, valid for one month. $sastokenUrl = New-AzStorageContainerSASToken -Name $storagecontainer -Permission rwdl -Context $storageaccountname.Context -ExpiryTime (Get-Date).AddMonths(1) -FullUri # Create a one-time backup New-AzWebAppBackup -ResourceGroupName $resourcegroup -Name $appname -StorageAccountUrl $sastokenUrl -BackupName $backupname
To schedule a backup, use the following code:
#Set variables $resourcegroup="AppServiceRG01" $appname="MSBPAPP01" $storageaccountname="backupsstorage" $storagecontainer="backups" $location="East US 2" # Generate an SAS token for the storage container, valid for 1 year. $sastokenUrl = New-AzStorageContainerSASToken -Name $storagecontainer -Permission rwdl -Context $storageaccountname.Context -ExpiryTime (Get-Date).AddYears(1) -FullUri # Schedule a backup every day, beginning in two hours, and retain for 30 days Edit-AzWebAppBackupConfiguration -ResourceGroupName $resourcegroup -Name $appname -StorageAccountUrl $sastokenUrl -FrequencyInterval 1 -FrequencyUnit Day -KeepAtLeastOneBackup -StartTime (Get-Date).AddHours(2) -RetentionPeriodInDays 30
Using the Azure CLI
You can set up backups for a web app with the Azure CLI using the az webapp config backup create command. This command triggers a one-time backup. To schedule a backup, you must use the az webapp config backup update command.
To trigger a one-time backup, use the following code:
#!/bin/bash #Set variables resourcegroupname="AppServiceRG01" appname=MSBP-APP01 storagename=backupsstorage location="EastUS2" backupcontainer="backups" backupname="backuponce" expirydate=$(date -I -d "$(date) + 2 months") # Generates an SAS token for the storage container, valid for one month. sastoken=$(az storage container generate-sas --account-name $storagename --name $backupcontainer --expiry $expirydate --permissions rwdl --output tsv) # Construct the SAS URL for the container sastokenurl=https://$storagename.blob.core.windows.net/$backupcontainer?$sastoken # Create a one-time backup az webapp config backup create --resource-group $resourcegroupname --webapp-name $appname --backup-name $backupname --container-url $sastokenurl
To schedule a backup, use the following code:
#!/bin/bash #Set variables resourcegroupname="AppServiceRG01" appname=MSBP-APP01 storagename=backupsstorage location="EastUS2" backupcontainer="appbackup" expirydate=$(date -I -d "$(date) + 1 month") # Generate an SAS token for the storage container, valid for one month. sastoken=$(az storage container generate-sas --account-name $storagename --name $backupcontainer --expiry $expirydate --permissions rwdl --output tsv) # Construct the SAS URL for the container sastokenurl=https://$storagename.blob.core.windows.net/$backupcontainer?$sastoken # Schedule a backup every day and retain for 10 days az webapp config backup update --resource-group $resourcegroupname --webapp-name $appname --container-url $sastokenurl --frequency 1d --retain-one true --retention 10