Backup Script for VMware

From KlavoWiki
Revision as of 11:29, 16 November 2008 by David (Sọ̀rọ̀ | contribs)
Jump to navigationJump to search

The following script will make a compressed tar.gz file for each VM that is not included in the SkipServer array.

VMware Server 2.0.x

I'm in the process of changing the usage of vmware-cmd to vmrun and changing the paramaters to suite.

VMware Server 1.0.x

#!/bin/bash
# Variables used by script
strCurDate=$(date +%Y%m%d)
strVMWareDir="/var/lib/vmware/Virtual Machines"
strTempDir="/backups/$strCurDate/"
strLogFile="/backups/$strCurDate/vmbackup.log"

# Array of machines not to back up based on directory name in /var/lib/vmware/Virtual Machines/
arrSkipServer=(servername1 servername2 servername3)
arrSkipServer_Count=${#arrSkipServer[@]}

echo "!!! Starting VMWare backup of $strVMWareDir/" >> $strLogFile
echo "!!! Starting VMWare backup of $strVMWareDir/"
mkdir -p $strTempDir
cd "$strVMWareDir"

find "$strVMWareDir"/ -iname *.vmx | while read FILE
do
   strVMName=`echo "$FILE" | awk -F/ '{print $6}'`
   arrSkipServer_Index=0
   strSkipServerFlag=0
   while [ "$arrSkipServer_Index" -lt "$arrSkipServer_Count" ]
   do
      if [ "${arrSkipServer[$arrSkipServer_Index])}" == "$strVMName" ]
      then
         strSkipServerFlag=1
      fi
      ((arrSkipServer_Index=arrSkipServer_Index + 1))
   done

   if [ $strSkipServerFlag == 0 ]
   then
      echo "*** Suspend: $FILE" >> $strLogFile
      echo "*** Suspend: $FILE"
      vmware-cmd "$FILE" suspend >> $strLogFile
      echo "*** ZIP: $FILE" >> $strLogFile
      echo "*** ZIP: $FILE"
      strZipName=$strVMName.tar.gz
      tar -cvzf $strTempDir/$strZipName $strVMName/* >> $strLogFile
      echo "*** Start: $FILE" >> $strLogFile
      echo "*** Start: $FILE"
      vmware-cmd "$FILE" start >> $strLogFile
   fi
done

echo "!!! Backup Complete"