How to create a backup of webapp from Azure Powershell to a storage account?
Note: This is a post for sharing piece of information, that I found helpful in my solution.
Tools & Pre-requisites :
Azure Powershell 4.0+
Windows Powershell ISE
Knowledge of StorageAccount ( Name, ResourceGroup Name )
Knowledge of WebApp for which backup is desired ( WebApp Name and ResourceGroup Name )
Valid & Active Azure portal / AD login credentials
##########################
# Goal : To Archive WebApp desired to Storage Container
# @Param ( StorageAccountName, ContainerName, WebAppName, StorageAccountResourceGroup, AppResourceGroupName )
Function getArchiveStorageHandle
{
param (
$StorageAccountName
,$AppBackupContainerName
,$webappName
,$WebStorgaeResourceGroupName
,$appRGname
)
######################################################################
#Set Active Storage to Template Storage Context
$storage = Get-AzureRmStorageAccount -ResourceGroupName $WebStorgaeResourceGroupName -Name $StorageAccountName
Write-Output ( "========================== Storage Account Context Object ===========================")
Write-Output ( $storage.Context )
$now = Get-Date
######################################################################
#Set SAS Token to get access to Container
$SAStoken = New-AzureStorageContainerSASToken -Name $AppBackupContainerName -Context $storage.Context -Permission rwdl -StartTime $now.AddHours(-1) -ExpiryTime $now.AddMonths(1) -FullUri
Write-Output ( "======== Use this SAS token if prompted for manual submission Storage Account URL ==========")
Write-Output ( $SAStoken )
######################################################################
#Set BackupName and Initiate Backup process
$backup_storage_name = $webappName + '-' + $now.DayOfYear
$backup = New-AzureRmWebAppBackup -ResourceGroupName $appRGname -Name $webappName -StorageAccountUrl $SAStoken -BackupName $backup_storage_name -Verbose
# Wait 20 seconds and check the status of the backup
Start-Sleep 20
$status = ($backup | Get-AzureRmWebAppBackup).BackupStatus
Write-Host "The backup status is $status" -foreground "Yellow"
Return $status
}
####### FN: GetArchiveStorage Ends Here #####
#############################################
$properties = @{
'connectionName' = "AzureRunAsConnection";
'myResourceGroupName' = "{your_webapp_Resource_group_here}";
'mySubscriptionName' = "{your subscription name}";
'date' = ( get-date ).ToString('MM/dd/yyyy');
'StorageAccountName' = '{your_storage_account_name}';
'AppBackupContainerName' = '{your_backup_containername_in_storage_account}';
'WebStorgaeResourceGroupName' = "{your_storage_account_resource_group}"; #Pull from the JSON file
'ResourceGroupLocation' = "East US"; #Please change this to actual if you prefer to use it.
}
$configObject = New-Object –TypeName PSObject –Prop $properties
Write-Output $configObject
Login-AzureRmAccount
Get-AzureRmSubscription –SubscriptionName $configObject.mySubscriptionName | Select-AzureRmSubscription
Get-AzureRmContext).Subscription
Select-AzureRMSubscription -SubscriptionName $configObject.mySubscriptionName
$ArchiveStatus = getArchiveStorageHandle -StorageAccountName $configObject.StorageAccountName -AppBackupContainerName $configObject.AppBackupContainerName -webappName $Resource.ResourceName -WebStorgaeResourceGroupName $configObject.WebStorgaeResourceGroupName -appRGname $configObject.myResourceGroupName
if( $ArchiveStatus -Match "Succeeded"){
Write-Host "HURRAY! Archival Completed Successfully!!"
}
Point to Know further: Check your App Service Tier plan. This sets restriction on number of backups per day, like 20.If you got more apps and(or) frequency per day is higher, need to take account of this constraint.
Disclaimer: The intention is to share to another newbie who might find this helpful.