在Azure中创建VM的快照

时间:2015-08-11 09:59:14

标签: powershell azure backup snapshot vhd

关注本文(http://www.coreazure.com/snapshot-vms-in-azure-2/)后,我尝试使用Azure门户的PowerShell(PS)创建VM的快照。这是我创建的用于拍摄快照的PS脚本

workflow snapshot1
{
   $subName = 'XYZ'

   $cred = Get-AutomationPSCredential -Name "xyz@xyzgmail.onmicrosoft.com" 

   Add-AzureAccount -Credential $cred
   Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount 'storagename'

   $serviceName = "a1smallvm"
   $vm = Get-AzureVM –ServiceName $serviceName –Name "a1smallvm"
   $storageContainer = "backups"

   InlineScript {
      # Create a storage for putting the backups of OSDisk & DataDisks 
      New-AzureStorageContainer -Name $Using:storageContainer -Permission off

      # Stop VM if running
      $Using:vm | Stop-AzureVM -StayProvisioned

      $vm = Get-AzureVM –ServiceName $Using:serviceName –Name "a1smallvm"

      $vmOSDisk = $vm | Get-AzureOSDisk
      $vmDataDisks = $vm | Get-AzureDataDisk
      Write-output "OSDisk: $vmOSDisk"
      $storageAccountName = $vmOSDisk.MediaLink.Host.Split(‘.’)[0]
      Write-output "Data Disk: $vmDataDisks"
      Write-output "StorageAccountName: $storageAccountName"

      $vmOSBlobName = $vmOSDisk.MediaLink.Segments[-1]

      $vmOSContainerName = $vmOSDisk.MediaLink.Segments[-1].Split(‘/’)[0]

      Write-output "vmOSBlobName: $vmOSBlobName"
      Write-output "vmOSContainerName: $vmOSContainerName"

      # Backup the osblob and oscontainer
      Start-AzureStorageBlobCopy -SrcContainer $vmOSContainerName -SrcBlob $vmOSBlobName -DestContainer $Using:storageContainer

      Get-AzureStorageBlobCopyState -Container $Using:storageContainer -Blob $vmOSBlobName -WaitForComplete

      # Backup the dataBlob and dataContainer
      ForEach ($vmDataDisk in $vmDataDisks) {

        $vmDataBlobName = $vmDataDisk.MediaLink.Segments[-1]

        $vmDataContainerName = $vmDataDisk.MediaLink.Segments[-2].Split(‘/’)[0]

        Start-AzureStorageBlobCopy -SrcContainer $vmDataContainerName -SrcBlob $vmDataBlobName -DestContainer backups -Force

        Get-AzureStorageBlobCopyState -Container backups -Blob $vmDataBlobName -WaitForComplete
      }    
  }    
}

cmdlet

Start-AzureStorageBlobCopy -SrcContainer $vmOSContainerName -SrcBlob $vmOSBlobName -DestContainer $Using:storageContainer

抛出错误:

Error: Start-AzureStorageBlobCopy : Container name 'a1smallvm-a1smallvm-2015-08-11.vhd' is invalid. Valid names start and end with a lower case letter or a number and has in between a lower case letter, number or dash with no consecutive dashes and is 3 through 63 characters long.

我得到的容器名称'a1smallvm-a1smallvm-2015-08-11.vhd'遵循正确的命名格式,但仍然是为什么它给出了一个错误,说明该名称无效。 VM是从门户创建的,它是一种A1类型的VM,操作系统是CentOS“OpenLogic 6.5”。 有什么问题的线索?

以下是Write-output的输出

OSDisk: Microsoft.WindowsAzure.Commands.ServiceManagement.Model.OSVirtualHardDisk
Data Disk: 
StorageAccountName: portalvhds14510n2y65vnh
vmOSBlobName: a1smallvm-a1smallvm-2015-08-11.vhd
vmOSContainerName: a1smallvm-a1smallvm-2015-08-11.vhd

正确的脚本: 存储帐户名称必须相同,否则我们必须添加目标存储帐户的上下文。

$storageAccountName = $vmOSDisk.MediaLink.Host.Split(‘.’)[0] Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount '$storagenameAccountName'

Segments的索引应为 -2 而不是 -1     $vmOSContainerName = $vmOSDisk.MediaLink.Segments[-2].Split(‘/’)[0]

1 个答案:

答案 0 :(得分:0)

我不是PowerShell专家(我确信有更好的方法)但您可以执行以下操作:

$vmOSContainerName = $vmOSDisk.MediaLink.AbsolutePath.Split('/')[1]

这将输出vhds,这是blob容器的名称。