Kopiowanie migawki dysku zarządzanego do tej samej lub innej subskrypcji przy użyciu interfejsu wiersza polecenia

Ten artykuł zawiera dwa skrypty. Pierwszy skrypt kopiuje migawkę dysku zarządzanego, który używał kluczy zarządzanych przez platformę do tej samej lub innej subskrypcji. Drugi skrypt kopiuje migawkę dysku zarządzanego, który używał kluczy zarządzanych przez klienta do tej samej lub innej subskrypcji. Te skrypty mogą być używane w następujących scenariuszach:

  • Migrowanie migawki w usłudze Premium Storage (Premium_LRS) do magazynu w warstwie Standardowa (Standard_LRS lub Standard_ZRS), aby zmniejszyć koszty.
  • Migrowanie migawki z magazynu lokalnie nadmiarowego (Premium_LRS, Standard_LRS) do magazynu strefowo nadmiarowego (Standard_ZRS), aby korzystać z większej niezawodności magazynu magazynu ZRS.
  • Przenieś migawkę do innej subskrypcji w tym samym regionie w celu dłuższego przechowywania.

Uwaga

Obie subskrypcje muszą znajdować się w tej samej dzierżawie

Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

Wymagania wstępne

Przykładowy skrypt

Uruchamianie usługi Azure Cloud Shell

Usługa Azure Cloud Shell to bezpłatna interaktywna powłoka, której możesz używać do wykonywania kroków opisanych w tym artykule. Udostępnia ona wstępnie zainstalowane i najczęściej używane narzędzia platformy Azure, które są skonfigurowane do użycia na koncie.

Aby otworzyć usługę Cloud Shell, wybierz pozycję Wypróbuj w prawym górnym rogu bloku kodu. Możesz również uruchomić usługę Cloud Shell w oddzielnej karcie przeglądarki, przechodząc do strony https://shell.azure.com.

Po otwarciu usługi Cloud Shell sprawdź, czy dla danego środowiska wybrano powłokę Bash . Kolejne sesje będą używać interfejsu wiersza polecenia platformy Azure w środowisku powłoki Bash, wybierz pozycję Kopiuj , aby skopiować bloki kodu, wkleić go do usługi Cloud Shell i nacisnąć klawisz Enter , aby go uruchomić.

Logowanie się do platformy Azure

Usługa Cloud Shell jest automatycznie uwierzytelniana na początkowym koncie zalogowanym. Użyj następującego skryptu, aby zalogować się przy użyciu innej subskrypcji, zastępując <Subscription ID> element identyfikatorem subskrypcji platformy Azure. Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

Aby uzyskać więcej informacji, zobacz set active subscription or log in interactively (Ustawianie aktywnej subskrypcji lub logowanie się interaktywnie)

Dyski z kluczami zarządzanymi przez platformę

#Provide the subscription Id of the subscription where snapshot exists
sourceSubscriptionId="<subscriptionId>"

#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. Please use Standard_ZRS in regions where zone redundant storage (ZRS) is available, otherwise use Standard_LRS
#Please check out the availability of ZRS here: https://docs.microsoft.com/azure/storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS

Dyski z kluczami zarządzanymi przez klienta

#Provide the subscription Id of the subscription where snapshot exists
sourceSubscriptionId="<subscriptionId>"

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

#Provide the name of the target disk encryption set
diskEncryptionSetName=myName

#Provide the target disk encryption set resource group
diskEncryptionResourceGroup=myGroup

#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

#Get the disk encryption set ID
diskEncryptionSetId=$(az disk-encryption-set show --name $diskEncryptionSetName --resource-group $diskEncryptionResourceGroup)

#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. Please use Standard_ZRS in regions where zone redundant storage (ZRS) is available, otherwise use Standard_LRS
#Please check out the availability of ZRS here: https://docs.microsoft.com/en-us/azure/storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
#To change the region, use the --location parameter
az snapshot create -g $targetResourceGroupName -n $snapshotName --source $snapshotId --disk-encryption-set $diskEncryptionSetID --sku Standard_LRS --encryption-type EncryptionAtRestWithCustomerKey

Czyszczenie zasobów

Uruchom następujące polecenie, aby usunąć grupę zasobów, maszynę wirtualną i wszystkie powiązane zasoby.

az group delete --name mySourceResourceGroupName

Przykładowa dokumentacja

Ten skrypt używa następujących poleceń do utworzenia migawki w subskrypcji docelowej przy użyciu Id migawki źródłowej. Każde polecenie w tabeli stanowi link do dokumentacji polecenia.

Polecenie Uwagi
az snapshot show Pobiera wszystkie właściwości migawki przy użyciu nazwy i właściwości grupy zasobów migawki. Właściwość Id służy do kopiowania migawki do innej subskrypcji.
az snapshot create Kopiuje migawkę, tworząc migawkę w innej subskrypcji przy użyciu Id nazwy i migawki nadrzędnej.

Następne kroki

Tworzenie maszyny wirtualnej na podstawie migawki

Aby uzyskać więcej informacji na temat interfejsu wiersza polecenia platformy Azure, zobacz dokumentację interfejsu wiersza polecenia platformy Azure.

Więcej przykładów skryptów interfejsu wiersza polecenia maszyny wirtualnej i dysków zarządzanych można znaleźć w dokumentacji maszyny wirtualnej z systemem Linux platformy Azure.