Backup of VMWare Fusion Virtual Machines

If you are backing up a VMWare Fusion virtual machine, you should snapshot, suspend or best of all shutdown (shutdown is recommended by VMWARE) the machine, before running the backup. Then once the backup has finished if you have shutdown or suspended the virtual machine you may start the virtual machine. Below are some helpful commands (as well as some links) for doing this on Mac OS X with VMware Fusion v2.

Helpful VMWare Fusion Scripts

  
  Note : The link above offers the most recent versions of scripts relating to VMWare Fusion.
         One of the scripts available at the above link will suspend all running virtual machines
         on the system, execute a script of your choice and then resume all the virtual machines
         which were initially suspended.
  

Helpful VMware Fusion Commands



  Later versions of VMWare Fusion have a different path specified for the 'vmrun' command : 
  "/Applications/VMware Fusion.app/Contents/Library/vmrun"



Suspend All VMWare Fusion Instances

NOTE : The latest version of the script listed below is now available on GitHub

If you copy the script below into a file “vmware_fusion_suspend_instances.bash” and then make it executable you will have a script on your system which will allow you to suspend all running VMware Fusion instances.

Note : “chmod 755 /path/to/vmware_fusion_suspend_instances.bash” will make it executable.

This script is released under the GNU GPL.

#/bin/bash 

# (C) 2011 Henri Shustak
# Lucid Information Systems
# http://www.lucidsystems.org

# Released under the GNU GPL v3 or later.

# Version history 
# v1.0 - initial release
# v1.1 - added optional reporting of VM's which are suspended by the script

# basic script which will attempt to suspend all VMWare Fusion systems running on a system.

# configuration 
if [ "${list_suspended}" == "" ] ; then
    # when suspending VM's the default is to not report that they have been suspended.
    list_suspended="NO"
fi

# internal variables
VMRUN_PATH="/Library/Application Support/VMware Fusion/vmrun"
num_vms_running=0
run_count_multiplier=2
run_count=0
max_run_count=0
next_vm_to_suspend=""

# how many vms are running?
function calculate_num_vms_to_suspend {
    sync
    num_vms_running=`"${VMRUN_PATH}" list | head -n 1 | awk -F "Total running VMs: " '{print $2}'`
    if [ $? != 0 ] || [ "$num_vms_running" == "" ] ; then
        # report the problem with getting the list of vm's
        echo "    ERROR! : Unable to determine the number of VM instances which are running : ${next_vm_to_suspend}"
        sleep 3
        sync
        exit -1
    fi
}

# get path to the vm we will try to suspend next
function calculate_path_to_next_vm_to_suspend {
    next_vm_to_suspend=`"${VMRUN_PATH}" list | head -n 2 | tail -n 1`
    if [ $? != 0 ] || [ "$num_vms_running" == "" ] ; then
        # report the problem with getting the list of vm's
        echo "    ERROR! : Unable to determine the path to the next VM instances to suspend : ${next_vm_to_suspend}"
        sleep 3
        sync
        exit -5
    fi
}

# get path to the vm we will try to suspend next
function suspend_next_vm {
    if [ "${next_vm_to_suspend}" != "" ] ; then 
        sync
        next_vm_to_suspend=`"${VMRUN_PATH}" -T fusion suspend "${next_vm_to_suspend}"`
        if [ $? != 0 ] ; then
            # report the problem with suspending this VM
            echo "    ERROR! : Unable to suspend VM : ${next_vm_to_suspend}"
            sleep 3
            sync
        else
            if [ "${list_suspended}" == "YES" ] ; then
                echo "    Successfully suspended VM : ${next_vm_to_suspend}"
            fi
        fi
    else
        # this check is not essential as it is covered by another function
        calculate_num_vms_to_suspend
        if [ ${num_vms_running} == 0 ] ; then
            echo "    ERROR! : No VM instances was found to suspend."
            exit -3
        else
            echo "    ERROR! : VM instances was found to suspend, but was not able to determine the path within the filesystem."
            exit -4
        fi
    fi
}

# logic
if [ -e "${VMRUN_PATH}" ] ; then
    calculate_num_vms_to_suspend
    max_run_count=`echo "$num_vms_running * ${run_count_multiplier}" | bc`
    while [ $num_vms_running != 0 ] ; do
        # One or more VM's are running and will try attempt to suspend.
        if [ ${run_count} != $max_run_count ] || [ $num_vms_running != 0 ] ; then
            # we have not hix the max run count
            calculate_path_to_next_vm_to_suspend
            suspend_next_vm
        else
          break  
        fi
        ((run_count++))
        calculate_num_vms_to_suspend
    done
else
    echo "    ERROR! : Unable to locate the VMWare Fusion run file."
    echo "             Please check that VMware Fusion is installed on this system."
    echo "             File referenced : ${VMRUN_PATH}"
    exit -2
fi

exit 0