Menyalin snapshot dari disk yang dikelola ke langganan yang sama atau berbeda dengan CLI

Artikel ini berisi dua skrip. Skrip pertama menyalin rekam jepret disk terkelola yang menggunakan kunci yang dikelola platform ke langganan yang sama atau berbeda. Skrip kedua menyalin rekam jepret disk terkelola yang menggunakan kunci yang dikelola pelanggan ke langganan yang sama atau berbeda. Skrip ini dapat digunakan untuk skenario berikut:

  • Migrasikan snapshot di penyimpanan Premium (Premium_LRS) ke penyimpanan Standar (Standard_LRS atau Standard_ZRS) untuk mengurangi biaya Anda.
  • Migrasikan snapshot dari penyimpanan redundan lokal (Premium_LRS, Standard_LRS) ke penyimpanan redundan zona (Standard_ZRS) untuk memanfaatkan keandalan penyimpanan ZRS yang lebih tinggi.
  • Pindahkan snapshot ke langganan yang berbeda di wilayah yang sama untuk retensi yang lebih lama.

Catatan

Kedua langganan harus berada di bawah penyewa yang sama

Jika Anda tidak memiliki Langganan Azure, buat Akun gratis Azure sebelum memulai.

Prasyarat

Sampel skrip

Meluncurkan Azure Cloud Shell

Azure Cloud Shell adalah shell interaktif gratis yang dapat Anda gunakan untuk menjalankan langkah-langkah dalam artikel ini. Shell ini memiliki alat Azure umum yang telah dipasang sebelumnya dan dikonfigurasi untuk digunakan dengan akun Anda.

Untuk membuka Cloud Shell, cukup pilih Cobalah dari sudut kanan atas blok kode. Anda juga dapat meluncurkan Cloud Shell di tab browser terpisah dengan membuka https://shell.azure.com.

Saat Cloud Shell terbuka, verifikasi bahwa Bash dipilih untuk lingkungan Anda. Sesi berikutnya akan menggunakan Azure CLI dalam lingkungan Bash, Pilih Salin untuk menyalin blok kode, tempelkan ke Cloud Shell, lalu tekan Enter untuk menjalankannya.

Masuk ke Azure

Cloud Shell diautentikasi secara otomatis dengan akun awal yang digunakan untuk masuk. Gunakan skrip berikut untuk masuk menggunakan langganan yang berbeda, menggantikan <Subscription ID> dengan ID Langganan Azure Anda. Jika Anda tidak memiliki Langganan Azure, buat Akun gratis Azure sebelum memulai.

subscription="<subscriptionId>" # add subscription here

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

Untuk informasi selengkapnya, lihat mengatur langganan aktif atau masuk secara interaktif

Disk dengan kunci yang dikelola 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/en-us/azure/storage/common/storage-redundancy-zrs#support-coverage-and-regional-availability
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS

Disk dengan kunci yang dikelola pelanggan

#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

Membersihkan sumber daya

Jalankan perintah berikut untuk menghapus grup sumber daya, VM, dan semua sumber daya terkait.

az group delete --name mySourceResourceGroupName

Referensi sampel

Skrip ini menggunakan perintah berikut untuk membuat snapshot dalam langganan target menggunakan Id snapshot sumber. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
az snapshot show Mendapatkan semua properti snapshot menggunakan nama dan properti grup sumber daya snapshot. Properti Id digunakan untuk menyalin snapshot ke langganan yang berbeda.
az snapshot create Menyalin snapshot dengan membuat snapshot di langganan yang berbeda menggunakan Id dan nama snapshot induk.

Langkah berikutnya

Membuat mesin virtual dari snapshot

Untuk informasi selengkapnya tentang antarmuka tingkat panggilan Azure, lihat Dokumentasi antarmuka tingkat panggilan Azure.

Lebih banyak komputer virtual dan sampel skrip CLI disk terkelola dapat ditemukan dalam dokumentasi Azure Linux VM.