Use PowerShell to restore an Azure SQL Managed Instance database to another geo-region
Applies to: Azure SQL Managed Instance
This PowerShell script example restores an Azure SQL Managed Instance database from a remote geo-region (geo-restore).
If you don't have an Azure trail subscription, create a trial subscription before you begin.
Note
This article uses the Azure Az PowerShell module, which is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
This tutorial requires Azure PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. You need to run Connect-AzAccount -Environment AzureChinaCloud
to create a connection with Azure.
Sample script
# Connect-AzAccount -Environment AzureChinaCloud
# The SubscriptionId in which to create these objects
$SubscriptionId = '<put subscription_id here>'
# Set the information for your managed instance
$SourceResourceGroupName = "myResourceGroup-$(Get-Random)"
$SourceInstanceName = "myManagedInstance-$(Get-Random)"
$SourceDatabaseName = "myInstanceDatabase-$(Get-Random)"
# Set the information for your destination managed instance
$TargetResourceGroupName = "myTargetResourceGroup-$(Get-Random)"
$TargetInstanceName = "myTargetManagedInstance-$(Get-Random)"
$TargetDatabaseName = "myTargetInstanceDatabase-$(Get-Random)"
Connect-AzAccount -Environment AzureChinaCloud
Set-AzContext -SubscriptionId $SubscriptionId
$backup = Get-AzSqlInstanceDatabaseGeoBackup `
-ResourceGroupName $SourceResourceGroupName `
-InstanceName $SourceInstanceName `
-Name $SourceDatabaseName
$backup | Restore-AzSqlInstanceDatabase -FromGeoBackup `
-TargetInstanceDatabaseName $TargetDatabaseName `
-TargetInstanceName $TargetInstanceName `
-TargetResourceGroupName $TargetResourceGroupName
Clean up deployment
Use the following command to remove the resource group and all resources associated with it.
Remove-AzResourceGroup -ResourceGroupName $TargetResourceGroupName
Script explanation
This script uses the following commands. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
New-AzResourceGroup | Creates a resource group in which all resources are stored. |
Get-AzSqlInstanceDatabaseGeoBackup | Gets one or more geo-backups from a database within an Azure SQL Managed Instance. |
Restore-AzSqlInstanceDatabase | Creates a database on SQL Managed Instance from geo-backup. |
Remove-AzResourceGroup | Deletes a resource group, including all nested resources. |
Next steps
For more information about PowerShell, see Azure PowerShell documentation.
Additional PowerShell script samples for Azure SQL Database can be found in Azure SQL Database PowerShell scripts.