Search code examples
azurepowershellazure-storageazure-virtual-machine

How can i copy a vm snapshot to storage account in Azure?


I can successfully create a VM disk snapshot using az cli. How can i copy the snapshot to an azure storage account using az cli ?


Solution

  • How can i copy the snapshot to an azure storage account using az cli ?

    You can use the command below to copy the VM snapshot to an Azure storage account using Azure CLI.

    First, copy the VM snapshot URL from the portal:

    Azure Portal > Snapshots > select your snapshot > Snapshot export > generate URL.

    enter image description here

    Command:

    az storage blob copy start --account-name "venkat679" --destination-blob "abcd.vhd" --destination-container "test" --account-key "<Your account key>" --source-uri "<snapshoturl>"
    

    Output:

    {
      "client_request_id": "bc99xxxxxee-a269-00155df677e8",
      "copy_id": "xxxx",
      "copy_status": "pending",
      "date": "2024-04-04T06:15:30+00:00",
      "etag": "\"0x8DC546EA152C803\"",
      "last_modified": "2024-04-04T06:15:30+00:00",
      "request_id": "8xxxxx,
      "version": "2022-11-02",
      "version_id": null
    }
    

    enter image description here

    It takes some time to finish the copy process, and after some time, it will reflect in your storage container.

    Portal:

    The file has been successfully copied to an Azure storage container.

    enter image description here

    Reference:

    az storage blob copy | Microsoft Learn

    Update:

    Here is the full script with output to copy snapshot to blob storage in bash.

    subscriptionId="xxxx"
    resourceGroupName="xxx" 
    snapshotName="snap1"
    sasExpiryDuration=3600
    storageAccountName="venkat123"
    storageContainerName="test"
    storageAccountKey="xxxx"
    destinationVHDFileName="sample.vhd"
    
    az account set --subscription $subscriptionId
    
    sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)
    
    az storage blob copy start --account-name $storageAccountName --destination-blob $destinationVHDFileName --destination-container $storageContainerName  --account-key $storageAccountKey --source-uri $sas  
    

    Output:

    enter image description here