使用 CLI 将托管磁盘的快照复制到相同或不同的订阅Copy snapshot of a managed disk to same or different subscription with CLI

此脚本会将托管磁盘的快照复制到相同或不同的订阅。This script copies a snapshot of a managed disk to same or different subscription. 将此脚本用于以下方案:Use this script for the following scenarios:

  1. 将高级存储 (Premium_LRS) 中的快照迁移到标准存储 (Standard_LRS) 以降低成本。Migrate a snapshot in Premium storage (Premium_LRS) to Standard storage (Standard_LRS) to reduce your cost.
  1. 将快照移到同一区域中的不同订阅,以延长保留时间。Move a snapshot to different subscription in the same region for longer retention.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

示例脚本Sample script

#Provide the subscription Id of the subscription where snapshot exists
sourceSubscriptionId=dd80b94e-0463-4a65-8d04-c94f403879dc

#Provide the name of your resource group where snapshot exists
sourceResourceGroupName=mySourceResourceGroupName

#Provide the name of the snapshot
snapshotName=mySnapshotName

#Set the context to the subscription Id where snapshot exists
az account set --subscription $sourceSubscriptionId

#Get the snapshot Id 
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)

#If snapshotId is blank then it means that snapshot does not exist.
echo 'source snapshot Id is: ' $snapshotId

#Provide the subscription Id of the subscription where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
targetSubscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0

#Name of the resource group where snapshot will be copied to
targetResourceGroupName=mytargetResourceGroupName

#Set the context to the subscription Id where snapshot will be copied to
#If snapshot is copied to the same subscription then you can skip this step
az account set --subscription $targetSubscriptionId

#Copy snapshot to different subscription using the snapshot Id
#We recommend you to store your snapshots in Standard storage to reduce cost. 

az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS

脚本说明Script explanation

此脚本使用以下命令,通过源快照的 ID 在目标订阅中创建快照。This script uses following commands to create a snapshot in the target subscription using the Id of the source snapshot. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az snapshot showaz snapshot show 使用快照的名称和资源组属性获取该快照的所有属性。Gets all the properties of a snapshot using the name and resource group properties of the snapshot. 使用 ID 属性将快照复制到其他订阅。Id property is used to copy the snapshot to different subscription.
az snapshot createaz snapshot create 通过使用父快照的 ID 和名称在不同订阅中创建快照来复制快照。Copies a snapshot by creating a snapshot in different subscription using the Id and name of the parent snapshot.

后续步骤Next steps

从快照创建虚拟机Create a virtual machine from a snapshot

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

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.