Backing up vSphere VMs

I found that the easiest method of backing up VMware vSphere VMs, lacking an actual backup infrastructure, is creating an OVF template of the VM (or VMs) which can be restored simply by deploying the OVF template in vCenter.

This is particularly easy when the VM can be shut down for the process.

$vm | Export-VApp -Destination "X:\MyBackups" -Force

This creates a folder in X:\MyBackups that contains the OVF file for the VM.

If the VM cannot be shut down, it is possible to create a clone of the VM before backing up the clone. Creating a clone is less straightforward and requires several steps. The vSphere operations expert in the office told me how to do this.

First thing to do is create a snapshot to base the clone on. It is perhaps important to keep a pointer to the snapshot so that it can be removed again after the process.

$snapshot = $vm | New-Snapshot -Name "Backup Snapshot" -Quiesce

(Note that the -Quiesce parameter requires VMware tools on the VM.)

For some reason we need a View on the VM.

$view = $vm | Get-View

To create a clone we need a CloneSpec.

$clonespec = New-Object VMware.Vim.VirtualMachineCloneSpec

The CloneSpec requires a snapshot the clone will later be based on...

$clonespec.Snapshot = $view.Snapshot.CurrentSnapshot

...and a location.

$clonespec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

The location for the CloneSpec wants a "managed object reference" ("moref") to a Datastore.

$dsview = $vm | Get-Datastore | Get-View
$moref = $dsview.MoRef
$clonespec.Location.Datastore = $moref

And it needs to be "transformed" for some reason.

$clonespec.Location.Transform = [VMware.Vim.VirtualMachineRelocateTransformation]::sparse

And finally, the clone can be created in the same logical folder as the original VM.

$view.CloneVM($view.Parent, "MyClone", $clonespec)

The cloned VM can now be backed up in the same way as explained in the beginning. It is powered off from the beginning and should perhaps not be powered on to avoid confusion.

 © Andrew Brehm 2016