Restoring Azure App Service backup to different site - azure

How can I use Restore-AzureRmWebAppBackup to restore an app service to a different existing site? This can usually be done in the portal, but for some reason the destination app service is not listed, even though it's in the same resource group.

How can I use Restore-AzureRmWebAppBackup to restore an app service to a different existing site?
Here is a sample code of restore web app from PowerShell.
$resourceGroupName = "target web app resource group name"
$appName = "target web app name"
$blobName = "backup blob name"
$storageAccountURL = "SAS URL of your blob container"
Restore-AzureRmWebAppBackup -ResourceGroupName $resourceGroupName -Name $appName -StorageAccountUrl $storageAccountURL -BlobName $blobName -Overwrite
We can find the blob name from the restore page after you backup your web app.
For more information, link below is for your reference.
Use PowerShell to back up and restore App Service apps

Following is the solution using Bash CLI:
prerequisite: A storage account with a container and the container URL for the same. The container URL contains the access the key (SAS). More information about the container URL can be found here: https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
You can also use the following command to generate the SAS token:
az storage container generate-sas --name <container-name> --expiry <sas-expiry> --permissions <permissions> --account-name <account-name> --account-key <account-key>
To clone the app, use the following commands:
backupname=appname_`date "+%Y%m%d%H%M%S"`
az webapp config backup create --resource-group $OriginresourceGroupName --webapp-name $originAppName --backup-name $backupname --slot $OriginSlotName --container-url "$ContainerURL"
az webapp config backup restore --backup-name $backupname --container-url $ContainerURL --resource-group $TargetresourceGroupName --webapp-name $TargetAppname --overwrite --ignore-hostname-conflict

Use below cmdlets to restore last successful backup to slot or app
$resourceGroupName = "<<RG NAME>>"
$primarywebappname = "<<APP NAME>>"
$slotname = "<<SLOT NAME>>"
# Get the last Sucessfull BackupID of the backup you want to restore
$backupID=Get-AzWebAppBackupList -ResourceGroupName $resourceGroupName -Name `
$primarywebappname|where BackupStatus -EQ "Succeeded" |select -Last 1 |select BackupId
# Get the backup object that you want to restore by specifying the BackupID
$backup = (Get-AzWebAppBackupList -ResourceGroupName $resourceGroupName -Name
$primarywebappname | where {$PSItem.BackupId -eq $backupID.BackupId})
# Restore the app by overwriting it with the backup data in the slot
$backup |Restore-AzWebAppBackup -Slot $slotname -IgnoreConflictingHostNames -Overwrite
# Restore the app by overwriting into the exsisting App
$backup |Restore-AzWebAppBackup -Overwrite

Related

PS Script to manage Azure Batch Application Version Retention

We have an Azure Devops release that builds our repo, packages it, and deploys it to our Azure Batch service with the following PowerShell script:
New-AzureRmBatchApplicationPackage -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -ApplicationVersion "$(Build.BuildNumber)-$(Release.ReleaseId)" -Format zip -FilePath "$(System.DefaultWorkingDirectory)/_artifact/artifact/bin/theapplication.zip"
Set-AzureRmBatchApplication -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)" -ApplicationId "$(ApplicationId)" -DefaultVersion "$(Build.BuildNumber)-$(Release.ReleaseId)"
$Context = Get-AzureRmBatchAccount -AccountName "$(BatchAccountName)" -ResourceGroupName "$(ResourceGroupName)"
Get-AzureBatchComputeNode -PoolId "$(PoolId)" -BatchContext $Context | Restart-AzureBatchComputeNode -BatchContext $Context
The problem is, this doesn't seem to "clean up" old versions of the application package, and I don't see a way in the API documentation to do so. So every few weeks I get this sort of error message:
The maximum allowed number of application packages have already been
added for the specified application.
How can I change this script to deploy the application and clean up old versions so I don't have periodic failures in my deployment?
Depending on if you want to keep any history and how you version you should be able to call Get-AzBatchApplicationPackage to list application packages associated with an Application (https://learn.microsoft.com/en-us/powershell/module/az.batch/get-azbatchapplicationpackage?view=azps-1.7.0). Note that ApplicationVersion is actually optional for this call.
Then Remove-AzBatchApplicationPackage (https://learn.microsoft.com/en-us/powershell/module/Az.Batch/Remove-AzBatchApplicationPackage?view=azps-1.7.0) on any application packages you do not want to keep around, whether that is all application packages other than the one you added or some function on the version is up to you.
We use Azure CLI (in PowerShell), so the syntax is a bit different than yours. This is our script to achieve what you are asking for (and more). The key is to use the argument --query on some of the calls, to be able to count number of application versions, and get the oldest version:
$Environment = "dev"
$ResourceGroup="rg-name-$Environment"
$BatchAccount="batchname$Environment"
$BatchApplicationName="MyApplication"
$BatchApplicationVersion="1.2.3.4"
$SetAsDefaultVersion=$true
$MaxNumOfAppVersions=39
Write-Host "Uploading new version of application to batch service..."
$filename = "folder/file.zip"
$numOfVersions = az batch application package list --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --query "length([].{name:name})"
Write-Host "There are currently $numOfVersions versions of the Application in the Batch Account"
if ($numOfVersions -gt $MaxNumOfAppVersions) {
# Do not automatically delete old version in the prod environment:
if (($Environment.ToLower() -eq "prod")) {
throw "The Batch Application $BatchApplicationName contains too many versions. Delete the oldest, after you have made sure that it is not in use in production."
}
$oldestAppVersion = az batch application package list --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --query "sort_by([].{name:name, lastActivationTime:lastActivationTime}, &lastActivationTime)[0].{name:name}" -o tsv
Write-Host "Deleting the oldest version (by lastActivationTime) of the Application, $oldestAppVersion, to avoid the max limit"
az batch application package delete --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --version-name $oldestAppVersion --yes
}
Write-Host "Uploading $filename..."
az batch application package create --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --package-file "$filename" --version-name "$BatchApplicationVersion"
if ($SetAsDefaultVersion) {
Write-Host "Setting this new version $BatchApplicationVersion as the default version..."
az batch application set --resource-group "$ResourceGroup" --name "$BatchAccount" --application-name "$BatchApplicationName" --default-version "$BatchApplicationVersion"
}
Write-Host "All done!"

how to read service configuration (.cscfg) of a cloud service created on RM subscription

I have a cloud service (classic) created on RM subscription. I need to read its service config (.cscfg), update the config and redrploy it back to the cloud service.
I did it using get-azuredeploy command on Classic, but this command doesnt work on RM subscription.
does anyone know how to get service config from RM subscription?
Try the command below, the $slot.Properties.configuration should be that.
$slot = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.ClassicCompute/domainNames/slots -ResourceName "xxxxx" -ApiVersion 2016-04-01
$slot.Properties.configuration

How to use deployment slots of app services with Github Source code

I am deploying my application from Github and I have three different deployment slot(Dev/Staging/prod) and would like to deploy the code only to Dev and swap the deployed code to the rest of the stages.
Just to not I don't have any pipeline tool yet so would like to understand with command line or GUI option as it is proof of concept.
You could use Azure CLI or powershell to access it.
Azure CLI:
# Replace the following URL with a public GitHub repo URL
gitrepo=https://github.com/Azure-Samples/php-docs-hello-world
webappname=mywebapp$RANDOM
# Deploy code from a public GitHub repository.
az webapp deployment source config --name $webappname --resource-group myResourceGroup \
--repo-url $gitrepo --branch master --manual-integration
Azure Powershell:
# Replace the following URL with a public GitHub repo URL
$gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"
# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = #{
repoUrl = "$gitrepo";
branch = "master";
isManualIntegration = "true";
}
Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName myResourceGroup -ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webappname/web -ApiVersion 2015-08-01 -Force
After publishing it to azure, you could swap the slot via the portal, also the azure Powershell and CLI.

How do I create an Image of a VM using Azure Resource Manager

I've been using the classic Azure Portal for a while now, and I know how to create a VM, customize it, then capture it as an Image and use that image to create more VMs.
Now I'm trying to use the new Azure Portal. I created the VM and customized it, now I want to capture an image so I can make more VMs exactly the same way. The problem is the new web portal doesn't capture option.
As far as I know, you can do it via Powershell:
Login-AzureRmAccount
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionId "<subscriptionID>"
Stop-AzureRmVM -ResourceGroupName <resourceGroup> -Name <vmName>
Set-AzureRmVm -ResourceGroupName <resourceGroup> -Name <vmName> -Generalized
Save-AzureRmVMImage -ResourceGroupName <resourceGroupName> -Name <vmName> `
-DestinationContainerName <destinationContainerName> -VHDNamePrefix <templateNamePrefix> `
-Path <C:\local\Filepath\Filename.json>
For more details visit: https://learn.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-capture-image

azure cannot create vm from vhd image

I'm currently running into difficulty in creating an Azure VM from a custom VM image. I am following the guide from Azure from here: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/
I've used Waagent and deprovisioned the machine as instructed, and deallocated, generalized, and captured my machine image (I have made some modifications to the core Ubuntu 16.04LTS image available from Azure software wise). I have successfully created the template.json file (Can provide it if needed). I then completed all the tasks below in the powershell script as outlined in the article, just extracting the parameters to variables to make things a bit easier.
## Global
$rgName = "testrg"
$location = "eastus"
## Storage
$storageName = "teststore"
$storageType = "Standard_GRS"
## Network
$nicname = "testnic"
$subnetName = "subnet1"
$vnetName = "testnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$ipName = "TestIP"
## Compute
$vmName = "testvm"
$computerName = "testcomputer"
$vmSize = "Standard_D1_v2"
$osDiskName = $vmName + "osDisk"
#template
$fileTemplate = "C:\AzureTemplate\template.json"
azure group create $rgName -l $location
azure network vnet create $rgName $vnetName -l $location
azure network vnet subnet create --resource-group $rgName --vnet-name $vnetName --name $subnetName --address-prefix $vnetSubnetAddressPrefix
azure network public-ip create $rgName $ipName -l $location
azure network nic create $rgName $nicName -k $subnetName -m $vnetName -p $ipName -l $location
azure network nic show $rgName $nicname
azure group deployment create $rgName $computerName -f $fileTemplate
I am able to successfully run all the commands to create the resource group and the network components, however, when I try to run the deployment command at the bottom of the powershell script, I get the following and it just hangs here indefinitely. Am I using the right approach to create a VM from a custom image? Or is that Azure guide outdated?
azure group deployment create $rgName $computerName -f $fileTemplate
[32minfo[39m: Executing command [1mgroup deployment create[22m
[32minfo[39m: Supply values for the following parameters
EDIT: Link to image showing the issue: http://imgur.com/a/Fgh8K
I believe your understanding is not complete. If you see at the last line it says Supply values for the following parameters
You need to pass the values for VM name, the admin user name and password, and the Id of the NIC you created previously. My be you should re-read the documentation. Here is the screenshot for your reference from https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/#deploy-a-new-vm-from-the-captured-image -

Resources