如何使用 PowerShell 来备份和还原 Azure Database for MySQL 服务器How to back up and restore an Azure Database for MySQL server using PowerShell

Azure Database for MySQL 服务器定期进行备份以便启用还原功能。Azure Database for MySQL servers is backed up periodically to enable restore features. 通过此功能,用户可将服务器及其所有数据库还原到新服务器上的某个较早时间点。Using this feature you may restore the server and all its databases to an earlier point-in-time, on a new server.

先决条件Prerequisites

若要完成本操作指南,需要:To complete this how-to guide, you need:

重要

尽管 Az.MySql PowerShell 模块为预览版,但必须使用以下命令从 Az PowerShell 模块单独安装它:Install-Module -Name Az.MySql -AllowPrereleaseWhile the Az.MySql PowerShell module is in preview, you must install it separately from the Az PowerShell module using the following command: Install-Module -Name Az.MySql -AllowPrerelease. Az.MySql PowerShell 模块正式版推出后,它会包含在将来的 Az PowerShell 模块发行版中。Once the Az.MySql PowerShell module is generally available, it becomes part of future Az PowerShell module releases.

使用 Connect-AzAccount cmdlet 连接到 Azure 帐户。Connect to your Azure account using the Connect-AzAccount cmdlet.

设置备份配置Set backup configuration

创建服务器时,可以选择将服务器配置为创建本地冗余备份还是创建异地冗余备份。At server creation, you make the choice between configuring your server for either locally redundant or geographically redundant backups.

备注

创建服务器后,无法在异地冗余或本地冗余之间更改服务器冗余类型。After a server is created, the kind of redundancy it has, geographically redundant vs locally redundant, can't be changed.

通过 New-AzMySqlServer 命令创建服务器时,GeoRedundantBackup 参数描述备份冗余选项。While creating a server via the New-AzMySqlServer command, the GeoRedundantBackup parameter decides your backup redundancy option. 如果为“启用”,则创建异地冗余备份。If Enabled, geo redundant backups are taken. 或者如果为“禁用”,则创建本地冗余备份。Or if Disabled, locally redundant backups are taken.

备份保留期由参数 BackupRetentionDay 设置。The backup retention period is set by the BackupRetentionDay parameter.

有关在服务器创建过程中设置这些值的详细信息,请参阅使用 PowerShell 创建 Azure Database for MySQL 服务器For more information about setting these values during server creation, see Create an Azure Database for MySQL server using PowerShell.

可以如下所述更改服务器的备份保留期:The backup retention period of a server can be changed as follows:

Update-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup -BackupRetentionDay 10

前面的示例将 mydemoserver 的备份保留期更改为 10 天。The preceding example changes the backup retention period of mydemoserver to 10 days.

备份保留期控制可以往回检索多长时间的时间点还原,因为它基于可用备份。The backup retention period governs how far back a point-in-time restore can be retrieved, since it's based on available backups. 下一部分中进一步介绍了时间点还原。Point-in-time restore is described further in the next section.

服务器时间点还原Server point-in-time restore

可以将服务器还原到以前的某个时间点。You can restore the server to a previous point-in-time. 将还原的数据复制到新服务器,现有服务器保持不变。The restored data is copied to a new server, and the existing server is left unchanged. 例如,如果意外删除了某个表,可以还原到删除时的时间点。For example, if a table is accidentally dropped, you can restore to the time just the drop occurred. 然后可以从服务器的已还原副本中检索缺少的表和数据。Then, you can retrieve the missing table and data from the restored copy of the server.

若要还原服务器,请使用 Restore-AzMySqlServer PowerShell cmdlet。To restore the server, use the Restore-AzMySqlServer PowerShell cmdlet.

运行还原命令Run the restore command

若要还原服务器,请在 PowerShell 中运行以下示例。To restore the server, run the following example from PowerShell.

$restorePointInTime = (Get-Date).AddMinutes(-10)
Get-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  Restore-AzMySqlServer -Name mydemoserver-restored -ResourceGroupName myresourcegroup -RestorePointInTime $restorePointInTime -UsePointInTimeRestore

Restore-AzMySqlServer cmdlet 的 PointInTimeRestore 参数集需要以下参数:The PointInTimeRestore parameter set of the Restore-AzMySqlServer cmdlet requires the following parameters:

设置Setting 建议的值Suggested value 说明Description  
ResourceGroupNameResourceGroupName  myresourcegroupmyresourcegroup  源服务器所在的资源组。The resource group where the source server exists. 
名称Name mydemoserver-restoredmydemoserver-restored 通过还原命令创建的新服务器的名称。The name of the new server that is created by the restore command.
RestorePointInTimeRestorePointInTime 2020-03-13T13:59:00Z2020-03-13T13:59:00Z 选择要还原的时间点。Select a point in time to restore. 此日期和时间必须在源服务器的备份保留期限内。This date and time must be within the source server's backup retention period. 使用 ISO8601 日期和时间格式。Use the ISO8601 date and time format. 例如,可以使用自己的本地时区,如 2020-03-13T05:59:00-08:00。For example, you can use your own local time zone, such as 2020-03-13T05:59:00-08:00. 也可以使用 UTC Zulu 格式,如 2018-03-13T13:59:00Z。You can also use the UTC Zulu format, for example, 2018-03-13T13:59:00Z.
UsePointInTimeRestoreUsePointInTimeRestore <SwitchParameter> 使用时间点模式还原。Use point-in-time mode to restore.

将服务器还原到以前的某个时间点时,会创建新服务器。When you restore a server to an earlier point-in-time, a new server is created. 原始服务器及其从指定时间点开始创建的数据库会复制到新服务器。The original server and its databases from the specified point-in-time are copied to the new server.

还原的服务器的位置值和定价层值与原始服务器保持相同。The location and pricing tier values for the restored server remain the same as the original server.

还原过程完成后,找到新服务器,验证数据是否已按预期还原。After the restore process finishes, locate the new server and verify that the data is restored as expected. 新服务器具有相同的服务器管理员登录名和密码,该登录名和密码在开始还原时对现有服务器有效。The new server has the same server admin login name and password that was valid for the existing server at the time the restore was started. 可以从新服务器的“概述”页更改密码。The password can be changed from the new server's Overview page.

还原期间创建的新服务器没有原始服务器上存在的 VNet 服务终结点。The new server created during a restore does not have the VNet service endpoints that existed on the original server. 必须单独为新服务器设置这些规则。These rules must be set up separately for the new server. 将从原始服务器还原防火墙规则。Firewall rules from the original server are restored.

异地还原Geo restore

如果为服务器配置了异地冗余备份,则可以从该现有服务器的备份创建新服务器。If you configured your server for geographically redundant backups, a new server can be created from the backup of the existing server. 可以在 Azure Database for MySQL 可用的任何区域中创建此新服务器。This new server can be created in any region that Azure Database for MySQL is available.

若要使用异地冗余备份创建服务器,请使用带有 UseGeoRestore 参数的 Restore-AzMySqlServer 命令。To create a server using a geo redundant backup, use the Restore-AzMySqlServer command with the UseGeoRestore parameter.

备注

首次创建服务器时,该服务器可能不会立即可用于异地还原。When a server is first created it may not be immediately available for geo restore. 填充必需的元数据可能需要几个小时。It may take a few hours for the necessary metadata to be populated.

若要异地还原服务器,请在 PowerShell 中运行以下示例:To geo restore the server, run the following example from PowerShell:

Get-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  Restore-AzMySqlServer -Name mydemoserver-georestored -ResourceGroupName myresourcegroup -Location chinaeast2 -Sku GP_Gen5_8 -UseGeoRestore

此示例在中国东部 2 创建一个名为 mydemoserver-georestored 且将属于 myresourcegroup 的新服务器 。This example creates a new server called mydemoserver-georestored in the China East 2 region that belongs to myresourcegroup. 它是第 5 代常规用途服务器,具有 8 个 vCore。It is a General Purpose, Gen 5 server with 8 vCores. 该服务器是基于也在资源组 myresourcegroup 中的 mydemoserver 的异地冗余备份创建的 。The server is created from the geo-redundant backup of mydemoserver, also in the resource group myresourcegroup.

若要在与现有服务器不同的资源组创建新服务器,请使用 ResourceGroupName 参数指定新资源组的名称,如以下示例所示:To create the new server in a different resource group from the existing server, specify the new resource group name using the ResourceGroupName parameter as shown in the following example:

Get-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  Restore-AzMySqlServer -Name mydemoserver-georestored -ResourceGroupName newresourcegroup -Location chinaeast2 -Sku GP_Gen5_8 -UseGeoRestore

Restore-AzMySqlServer cmdlet 的 GeoRestore 参数集需要以下参数:The GeoRestore parameter set of the Restore-AzMySqlServer cmdlet requires the following parameters:

设置Setting 建议的值Suggested value 说明Description  
ResourceGroupNameResourceGroupName myresourcegroupmyresourcegroup 新服务器属于的资源组的名称。The name of the resource group the new server belongs to.
名称Name mydemoserver-georestoredmydemoserver-georestored 新服务器的名称。The name of the new server.
位置Location chinaeast2chinaeast2 新服务器的位置。The location of the new server.
UseGeoRestoreUseGeoRestore <SwitchParameter> 使用异地模式还原。Use geo mode to restore.

通过异地还原创建新服务器时,它将继承与源服务器相同的存储大小和定价层,除非指定 Sku 参数。When creating a new server using geo restore, it inherits the same storage size and pricing tier as the source server unless the Sku parameter is specified.

还原过程完成后,找到新服务器,验证数据是否已按预期还原。After the restore process finishes, locate the new server and verify that the data is restored as expected. 新服务器具有相同的服务器管理员登录名和密码,该登录名和密码在开始还原时对现有服务器有效。The new server has the same server admin login name and password that was valid for the existing server at the time the restore was started. 可以从新服务器的“概述”页更改密码。The password can be changed from the new server's Overview page.

在还原期间创建的新服务器没有原始服务器上存在的 VNet 服务终结点。The new server created during a restore does not have the VNet service endpoints that existed on the original server. 必须单独为新服务器设置这些规则。These rules must be set up separately for this new server. 将从原始服务器还原防火墙规则。Firewall rules from the original server are restored.

后续步骤Next steps