To back up a virtual machine, you first need to create a snapshot. Once the snapshot is created, you then need to identify the virtual disks associated with this snapshot. A virtual machine might have multiple snapshots associated with it. Each snapshot has a virtual “copy” of the virtual disks for the virtual machine. These copies are named with the base name of the disk, and a unique decimal number appended to the name. The format of the number is a hyphen character followed by a 6-digit zero-filled number. An example a disk copy name might be mydisk-NNNNNN.vmdk where NNNNNN would be some number like: 000032.
The vSphere API identifies virtual disk files by prefixing the datastore name onto the file system pathname and the filename: [storageN] myvmname/mydisk-NNNNNN.vmdk. The name in square brackets corresponds to the short name of the datastore that contains this virtual disk, while the remainder of the path string represents the location relative to the root of this datastore.
To get the name and characteristics of a virtual disk file, you use the PropertyCollector to select the property: config.hardware.device from a VirtualMachine managed object. This returns an array of virtual devices associated with a VirtualMachine or Snapshot. You must scan this list of devices to extract the list of virtual disks. All that is necessary is to see if each VirtualDevice entry extends to VirtualDisk. When you find such an entry, examine the BackingInfo property. You must extend the type of the backing property to one of the following, or a VirtualMachineSnapshot managed object:
- VirtualDiskFlatVer1BackingInfo
- VirtualDiskFlatVer2BackingInfo
- VirtualDiskRawDiskMappingVer1BackingInfo
- VirtualDiskSparseVer1BackingInfo
- VirtualDiskSparseVer2BackingInfo
It is important to know which backing type is in use in order to be able to re-create the Virtual Disk.It is also important to know that you cannot snapshot a disk of type VirtualDiskRawDiskMappingVer1BackingInfo, and therefore you cannot back up this type of Virtual Disk.
The properties of interest are the backing fileName and the VirtualDisk capacityInKB. Additionally, when change tracking is in place, you should also save the changeID.