Azure App Services: Get Scheduled Backup Configuration for a Single App Service and Associated Slots using PowerShell
In this blog I will share one way to get Azure App Services scheduled backup configuration for a single App Service and its 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 all App Services and their slots, review Azure App Services: Get Scheduled Backup Configuration for All App Services 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:
- A single App Service.
- A single App Service and its Slots.
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-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 a single App Service
- Use Get-AzWebAppBackupConfiguration to get App Service scheduled backup configuration.
1Get-AzWebAppBackupConfiguration -Name "app1-saad-dev" -ResourceGroupName "rg-app1-saad-dev"
Get scheduled backup configuration for a single App Service and its slots
- Initialize an array variable, $OutTable, to save output.
- Get App Service scheduled backup configuration.
- Add App Service scheduled backup configuration to $OutTable.
- Use Get-AzWebAppSlot to get App Service slots.
- If the App Service has slots, get slot scheduled backup configuration.
- (a) For each slot, (b) get slot scheduled backup configuration.
- If the web App does not have a backup schedule, add “No backup scheduled” to $OutTable otherwise add slot scheduled backup configuration to $OutTable.
- Display $OutTable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
function Set-Result { [CmdletBinding()] param( [Parameter()] [String] $WebAppName, [Object] $BackupConfig ) $Result = "" | Select Name, StorageAccountUrl, FrequencyInterval, FrequencyUnit, RetentionPeriodInDays, StartTime, KeepAtLeastOneBackup, Enabled $Result.Name = $WebAppName if ($BackupConfig -eq $null) { # App Service does not have a backup scheduled $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) } $OutTable = @() # 1. Initialize an array variable to save output Write-Host "" # display empty line # 2. Get App Service scheduled backup configuration Write-Host ("Getting backup schedule configuration for [app1-saad-dev]") $RetValues = Get-AzWebAppBackupConfiguration -Name "app1-saad-dev" -ResourceGroupName rg-app1-saad-dev # 3. Add scheduled backup configuration to $OutTable $OutTable += Set-Result -WebAppName $WebApp.Name -BackupConfig $RetValues # 4. Get App Service slots $WebAppSlots = Get-AzWebAppSlot -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue if ($WebAppSlots -ne $null) { # 5. If the App Service has slots, get slot scheduled backup configuration foreach ($WebAppSlot in $WebAppSlots) { # 6. (a) For each slot, Write-Host ("Getting backup schedule configuration for [" + $WebAppSlot.Name + "]") # 6. (b) get slot scheduled backup configuration $RetValues = Get-AzWebAppBackupConfiguration -Name $WebApp.Name -ResourceGroupName $WebApp.ResourceGroup -ErrorAction SilentlyContinue # 7. If the web App does not have a backup schedule, add “No backup scheduled” to $OutTable otherwise add slot scheduled backup configuration to $OutTable $OutTable += Set-Result -WebAppName $WebAppSlot.Name -BackupConfig $RetValues } } Write-Host "" # 8. 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