Backup Script for VMware

From KlavoWiki
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

#!/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"

mkdir -p $strTempDir
touch $strLogFile

# 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}'`
   strvmxName=`echo "$FILE" | awk -F/ '{print $7}'`
   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: [standard] $strVMName/$strvmxName" >> $strLogFile
      echo "*** Suspend: [standard] $strVMName/$strvmxName"
      vmrun -T server -h https://vm.klaverstyn.com:8333/sdk -u root -p password stop "[standard] $strVMName/$strvmxName" soft >> $strLogFile
      echo "*** ZIP: $FILE" >> $strLogFile
      echo "*** ZIP: $FILE"
      strZipName=$strVMName.tar.gz
      tar -cvzf $strTempDir/$strZipName $strVMName/* >> $strLogFile
      echo "*** Start: [standard] $strVMName/$strvmxName" >> $strLogFile
      echo "*** Start: [standard] $strVMName/$strvmxName"
      vmrun -T server -h https://vm.klaverstyn.com:8333/sdk -u root -p password start "[standard] $strVMName/$strvmxName" >> $strLogFile
   fi
done

echo "!!! Backup Complete"

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"