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
Gunakan lingkungan Bash di Azure Cloud Shell. Untuk informasi selengkapnya, lihat Mulai Cepat untuk Bash di Azure Cloud Shell.
Jika Anda lebih memilih untuk menjalankan perintah referensi CLI secara lokal, pasang Azure CLI. Jika Anda menjalankan Windows atau macOS, pertimbangkan untuk menjalankan Azure CLI dalam kontainer Docker. Untuk informasi lebih lanjut, lihat Cara menjalankan Azure CLI di kontainer Docker.
Jika Anda menggunakan instalasi lokal, masuk ke Azure CLI dengan menggunakan perintah login az. Untuk menyelesaikan proses autentikasi, ikuti langkah-langkah yang ditampilkan di terminal Anda. Untuk opsi masuk lainnya, lihat Masuk dengan Azure CLI.
Saat Anda diminta, instal ekstensi Azure CLI pada penggunaan pertama. Untuk informasi selengkapnya tentang ekstensi, lihat Menggunakan ekstensi dengan Azure CLI.
Jalankan versi az untuk menemukan versi dan pustaka dependen yang diinstal. Untuk meningkatkan ke versi terbaru, jalankan peningkatan az.
Skrip sampel
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 Coba 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 di lingkungan Bash, Pilih Salin untuk menyalin blok kode, tempel ke Cloud Shell, dan 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
Sampel referensi
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 Azure CLI, lihat Dokumentasi Azure CLI.
Lebih banyak sampel skrip CLI komputer virtual dan disk terkelola dapat ditemukan dalam dokumentasi Azure Linux VM.