Skip to content

Instantly share code, notes, and snippets.

@mathieu-benoit
Last active May 18, 2018 15:23
Show Gist options
  • Save mathieu-benoit/e9bf7ac5d93f4c1270a635af147f98c0 to your computer and use it in GitHub Desktop.
Save mathieu-benoit/e9bf7ac5d93f4c1270a635af147f98c0 to your computer and use it in GitHub Desktop.
Copy Azure VM Managed Images
#This script has been inspired by this resource: https://michaelcollier.wordpress.com/2017/05/03/copy-managed-images/
#But maybe this new Private Preview feature will help in the future? https://azure.microsoft.com/en-us/blog/shared-image-gallery-now-in-limited-public-preview/
#Source:
SourceSubscriptionId=?
RG=mylinuxvm
VM=mylinuxvm
LOC=canadaeast
SNAP=mylinuxmsnapshot
#Target:
TargetSubscriptionId=?
RG2=mylinuxvmtarget
LOC2=eastus
STOR=mytargetlinuxvmsetup
CONT=mytargetsnapshotcontainer
BLOB="myosdisk.vhd"
#Set the source subscription
az account set --subscription $SourceSubscriptionId
#Create a disk snapshot
diskName=$(az vm show -g $RG -n $VM --query "storageProfile.osDisk.name" -o tsv)
az snapshot create -g $RG -n $SNAP --location $LOC --source $diskName
snapshotId=$(az snapshot show -g $RG -n $SNAP --query "id" -o tsv )
# Get the SAS for the snapshotId
snapshotSasUrl=$(az snapshot grant-access -g $RG -n $SNAP --duration-in-seconds 3600 -o tsv)
# Switch to the target subscription
az account set --subscription $TargetSubscriptionId
#Create a target resource group and storage account
az group create -n $RG2 -l $LOC2
az storage account create -g $RG2 -l $LOC2 -n $STOR --kind Storage --sku Standard_LRS
# Get the key and sas token of the target storage account
storageAccountKey=$(az storage account keys list -g $RG2 --account-name $STOR --query "[:1].value" -o tsv)
storageSasToken=$(az storage account generate-sas --expiry 2018-06-15'T'12:00'Z' --permissions aclrpuw --resource-types sco --services b --https-only --account-name $STOR --account-key $storageAccountKey -o tsv)
#Create the target storage container
az storage container create -n $CONT --account-name $STOR --sas-token $storageSasToken
#Copy the snapshot to the target region using the SAS URL
copyId=$(az storage blob copy start --source-uri $snapshotSasUrl --destination-blob $BLOB --destination-container $CONT --sas-token $storageSasToken --account-name $STOR)
#Figure out when the copy is destination-container
#TODO: Put this in a loop until status is 'success'
az storage blob show --container-name $CONT -n $BLOB --account-name $STOR --sas-token $storageSasToken --query "properties.copy.status"
#Get the URI of the blob
blobEndpoint=$(az storage account show -g $RG2 -n $STOR --query "primaryEndpoints.blob" -o tsv)
osDiskVhdUri="$blobEndpoint$CONT/$BLOB"
#Create the snapshot in the target resource group
snapshotName="$BLOB-snap"
az snapshot create -g $RG2 -n $snapshotName -l $LOC2 --source $osDiskVhdUri
#Create the image based on the snapshot
snapshotId=$(az snapshot show -g $RG2 -n $snapshotName --query "id" -o tsv)
az image create -g $RG2 -n $BLOB -l $LOC2 --os-type Linux --source $snapshotId
#Create a VM based on this image
imageId=$(az image show -g $RG2 -n $BLOB --query "id")
az vm create -g $RG2 -n MyTargetVm --image $imageId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment