VM Backup

From KlavoWiki
Jump to navigationJump to search

NOTE: Please see Backup Script for VMware for an updated version.

The following script will make a compressed tar.gz file for each existing virtual computer. The compressed file is saved in /root/vmbackup/%date%

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

mkdir -p $strTempDir

echo "!!! Starting VMWare backup of $strVMWareDir/" >> $strLogFile
cd "$strVMWareDir"

find "$strVMWareDir"/ -iname *.vmx | while read FILE
do
   echo "*** Suspend: $FILE" >> $strLogFile
   vmware-cmd "$FILE" suspend >> $strLogFile

   echo "*** ZIP: $FILE" >> $strLogFile
   strZipName=`echo "$FILE" | awk -F/ '{print $6}'`.tar.gz
   tar -cvzf $strTempDir/$strZipName `echo "$FILE" | awk -F/ '{print $6}'`/* >> $strLogFile

   echo "*** Start: $FILE" >> $strLogFile
   vmware-cmd "$FILE" start >> $strLogFile
done

echo "!!! Backup Complete"