Backing up VMware Guest VM’s
I’m responsible for maintaining 15+ VMware guest vm’s at my work and I needed to find a quick solution to ensure that I have proper backups for these guest vm’s should the host decide to misbehave. What I’ve done is created a nightly and weekly script for each guest vm that I wanted to backup and set my scripts to run in the nightly and weekly cronjobs.
VMware for Linux has a perl shell script (/usr/bin/vmware-cmd) as part of its installaton. Running the script by itself reveals a fairly extensive list of options and syntax, but I’m only interested in suspending and starting the guest vm’s from shell. I’ll include the list of options and syntax at the end of post.
What these scripts do is suspend the guest vm, tar the guest vm directory to a SAN volume mapped through Samba and restart the guest vm when everything is done. Depending on the size of your guest vms and network infrastructure, this process could take any where from 2 – 15 minutes so you have to decide if this is something you want to implement for yourself. Whatever services hosted on your guest vm’s will be unavailable during the backup. The reason why I do this is that if a vm should ever become corrupt from the night before or the vm host crashes, I can easily bring up another vm host and restore the vm from my tar’d backup in less time than it normally would if I had to rebuild the entire guest vm.
Here’s a sample of the nightly script:
#!/bin/bash
echo “Suspending VM…”
/usr/bin/vmware-cmd /srv/vmware/Virtual\ Machines/yourvm/yourvm.vmx suspend
cd /srv/vmware/Virtual\ Machines/
echo “Backing up VM…”
tar czvf /samba/vmbackup/Nightly/yourvm-nightly.tar.gz yourvm
echo “Starting VM…”
/usr/bin/vmware-cmd /srv/vmware/Virtual\ Machines/yourvm/yourvm.vmx start
Here’s a sample of my weekly script. For the weekly, I have it send out an email to me when it completes:
#!/bin/bash
echo “Suspending VM…”
/usr/bin/vmware-cmd /srv/vmware/Virtual\ Machines/yourvm/yourvm.vmx suspend
cd /srv/vmware/Virtual\ Machines/
echo “Backing up VM…”
tar czvf /samba/vmbackup/Weekly/yourvm-$(date +”%Y%m%d”).tar.gz yourvm
echo “Starting VM…”
/usr/bin/vmware-cmd /srv/vmware/Virtual\ Machines/yourvm/yourvm.vmx start
echo “Weekly Backup Successful: $(date)” | mail -s “yourvm weekly backup” admin@company.com
If you have other methods that you’ve implemented for backing up your guest vm’s, I’d like to hear about them so feel free to comment and share them with everyone.
Usage:
/usr/bin/vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>
/usr/bin/vmware-cmd -s <options> <server-action> <arguments>Options:
Connection Options:
-H <host> specifies an alternative host (if set, -U and -P must also be set)
-O <port> specifies an alternative port
-U <username> specifies a user
-P <password> specifies a password
General Options:
-h More detailed help.
-q Quiet. Minimal output
-v Verbose.Server Operations:
/usr/bin/vmware-cmd -l
/usr/bin/vmware-cmd -s register <config_file_path>
/usr/bin/vmware-cmd -s unregister <config_file_path>
/usr/bin/vmware-cmd -s getresource <variable>
/usr/bin/vmware-cmd -s setresource <variable> <value>VM Operations:
/usr/bin/vmware-cmd <cfg> getconnectedusers
/usr/bin/vmware-cmd <cfg> getstate
/usr/bin/vmware-cmd <cfg> start <powerop_mode>
/usr/bin/vmware-cmd <cfg> stop <powerop_mode>
/usr/bin/vmware-cmd <cfg> reset <powerop_mode>
/usr/bin/vmware-cmd <cfg> suspend <powerop_mode>
/usr/bin/vmware-cmd <cfg> setconfig <variable> <value>
/usr/bin/vmware-cmd <cfg> getconfig <variable>
/usr/bin/vmware-cmd <cfg> setguestinfo <variable> <value>
/usr/bin/vmware-cmd <cfg> getguestinfo <variable>
/usr/bin/vmware-cmd <cfg> getid
/usr/bin/vmware-cmd <cfg> getpid
/usr/bin/vmware-cmd <cfg> getproductinfo <prodinfo>
/usr/bin/vmware-cmd <cfg> connectdevice <device_name>
/usr/bin/vmware-cmd <cfg> disconnectdevice <device_name>
/usr/bin/vmware-cmd <cfg> getconfigfile
/usr/bin/vmware-cmd <cfg> getheartbeat
/usr/bin/vmware-cmd <cfg> getuptime
/usr/bin/vmware-cmd <cfg> getremoteconnections
/usr/bin/vmware-cmd <cfg> gettoolslastactive
/usr/bin/vmware-cmd <cfg> getresource <variable>
/usr/bin/vmware-cmd <cfg> setresource <variable> <value>
/usr/bin/vmware-cmd <cfg> setrunasuser <username> <password>
/usr/bin/vmware-cmd <cfg> getrunasuser
/usr/bin/vmware-cmd <cfg> getcapabilities
/usr/bin/vmware-cmd <cfg> addredo <disk_device_name>
/usr/bin/vmware-cmd <cfg> commit <disk_device_name> <level> <freeze> <wait>
/usr/bin/vmware-cmd <cfg> answer