将 VM 快照复制到新的 VM 环境

CMR*_*CMR 4 vmware-vcenter vmware-vsphere

我们有 2 个独立的 VMWare 环境,一个是主环境,它在许多站点上拥有数百个虚拟机。另一个是安装在一台服务器上的小得多的,仅用于归档旧系统。

我想要做的是拍摄我们一个实时 VM 的当前状态的快照,并使用它复制到另一个 VMWare 环境并在那里创建一台新机器,将其用作该系统的存档。

这可能/容易吗?

Gre*_*egL 5

该死,如果您一直在使用 vSphere 6,您就可以完成 vCenter 间的克隆并完成它。

无论如何,如果您使用 PowerCLI,这项任务对于 5.5 来说也不是非常困难。

步骤如下:

  1. 获取 VM 的快照(使用 PowerCLI 或任一 GUI 都没有关系)
  2. 使用 PowerCLI 这个方便的一点点将快照克隆到新的 VM:
    New-VM -Name $CloneName -VM $SourceVM -Location $CloneFolder -Datastore $Datastore -ResourcePool $ResourcePool -VMHost $VMHost -LinkedClone -ReferenceSnapshot $Snapshot
    您可以在此处查看所有选项的含义以及如何填充它们。
    他们的关键是“-ReferenceSnapshot”选项。

  3. 将您的新虚拟机导出到 OVF/OVA,或将文件夹从 DS 复制到网络上的某个位置

  4. 将其导入其他 vCenter

我让我的 IT 安全团队要求一份正在运行的 VM 的“取证”副本,包括内存快照,这样他们就可以在出现病毒或某种破坏的情况下进行一些调查。为了让我的生活更轻松,我编写了一个 PS 函数来完成所有繁重的工作。它只需要一个源虚拟机(按名称或对象)和磁盘上的文件夹。它完成其余的工作。

Function ExportVM {
    Param(
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [PSObject]$SourceVM,

    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [String]$DestinationPath
    )

    #Check if the destination path exists, bail out if it doesn't
    if ( -not (Test-path $DestinationPath -IsValid) ) {
        Write-Warning "Please provide a valid path for the exported VM"
        return
    }

    #Get the SourceVM, bail out if it fails
    if ($SourceVM.GetType().Name -eq "string"){
        try {
            $SourceVM = Get-VM $SourceVM -ErrorAction Stop
        }
        catch [Exception]{
            Write-Warning "VM $SourceVM does not exist"
            return
        }
    }
    elseif ($SourceVM -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]){
        Write-Warning "You did not pass a string or a VM object for 'SourceVM'"
        Return
    }

    try {
        $DestinationPath = $DestinationPath + "\" + $SourceVM.Name

        #Setup the required compoments to compute an MD5 hash
        $algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
        $md5StringBuilder = New-Object System.Text.StringBuilder 50
        $ue = New-Object System.Text.UTF8Encoding

        #Define the snapshot name
        $SnapshotName = "IT-Security Export - " + (Get-Date -UFormat "%b-%d-%Y, %R")
        #Create the snapshot
        $Snapshot = New-Snapshot -VM $SourceVM -Name $SnapshotName -Description "Snapshot for IT-Security Forensic export" -Memory -Quiesce -Confirm:$false

        $Snapshot

        #Define variables needed to create the clone
        $CloneFolder = $SourceVM.Folder
        $Datastore = Get-Datastore -RelatedObject $SourceVM
        $ResourcePool = Get-ResourcePool -VM $SourceVM
        $VMHost = Get-VMHost -VM $SourceVM

        #Build a unique name for the cloned machine based on the snapshot name
        $algo.ComputeHash($ue.GetBytes($SnapshotName)) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
        $CloneName = $SourceVM.Name +"_ITSecExport_" + $md5StringBuilder.ToString().SubString(0,15)

        #Clone the VM
        $CloneVM = New-VM -Name $CloneName -VM $SourceVM -Location $CloneFolder -Datastore $Datastore -ResourcePool $ResourcePool -VMHost $VMHost -LinkedClone -ReferenceSnapshot $Snapshot

        #Define the name of the PSDrive, based on the Datastore name
        $DSName = "ITSecExport_" + ($Datastore.name -replace "[^a-zA-Z0-9]","")
        #Check to see if it already exists, remove if it does
        if (Get-PSDrive | Where {$_.Name -like $DSName}) {
            Remove-PSDrive $DSName
        }
        #Add the new drive
        $PSDrive = New-PSDrive -Location $Datastore -Name $DSName -Scope Script -PSProvider VimDatastore -Root "\"

        #Define variables needed to copy the SourceVM's VMX and the snapshot's VMSN
        $SnapshotID = (Get-VM $SourceVM |Get-Snapshot | where {$_.Name -like $SnapshotName}).ExtensionData.ID
        $SourceVM_VMXPath = (Get-View $SourceVM).Config.Files.VmPathName.Split(" ")[1].replace("/","\")
        $SourceVM_VMSNPath = $SourceVM_VMXPath.Replace(".vmx", "-Snapshot" + $SnapshotID + ".vmsn")
        #$CloneVM_VMPath = (Get-View $CloneVM).Config.Files.VmPathName.Split(" ")[1].Split("/")[0]

        #Copy the VMSN and VMX
        Copy-DatastoreItem -Item ${DSName}:\$SourceVM_VMXPath -Destination $DestinationPath -Force
        Copy-DatastoreItem -Item ${DSName}:\$SourceVM_VMSNPath -Destination $DestinationPath -Force

        #Copy-DatastoreItem -Item ${DSName}:\$CloneVM_Path\* $DestinationPath"$CloneName" -Force -Recurse

        #Export the VM
        $CloneVM | Export-VApp -Destination $DestinationPath -Force

        #Clean up
        Remove-VM -DeletePermanently $CloneVM -Confirm:$false
        Remove-Snapshot -Snapshot $Snapshot -Confirm:$false
        Remove-PSDrive -Name $DSName
    }
    catch [Exception]{
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Write-Warning "Looks like we ran in to an error"
        Write-Warning "  $ErrorMessage"
        return
    }
}
Run Code Online (Sandbox Code Playgroud)