使用 CLI 将快照导出/复制到不同区域中的存储帐户

此脚本将托管快照导出到不同区域中的存储帐户。 它首先会生成快照的 SAS URI,然后使用它将快照复制到不同区域中的存储帐户。 使用此脚本将托管磁盘的备份保留在灾难恢复的不同区域。

若要运行此示例,请安装最新版本的 Azure CLI。 若要开始,请运行 az login 以创建与 Azure 的连接。

适用于 Azure CLI 的示例是针对 bash shell 编写的。 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

示例脚本

# Verified per Raman Kumar as of 2/23/2022

# <FullScript>
#Provide the subscription Id where snapshot is created
subscriptionId="<subscriptionId>"

#Provide the name of your resource group where snapshot is created
resourceGroupName=myResourceGroupName

#Provide the snapshot name 
snapshotName=mySnapshotName

#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
sasExpiryDuration=3600

#Provide storage account name where you want to copy the snapshot. 
storageAccountName=mystorageaccountname

#Name of the storage container where the downloaded snapshot will be stored
storageContainerName=mystoragecontainername

#Provide the key of the storage account where you want to copy snapshot. 
storageAccountKey=mystorageaccountkey

#Provide the name of the VHD file to which snapshot will be copied.
destinationVHDFileName=myvhdfilename

az account set --subscription $subscriptionId

sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)

az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas
# </FullScript>

脚本说明

此脚本使用以下命令为托管快照生成 SAS URI,并使用 SAS URI 将快照复制到存储帐户。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az snapshot grant-access 生成只读 SAS,用于将基础 VHD 文件复制到存储帐户或将其下载到本地
az storage blob copy start 将 blob 从一个存储帐户异步复制到另一个存储帐户

后续步骤

从 VHD 创建托管磁盘

从托管磁盘创建虚拟机

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。