Wednesday, 9 July 2008

Backing up VMware Workstation VMs

We run a small number of VM instances which are mostly sandbox type environments and hence backups are not always essential, but if you are running something a bit more crucial and wish to back up a VM regularly, what are your options? Running 'traditional' third party backup s/w within the guest OS is one approach (and adopted widely) and more VM-centric enterprise solutions such as VMware's own VCB (VMWare Consolidated Backup) are also well suited but only available for VMware ESX.

Where does that leave us if we run VMware Workstation and want to back up the entire VM Guest 'image'? The easiest manual way of doing this is by selecting 'suspend' in VMware Workstation for the relevant guest, copy the VM files to another drive  (.vmx, .vmdk etc) and restart the VM. Easy. The con of this, of course, is that you are bringing down your VM for a short period but for most situations it does the job and has the advantage of being very easy to get the VM started again in a DR situation (i.e. simply take the backed up VM files and fire them up on a new host).

So how can we use the above 'suspend and resume' approach but automate it so it does this nightly for example? The command line comes to your rescue :-)

Before you start, ensure your relevant VM is not open on the desktop through the VMWare Workstation GUI, as this locks the instance.

Starting a VM instance from the command line

Use vmrun command with start parameter, for example:

"C:\Program Files\VMware\VMware Workstation\vmrun" start "F:\YourVMs\YourVMInstance\Windows Server 2003 Standard Edition.vmx"

Suspending a VM instance from the command line

Use vmrun command with suspend parameter, for example:

"C:\Program Files\VMware\VMware Workstation\vmrun" suspend "F:\YourVMs\YourVMInstance\Windows Server 2003 Standard Edition.vmx"

(for more switches, simply type vmrun with no parameters).

Creating a backup script

We've now got all we need to create a very simple windows cmd file, for example, create a VMBackup.cmd file and enter:

:: Suspend VM
"C:\Program Files\VMware\VMware Workstation\vmrun" suspend "F:\YourVMs\YourVMInstance\Windows Server 2003 Standard Edition.vmx"

:: ROBOCOPY (or xcopy) the files somewhere, pref a different box or network drive, for example
Robocopy.exe F:\YourVMs\YourVMInstance\ G:\externaldrive\backuparea\VMBackups\ /e /np /eta /r:1 /w:1 /log:F:\VMScheduledBackups\Logs\YourVMInstance-BackupLog.txt

:: restart the VM
"C:\Program Files\VMware\VMware Workstation\vmrun" start "F:\YourVMs\YourVMInstance\Windows Server 2003 Standard Edition.vmx"

You can now schedule this cmd file to run as a windows scheduled task in the normal way.  I use both a daily and weekly backup regime.

This is obviously a very simple backup approach and it does not alert backup failures etc (you've just got your trusty logs!) but for non-critical sandbox type environments it does the job nicely.

Clarkey

5 comments:

Anonymous said...

if you want to extend this to include some log, it should be possible to add a line or two to your script to echo comments and run a md5 or similar on the source and backup.
i am not about to add how to do this but a quick search of the internet should be enough to work it out

Dave Clarke said...

Yes absolutely, all my backup scripts have full logging, but these were not included to keep the example short. Cheers Dave

Mike said...

Hi - I completely powered off my VM and copied the entire VM directory. I then used the "Import" feature to add the VM to a different computer also running VMWare Workstation 7. The VM began booting, but blue-screened during Windows start-up. Can you see anything I did wrong?
I am running VMWare Workstation 7 and my guest OS is Windows Server 2008 R2 Standard.

Dave Clarke said...

I've not had any problems opening backed up VMs (we have DR tested a few times), although we have not upgraded to Workstation 7 yet. It is worth reading the release notes for version 7 http://www.vmware.com/support/ws7/doc/releasenotes_ws7.html to see if this helps you. Dave

Deikensentsu said...

Its worth noting that you can save your commands as "jobs" in RoboCopy, which allows for really short commands to backup and restore on the fly if you can't schedule this to be done regularly (like on my company laptop I take home every night).

The /SAVE:jobname command will save the preceding command as a .rcj file, you can then run robocopy /JOB:jobname.rcj and it will execute the commands saved in the file.