Azure API Management – Using PowerShell for backup and restore


Automation is the new Black, as "said" by Mark Russinovich during AzureCon.

One of the ways to automate in Azure is by using PowerShell tools for Azure, next to other options. I've been working with Azure API Management for some customers lately, and one interesting short post caught my attention: Full set of Windows PowerShell cmdlets for Azure API Management API. This means that now a number of functions to manage API Management have been made available as PowerShell cmdlets, making it easy to use in automation tasks.
Backup and restore is one of the features I’ve been using. While its primary use is for having a backup of your current API Management account, for example making sure this is done very regularly as a way to recover in the event of failure. Another use for the backup/restore feature is when going from development to a staging environment.
Official documentation for doing backup/restore shows us how to do this using the Azure Resource Manager based API Management REST API. But it also requires creating an Azure AD application, setting the right permissions and getting a token for authenticating requests to Azure Resource Manager. Stuart Leeks has an interesting blog post with an alternative non-supported way to use PowerShell, and now with the official cmdlets this is exactly what we can do.

So why not give that a try?

Creating a backup requires the following information: your API Management account, the resource group name and a storage account to which to save the backup to as well as the container in which it will go.

Prepare for the backup

1. Make sure you have configured PowerShell and connected to your subscription.

2. Connect to the storage account and make sure to create the container to which you will store your backup to. Note the name. You can do this through the Azure Management Portal, or using a Storage explorer tool. Also take note of the type of Storage that you are using. There are currently two types, one used in the current Management Portal, and the new mode using Azure Resource Manager.

3. Identify the name of the Resource Group in which your API Management account is created: use https://resources.azure.com/ to navigate to the subscription. The resource group name has automatically been created, and is in the form of 'Api-Default-{service-region}'.
Run backup

Create a Storage Context.

 if ($ARMStorage)
 {
  Switch-AzureMode AzureResourceManager
     $StorageAccountKey = (Get-AzureStorageAccountKey -ResourceGroupName $StorageAccountResourceGroupName -Name $StorageAccountName).Key1
    }
    else
 {
  Switch-AzureMode AzureServiceManagement
     $StorageAccountKey = (Get-AzureStorageKey -StorageAccountName $StorageAccountName).Primary
    }
   

$StorageAccountContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

Run backup

Switch-AzureMode AzureResourceManager

Backup-AzureApiManagement -Name "myAPIMaccount" -ResourceGroupName "Api-Default-West-Europe" -StorageContext $StorageAccountContext `
                       -TargetContainerName "mycontainer" `
                       -TargetBlobName "myapimbackupddmmyy"

Typically you’d create variables for the above values, but for testing we can hard-code them.
For automating the backup you can use something like Azure Automation, currently you’ll need some manual work to support Azure Resource Manager though.

Run restore

To run a restore, simply run the following command.

Restore-AzureApiManagement -Name "myAPIMaccount" -ResourceGroupName "Api-Default-West-Europe" `
                        -StorageContext $StorageAccountContext `
                        -SourceContainerName "mycontainer" `
                        -SourceBlobName "myapimbackupddmmyy" -Verbose

Do read through the notes on the official documentation to understand some of the constraints, including the fact that a restore is only guaranteed maximum 7 days after backup.

 

 

 

 

 

 

Comments (0)

Skip to main content