Backup Script for VMware: Difference between revisions

From KlavoWiki
Jump to navigationJump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
The following script will make a compressed tar.gz file for each VM that is not included in the SkipServer array.
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 =
= 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.<br>
The following '''has not''' been tested.
<pre>
<pre>
#!/bin/bash
#!/bin/bash
Line 10: Line 8:
strTempDir="/backups/$strCurDate/"
strTempDir="/backups/$strCurDate/"
strLogFile="/backups/$strCurDate/vmbackup.log"
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/
# Array of machines not to back up based on directory name in /var/lib/vmware/Virtual Machines/
arrSkipServer=(pappasmurf pappasmurftest au-bne-testxp1)
arrSkipServer=(servername1 servername2 servername3)
arrSkipServer_Count=${#arrSkipServer[@]}
arrSkipServer_Count=${#arrSkipServer[@]}


Line 37: Line 38:
   if [ $strSkipServerFlag == 0 ]
   if [ $strSkipServerFlag == 0 ]
   then
   then
       echo "*** Suspend: [standard]$strVMName/$strvmxName" >> $strLogFile
       echo "*** Suspend: [standard] $strVMName/$strvmxName" >> $strLogFile
       echo "*** Suspend: [standard]$strVMName/$strvmxName"
       echo "*** Suspend: [standard] $strVMName/$strvmxName"
       vmrun -T server -h https://bne-vserver.ingrnet.com/sdk -u root -p OSauce0k$ suspend "[standard] $strVMName/$strvmxName" >> $strLogFile
       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" >> $strLogFile
       echo "*** ZIP: $FILE"
       echo "*** ZIP: $FILE"
       strZipName=$strVMName.tar.gz
       strZipName=$strVMName.tar.gz
       tar -cvzf $strTempDir/$strZipName $strVMName/* >> $strLogFile
       tar -cvzf $strTempDir/$strZipName $strVMName/* >> $strLogFile
       echo "*** Start: [standard]$strVMName/$strvmxName" >> $strLogFile
       echo "*** Start: [standard] $strVMName/$strvmxName" >> $strLogFile
       echo "*** Start: [standard]$strVMName/$strvmxName"
       echo "*** Start: [standard] $strVMName/$strvmxName"
       vmrun -T server -h https://bne-vserver.ingrnet.com/sdk -u root -p OSauce0k$ suspend "[standard] $strVMName/$strvmxName" >> $strLogFile
       vmrun -T server -h https://vm.klaverstyn.com:8333/sdk -u root -p password start "[standard] $strVMName/$strvmxName" >> $strLogFile
   fi
   fi
done
done

Latest revision as of 11:55, 23 May 2009

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"