#!/bin/sh
# Jim Flynn 11-10-2003 jmflynn@covenantretirement.org
# Shell script that reads in vm-list from /etc/vmware/ determines if machine is
# running. If it is running suspend the machine, copy it to a directry for backup
# and resume the machine. Feel free to copy and improve.
#
#
#
# Variables
BACKUPLOG="/var/log/gsxbackup.log"
TARGET="/vmbackup"
OFF="getstate() = off"
ON="getstate() = on"
FILE="/etc/vmware/vm-list"
LIST=`cat "$FILE" | grep "^config" | sed -e 's/config//' -e 's/"//g'`

for config in $LIST
do
state=`/usr/bin/vmware-cmd $config getstate`
# change the number in the first sed command to match how many slashes are in your dir tree.
backupdir=` echo $config | sed -e 's/[/]/ /4' -e 's/ /|/g' | cut -f1 -d\|`
# loops through machines defined in vm-list and performs suspend/copy/resume.
if [ "$state" = "$ON" ]; then
/usr/bin/vmware-cmd $config suspend hard >> $BACKUPLOG
cp -R "$backupdir" "$TARGET" >> $BACKUPLOG
/usr/bin/vmware-cmd $config start hard >> $BACKUPLOG
else
cp -R "$backupdir" "$TARGET" >> $BACKUPLOG
fi
done