Set up a backup for a WebApp with PowerShell in Azure

%3CLINGO-SUB%20id%3D%22lingo-sub-1814841%22%20slang%3D%22en-US%22%3ESet%20up%20a%20backup%20for%20a%20WebApp%20with%20PowerShell%20in%20Azure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1814841%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Azure%20friends%2C%3C%2FP%3E%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%26nbsp%3BPlease%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23So%20that%20you%20can%20carry%20out%20the%20configuration%2C%20you%20need%20the%20necessary%20cmdlets%2C%20these%20are%20contained%20in%20the%20module%20Az%20(is%20the%20higher-level%20module%20from%20a%20number%20of%20submodules)%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall-Module%20-Name%20Az%20-Force%20-AllowClobber%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Log%20into%20Azure%3CBR%20%2F%3E%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Select%20the%20correct%20subscription%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzContext%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-AzSubscription%20-SubscriptionName%20%22your%20subscription%20name%22%20%7C%20Select-AzSubscription%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23%20Some%20Variables%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSTRONG%3E%24webappname%3D%22mywebapp%24(Get-Random%26nbsp%3B-Minimum%26nbsp%3B100000%26nbsp%3B-Maximum%26nbsp%3B999999)%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24storagename%3D%22%24(%24webappname)storage%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24container%3D%22appbackup%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24location%3D%22West%26nbsp%3BEurope%22%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BCreate%26nbsp%3Ba%26nbsp%3Bresource%26nbsp%3Bgroup.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ENew-AzResourceGroup%26nbsp%3B-Name%26nbsp%3BmyResourceGroup%26nbsp%3B-Location%26nbsp%3B%24location%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BCreate%26nbsp%3Ba%26nbsp%3Bstorage%26nbsp%3Baccount.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24storage%26nbsp%3B%3D%26nbsp%3BNew-AzStorageAccount%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-Name%26nbsp%3B%24storagename%26nbsp%3B-SkuName%26nbsp%3BStandard_LRS%26nbsp%3B-Location%26nbsp%3B%24location%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BCreate%26nbsp%3Ba%26nbsp%3Bstorage%26nbsp%3Bcontainer.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ENew-AzStorageContainer%26nbsp%3B-Name%26nbsp%3B%24container%26nbsp%3B-Context%26nbsp%3B%24storage.Context%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BGenerates%26nbsp%3Ban%26nbsp%3BSAS%26nbsp%3Btoken%26nbsp%3Bfor%26nbsp%3Bthe%26nbsp%3Bstorage%26nbsp%3Bcontainer%2C%26nbsp%3Bvalid%26nbsp%3Bfor%26nbsp%3B1%26nbsp%3Byear.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BNOTE%3A%26nbsp%3BYou%26nbsp%3Bcan%26nbsp%3Buse%26nbsp%3Bthe%26nbsp%3Bsame%26nbsp%3BSAS%26nbsp%3Btoken%26nbsp%3Bto%26nbsp%3Bmake%26nbsp%3Bbackups%26nbsp%3Bin%26nbsp%3BWeb%26nbsp%3BApps%26nbsp%3Buntil%26nbsp%3B-ExpiryTime%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24sasUrl%26nbsp%3B%3D%26nbsp%3BNew-AzStorageContainerSASToken%26nbsp%3B-Name%26nbsp%3B%24container%26nbsp%3B-Permission%26nbsp%3Brwdl%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-Context%26nbsp%3B%24storage.Context%26nbsp%3B-ExpiryTime%26nbsp%3B(Get-Date).AddYears(1)%26nbsp%3B-FullUri%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BCreate%26nbsp%3Ban%26nbsp%3BApp%26nbsp%3BService%26nbsp%3Bplan%26nbsp%3Bin%26nbsp%3BStandard%26nbsp%3Btier.%26nbsp%3BStandard%26nbsp%3Btier%26nbsp%3Ballows%26nbsp%3Bone%26nbsp%3Bbackup%26nbsp%3Bper%26nbsp%3Bday.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ENew-AzAppServicePlan%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B-Name%26nbsp%3B%24webappname%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-Location%26nbsp%3B%24location%26nbsp%3B-Tier%26nbsp%3BStandard%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BCreate%26nbsp%3Ba%26nbsp%3Bweb%26nbsp%3Bapp.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3ENew-AzWebApp%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B-Name%26nbsp%3B%24webappname%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-Location%26nbsp%3B%24location%26nbsp%3B-AppServicePlan%26nbsp%3B%24webappname%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BSchedule%26nbsp%3Ba%26nbsp%3Bbackup%26nbsp%3Bevery%26nbsp%3Bday%2C%26nbsp%3Bbeginning%26nbsp%3Bin%26nbsp%3Bone%26nbsp%3Bhour%2C%26nbsp%3Band%26nbsp%3Bretain%26nbsp%3Bfor%26nbsp%3B10%26nbsp%3Bdays%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EEdit-AzWebAppBackupConfiguration%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B-Name%26nbsp%3B%24webappname%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-StorageAccountUrl%26nbsp%3B%24sasUrl%26nbsp%3B-FrequencyInterval%26nbsp%3B1%26nbsp%3B-FrequencyUnit%26nbsp%3BDay%26nbsp%3B-KeepAtLeastOneBackup%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-StartTime%26nbsp%3B(Get-Date).AddHours(1)%26nbsp%3B-RetentionPeriodInDays%26nbsp%3B10%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3BList%26nbsp%3Bstatuses%26nbsp%3Bof%26nbsp%3Ball%26nbsp%3Bbackups%26nbsp%3Bthat%26nbsp%3Bare%26nbsp%3Bcomplete%26nbsp%3Bor%26nbsp%3Bcurrently%26nbsp%3Bexecuting.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3EGet-AzWebAppBackupList%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B-Name%26nbsp%3B%24webappname%3C%2FSTRONG%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%3CSPAN%3E%23%26nbsp%3B(OPTIONAL)%26nbsp%3BChange%26nbsp%3Bthe%26nbsp%3Bbackup%26nbsp%3Bschedule%26nbsp%3Bto%26nbsp%3Bevery%26nbsp%3B2%26nbsp%3Bdays%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24configuration%26nbsp%3B%3D%26nbsp%3BGet-AzWebAppBackupConfiguration%26nbsp%3B-ResourceGroupName%26nbsp%3BmyResourceGroup%26nbsp%3B%60%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E-Name%26nbsp%3B%24webappname%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24configuration.FrequencyInterval%26nbsp%3B%3D%26nbsp%3B2%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%3CSTRONG%3E%24configuration%26nbsp%3B%7C%26nbsp%3BEdit-AzWebAppBackupConfiguration%3C%2FSTRONG%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CP%3ENow%20you%20have%20used%20the%20PowerShell%20to%20deploy%20a%20WebApp%20in%20Azure%20and%20set%20up%20the%20backup!%20Congratulations!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Delete%20all%20resources%20(when%20you%20no%20longer%20need%20it)%3CBR%20%2F%3E%3CSTRONG%3ERemove-AzResourceGroup%20-Name%20myResourceGroup%20-Force%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EP.S.%26nbsp%3BAll%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1814841%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWeb%20Apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
MVP

 

Hi Azure friends,

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp
Clear-Host

 

#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose

 

#Log into Azure
Connect-AzAccount

 

#Select the correct subscription

Get-AzContext

Get-AzSubscription

Get-AzSubscription -SubscriptionName "your subscription name" | Select-AzSubscription

 

# Some Variables

$webappname="mywebapp$(Get-Random -Minimum 100000 -Maximum 999999)"
$storagename="$($webappname)storage"
$container="appbackup"
$location="West Europe"

# Create a resource group.
New-AzResourceGroup -Name myResourceGroup -Location $location

# Create a storage account.
$storage = New-AzStorageAccount -ResourceGroupName myResourceGroup `
-Name $storagename -SkuName Standard_LRS -Location $location

# Create a storage container.
New-AzStorageContainer -Name $container -Context $storage.Context

# Generates an SAS token for the storage container, valid for 1 year.
# NOTE: You can use the same SAS token to make backups in Web Apps until -ExpiryTime
$sasUrl = New-AzStorageContainerSASToken -Name $container -Permission rwdl `
-Context $storage.Context -ExpiryTime (Get-Date).AddYears(1) -FullUri

# Create an App Service plan in Standard tier. Standard tier allows one backup per day.
New-AzAppServicePlan -ResourceGroupName myResourceGroup -Name $webappname `
-Location $location -Tier Standard

# Create a web app.
New-AzWebApp -ResourceGroupName myResourceGroup -Name $webappname `
-Location $location -AppServicePlan $webappname

# Schedule a backup every day, beginning in one hour, and retain for 10 days
Edit-AzWebAppBackupConfiguration -ResourceGroupName myResourceGroup -Name $webappname `
-StorageAccountUrl $sasUrl -FrequencyInterval 1 -FrequencyUnit Day -KeepAtLeastOneBackup `
-StartTime (Get-Date).AddHours(1) -RetentionPeriodInDays 10

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

# (OPTIONAL) Change the backup schedule to every 2 days
$configuration = Get-AzWebAppBackupConfiguration -ResourceGroupName myResourceGroup `
-Name $webappname
$configuration.FrequencyInterval = 2
$configuration | Edit-AzWebAppBackupConfiguration
 

Now you have used the PowerShell to deploy a WebApp in Azure and set up the backup! Congratulations!

 

#Delete all resources (when you no longer need it)
Remove-AzResourceGroup -Name myResourceGroup -Force

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

 

0 Replies