Last active
July 7, 2022 16:45
-
-
Save alankinane/621a4d5e80f869ba701212bdccc9f546 to your computer and use it in GitHub Desktop.
Copy managed disks to archive tier blob storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Provide the name of your resource group where snapshot will be created | |
$vmResourceGroupName = "" | |
#Provide the name of your resource group where the blobs will be stored | |
$storageaccountResourceGroupName = "" | |
#Provide the name of the virtual machine where the snapshot will be made | |
$vmName = "" | |
#Provide storage account name where you want to copy the snapshot. | |
$storageAccountName = "" | |
#Name of the storage container where the downloaded snapshot will be stored | |
$storageContainerName = "" | |
#Provide the name of the VHD files for blob storage | |
$pageblobVHDFileName = "pageblobsnapshot.vhd" | |
$archiveVHDFileName = "blockblobarchive.vhd" | |
#Set virtual machine context where snapshots will be taken | |
$vmContext = Get-AzVM -ResourceGroupName $VMResourceGroupName -Name $vmName | |
#Set the location of virtual machine for snapshot creation | |
$location = $vmContext.Location | |
#Set snapshot config settings | |
#Use the below command for the OS disk | |
$snapshotConfig = New-AzSnapshotConfig -SourceUri $vmContext.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy | |
#For data disks use the below command where [x] is the ID of the data disk | |
#$snapshotConfig = New-AzSnapshotConfig -SourceUri $vmContext.StorageProfile.DataDisks[0].ManagedDisk.Id -Location $location -CreateOption copy | |
#Set the name of the snapshot resource | |
#Use the below command for the OS disk | |
$snapshotName = $vmContext.StorageProfile.OsDisk.name+"-snapshot" | |
#For data disks use the below command where [x] is the ID of the data disk | |
#$snapshotName = $vmContext.StorageProfile.DataDisks[0].name+"-snapshot" | |
#Create snapshot | |
New-AzSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotName -ResourceGroupName $vmResourceGroupName | |
#Generate the SAS for the snapshot - valid for 24 hours | |
$snapshotSAS = Grant-AzSnapshotAccess -ResourceGroupName $vmResourceGroupName -SnapshotName $SnapshotName -DurationInSecond 86400 -Access Read | |
#Get the storage account context for the VHD blobs | |
$storageAccountContext = (Get-AzStorageAccount -ResourceGroupName $storageaccountResourceGroupName -AccountName $storageAccountName).context | |
#Generate the SAS for the storage account - valid for 24 hours | |
$storageaccountSAS = New-AzStorageAccountSASToken -Context $storageAccountContext -Service Blob -ResourceType Service,Container,Object -Permission "rw" -ExpiryTime (Get-Date).AddDays(1) | |
#Copy snapshot to page blob | |
azcopy copy $snapshotSAS.accessSAS "https://$storageAccountName.blob.core.windows.net/$storageContainerName/$pageblobVHDFileName$storageaccountSAS" | |
#Revoke SAS for the snapshot | |
Revoke-AzSnapshotAccess -ResourceGroupName $vmResourceGroupName -SnapshotName $SnapshotName | |
#Delete snapshot | |
Remove-AzSnapshot -SnapshotName $snapshotName -ResourceGroupName $vmResourceGroupName -Force | |
#Copy page blob to block blob | |
azcopy copy "https://$storageAccountName.blob.core.windows.net/$storageContainerName/$pageblobVHDFileName$storageaccountSAS" "https://$storageAccountName.blob.core.windows.net/$storageContainerName/$archiveVHDFileName$storageaccountSAS" --blob-type=BlockBlob | |
#Delete page blog | |
Remove-AzStorageBlob -Container $storageContainerName -Blob $pageblobVHDFileName -Context $storageAccountContext | |
#Set block blob to archive tier | |
$archiveblob = Get-AzStorageBlob -Container $storageContainerName -Blob $archiveVHDFileName -Context $storageAccountContext | |
$archiveblob.BlobClient.SetAccessTier("Archive", $null, "Standard") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment