Azure App Services: Get Scheduled Backup Configuration for All App Services and Associated Slots using PowerShell
In this blog I will share one way to get Azure App Services scheduled backup configuration for a all App Services and all App Services and each App Service Slots using PowerShell.
My blogs have relatively simple, and sometimes complex, examples and I’m hoping that you will be able to tailor them to your need or use them in your own scripts.
To get scheduled backup configuration for a single App Service and its slots, Azure App Services: Get Scheduled Backup Configuration for a Single App Service and Associated Slots using PowerShell.
To get App Services deployment slots, review Azure App Services: Get Deployment Slots using PowerShell.
Prerequisites
- Install Azure PowerShell if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
- Install Azure Active Directory PowerShell module if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
Introduction
The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, the perfect solution to a challenge or the perfect process to accomplish a task.
Using PowerShell, I will show you how to get scheduled backup configuration for:
- All App Services.
- All App Services and each App Service Slots.
I will also show, for demonstration, how to display PowerShell cmdlet output in a table format.
PowerShell Cmdlets
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Set-AzContext Sets the tenant, subscription, and environment for cmdlets to use in the current session.
- Get-AzWebApp Gets Azure Web Apps in the specified resource group.
- Get-AzWebAppBackupConfiguration Gets the backup configuration of an Azure Web App.
- Get-AzWebAppSlot Gets an Azure Web App slot.
Sign in to Azure
- Sign in to Azure. If you have multiple subscriptions or tenants, make sure to sign in to the correct subscription.
- You can use Set-AzContext to set the tenant, subscription, and environment for cmdlets to use in the current session.
1Connect-AzAccount -Subscription "aa1111a1-1111-1a1a-11a1-1111a1a1a1a1"
Get scheduled backup configuration for all App Services
- Use Get-AzWebApp to get a list of all App Services in the subscription.
- Initialize a variable, $OutTable, to save output.
- (a) For each Web App, (b) use Get-AzWebAppBackupConfiguration to get the App Service scheduled backup configuration.
- If the App Service does not have a scheduled backup configuration, add “No backup scheduled” to $OutTable.
- If the App Service has a scheduled backup configuration, add it to $OutTable.
- Display $OutTable.
123456789101112131415161718192021222324252627282930313233$WebApps = Get-AzWebApp | Sort-Object Name # 1. Get a list of all Web Apps in the subscription$OutTable = @() # 2. Initialize an array variable to save outputWrite-Host "" # display empty lineforeach ($WebApp in $WebApps) { # 3. (a) For each Web AppWrite-Host ("Getting backup schedule configuration for [" + $WebApp.Name + "]")# 3. (b) get the App Service scheduled backup configuration$RetValues = Get-AzWebAppBackupConfiguration -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue$Result = "" | Select Name, StorageAccountUrl, FrequencyInterval, FrequencyUnit, RetentionPeriodInDays, StartTime, KeepAtLeastOneBackup, Enabled$Result.Name = $WebApp.Nameif ($RetValues -eq $null) { # 4. If the web App does not have a backup schedule, add “No backup scheduled” to $OutTable$Result.StorageAccountUrl = "No backup scheduled"$Result.FrequencyInterval = ""$Result.FrequencyUnit = ""$Result.RetentionPeriodInDays = ""$Result.StartTime = ""$Result.KeepAtLeastOneBackup = ""$Result.Enabled = ""}else {# 5. If the App Service has a scheduled backup configuration, add it to $OutTable.$Result.StorageAccountUrl = ($RetValues.StorageAccountUrl).Split('/')[2]$Result.FrequencyInterval = $RetValues.FrequencyInterval$Result.FrequencyUnit = $RetValues.FrequencyUnit$Result.RetentionPeriodInDays = $RetValues.RetentionPeriodInDays$Result.StartTime = $RetValues.StartTime$Result.KeepAtLeastOneBackup = $RetValues.KeepAtLeastOneBackup$Result.Enabled = $RetValues.Enabled}$OutTable += $Result}Write-Host ""# 6. Display $OutTable$OutTable | Format-Table
Get scheduled backup configuration for all App Services and each App Service Slots
- Use Get-AzWebApp to get a list of all App Services in the subscription.
- Initialize a variable, $OutTable, to save output.
- (a) For each Web App, (b) get the App Service scheduled backup configuration using Get-AzWebAppBackupConfiguration.
- If the App Service does not have a scheduled backup configuration, add “No backup scheduled” to $OutTable otherwise add scheduled backup configuration to $OutTable.
- Use Get-AzWebAppSlot to get App Service slots
- If the App Service has slots, get scheduled backup configuration for each slot. Otherwise, go to the next App Service.
- (a) For each slot, (b) get slot scheduled backup configuration.
- If the App Service slot does not have a scheduled backup configuration, add “No backup scheduled” to $OutTable otherwise add scheduled backup configuration to $OutTable.
- Display $OutTable.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758function Set-Result {[CmdletBinding()]param([Parameter()][String] $WebAppName,[Object] $BackupConfig)$Result = "" | Select Name, StorageAccountUrl, FrequencyInterval, FrequencyUnit, RetentionPeriodInDays, StartTime, KeepAtLeastOneBackup, Enabled$Result.Name = $WebAppNameif ($BackupConfig -eq $null) { # App Service does not have a scheduled backup configuration$Result.StorageAccountUrl = "No backup scheduled"$Result.FrequencyInterval = ""$Result.FrequencyUnit = ""$Result.RetentionPeriodInDays = ""$Result.StartTime = ""$Result.KeepAtLeastOneBackup = ""$Result.Enabled = ""}else {# Populate scheduled backup configuration in $Result$Result.StorageAccountUrl = ($BackupConfig.StorageAccountUrl).Split('/')[2]$Result.FrequencyInterval = $BackupConfig.FrequencyInterval$Result.FrequencyUnit = $BackupConfig.FrequencyUnit$Result.RetentionPeriodInDays = $BackupConfig.RetentionPeriodInDays$Result.StartTime = $BackupConfig.StartTime$Result.KeepAtLeastOneBackup = $BackupConfig.KeepAtLeastOneBackup$Result.Enabled = $BackupConfig.Enabled}return ($Result)}$WebApps = Get-AzWebApp | Sort-Object Name # 1. Get a list of all Web Apps in the subscription$OutTable = @() # 2. Initialize an array variable to save outputWrite-Host "" # display empty lineforeach ($WebApp in $WebApps) { # 3. (a) For each Web AppWrite-Host ("Getting backup schedule configuration for [" + $WebApp.Name + "]")# 3. (b) Get scheduled backup configuration$RetValues = Get-AzWebAppBackupConfiguration -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue# 4. If the App Service does not have a scheduled backup configuration, add “No backup scheduled” to $OutTable# otherwise add scheduled backup configuration to $OutTable$OutTable += Set-Result -WebAppName $WebApp.Name -BackupConfig $RetValues# 5. Get App Service slots$WebAppSlots = Get-AzWebAppSlot -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue# 6. If the App Service has slots, get scheduled backup configuration for each slot. Otherwise, go to the next App Serviceif ($WebAppSlots -ne $null) {foreach ($WebAppSlot in $WebAppSlots) { # 7. (a) For each slot,Write-Host ("Getting backup schedule configuration for [" + $WebAppSlot.Name + "]")# 7. (b) get slot scheduled backup configuration$RetValues = Get-AzWebAppBackupConfiguration -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue# 8. If the App Service slot does not have a scheduled backup configuration, add “No backup scheduled” to $OutTable# otherwise add scheduled backup configuration to $OutTable.$OutTable += Set-Result -WebAppName $WebAppSlot.Name -BackupConfig $RetValues}}}Write-Host ""# 9. Display $OutTable$OutTable | Format-Table
Conclusion
In this blog we explored how to get Azure App Services scheduled backup configuration using PowerShell. We also explored how to output PowerShell cmdlet result as a table.
Did you find this blog easy to follow and helpful to you? Let me know in the comments below.
Disclaimer
Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
There's no comments