Mengekspor/Menyalin VHD dari disk terkelola ke akun penyimpanan di wilayah yang berbeda dengan PowerShell (Windows)

Skrip ini mengekspor VHD dari disk terkelola ke akun penyimpanan di wilayah yang berbeda. Ini pertama-tama menghasilkan SAS URI dari disk terkelola dan kemudian menggunakannya untuk menyalin VHD yang mendasarinya ke akun penyimpanan di wilayah yang berbeda. Gunakan skrip ini untuk menyalin disk terkelola ke wilayah lain untuk ekspansi regional.

Jika perlu, instal modul Azure PowerShell menggunakan petunjuk yang ada di panduan Azure PowerShell, lalu jalankan Connect-AzAccount untuk membuat koneksi dengan Azure. Selain itu, Anda harus memiliki kunci publik SSH bernama id_rsa.pub di direktori .ssh profil pengguna Anda.

Jika Anda tidak memiliki langganan Azure, buatlah akun gratis sebelum Anda memulai.

Contoh skrip

#Provide the subscription Id of the subscription where managed disk is created
$subscriptionId = "yourSubscriptionId"

#Provide the name of your resource group where managed is created
$resourceGroupName ="yourResourceGroupName"

#Provide the managed disk name 
$diskName = "yourDiskName"

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

#Provide storage account name where you want to copy the underlying VHD of the managed disk. 
$storageAccountName = "yourstorageaccountName"

#Name of the storage container where the downloaded VHD will be stored
$storageContainerName = "yourstoragecontainername"

#Provide the key of the storage account where you want to copy the VHD of the managed disk. 
$storageAccountKey = 'yourStorageAccountKey'

#Provide the name of the destination VHD file to which the VHD of the managed disk will be copied.
$destinationVHDFileName = "yourvhdfilename"

#Set the value to 1 to use AzCopy tool to download the data. This is the recommended option for faster copy.
#Download AzCopy v10 from the link here: https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10
#Ensure that AzCopy is downloaded in the same folder as this file
#If you set the value to 0 then Start-AzStorageBlobCopy will be used. Azure storage will asynchronously copy the data. 
$useAzCopy = 1 

# Set the context to the subscription Id where managed disk is created
Select-AzSubscription -SubscriptionId $SubscriptionId

#Generate the SAS for the managed disk 
$sas = Grant-AzDiskAccess -ResourceGroupName $ResourceGroupName -DiskName $diskName -DurationInSecond $sasExpiryDuration -Access Read 

#Create the context of the storage account where the underlying VHD of the managed disk will be copied
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

#Copy the VHD of the managed disk to the storage account
if($useAzCopy -eq 1)
{
    $containerSASURI = New-AzStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds($sasExpiryDuration) -FullUri -Name $storageContainerName -Permission rw
    azcopy copy $sas.AccessSAS $containerSASURI

}else{

    Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName
}

Penjelasan skrip

Skrip ini menggunakan perintah berikut untuk menghasilkan SAS URI dari disk terkelola dan menyalin VHD yang mendasarinya ke akun penyimpanan menggunakan SAS URI. Setiap perintah dalam tabel ditautkan ke dokumentasi khusus perintah.

Perintah Catatan
Grant-AzDiskAccess Menghasilkan SAS URI untuk disk terkelola yang digunakan untuk menyalin VHD yang mendasarinya ke akun penyimpanan.
New-AzureStorageContext Membuat konteks akun penyimpanan menggunakan nama dan kunci akun. Konteks ini dapat digunakan untuk melakukan operasi baca/tulis pada akun penyimpanan.
Start-AzureStorageBlobCopy Menyalin VHD snapshot yang mendasarinya ke akun penyimpanan

Langkah berikutnya

Membuat disk terkelola dari VHD

Membuat mesin virtual dari disk terkelola

Untuk informasi selengkapnya tentang modul Azure PowerShell, lihat dokumentasi Azure PowerShell.

Sampel skrip PowerShell mesin virtual tambahan dapat ditemukan di dokumentasi VM Windows Azure.