使用 PowerShell 将 Azure SQL 托管实例数据库还原到另一个地理区域Use PowerShell to restore an Azure SQL Managed Instance database to another geo-region

适用于:是Azure SQL 托管实例 APPLIES TO: yesAzure SQL Managed Instance

此 PowerShell 脚本示例从远程地理区域还原 Azure SQL 托管实例数据库(异地还原)。This PowerShell script example restores an Azure SQL Managed Instance database from a remote geo-region (geo-restore).

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本教程需要 Azure PowerShell 1.4.0 或更高版本。This tutorial requires Azure PowerShell 1.4.0 or later. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 需要运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。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.

CommandCommand 说明Notes
New-AzResourceGroupNew-AzResourceGroup 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
Get-AzSqlInstanceDatabaseGeoBackupGet-AzSqlInstanceDatabaseGeoBackup 创建 SQL 托管实例数据库的异地冗余备份。Creates a geo-redundant backup of a SQL Managed Instance database.
Restore-AzSqlInstanceDatabaseRestore-AzSqlInstanceDatabase 通过异地备份在 SQL 托管实例上创建数据库。Creates a database on SQL Managed Instance from geo-backup.
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组,包括所有嵌套的资源。Deletes a resource group, including all nested resources.

后续步骤Next steps

有关 PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information about PowerShell, see Azure PowerShell documentation.

可以在 Azure SQL 数据库 PowerShell 脚本中找到更多 Azure SQL 数据库 PowerShell 脚本示例。Additional PowerShell script samples for Azure SQL Database can be found in Azure SQL Database PowerShell scripts.