Linux bash script for VMware: automated backup vm’s

Linux bash script for VMware: automated backup vm’s

Linux bash script for VMware: automated backup vm’s 150 150 Roderick Derks

#! /bin/bash
# Roderick Derks
# www.r71.nl
# 20061111
#
# backup vmware's virtual machines to a remote computer
# 1. first stop the vm's that are running
# 2. copy the vm files
# 3. start only the vm's that were running
########################################

### day and hour
    DAY=$(date | cut -d" " -f1)
    HOUR=$(date|tr -s " "|cut -d" " -f4| cut -d":" -f1)
### logfile
    export LOGFILE="/var/log/backup_vmware_servers.log"
    if [ ! -e "${LOGFILE}" ]
       then touch "$LOGFILE" # create a new logfile if not present
    fi
### variables
    VM_HOST_SRC="vmware_host_server.yourdomain.com"
    VM_HOST_DEST="vmware_dest_server.yourdomain.com"
    VM_ROOT_SRC="/DATA/vmware/vmmachines/"   # use trailing backslash, it's used in the rsync command
    VM_ROOT_DEST="/DATA/vmware/vmmachines"
    EMAILADDRESS="your_email_account@yourdomain.com"

### check local machine name
if [ $HOSTNAME != "${VM_HOST_SRC}" ]; then
   echo Hostname=${HOSTNAME}:
   echo "Dit script will only run on ${VM_HOST_SRC}"
   exit
fi

echo $HOSTNAME > ${LOGFILE}          # flush logfile and add data
date >> ${LOGFILE}
echo >> ${LOGFILE}

# check if remote server is up using return value: 1 is failed (down), 0 is success (up)
ping ${DEST_HOST} -c2|grep -q " 0% packet loss"
if [ $? -eq 0 ]; then
   echo
   echo ${VM_HOST_DEST} is up, ping test succeeded.  >> ${LOGFILE}
   echo ${VM_HOST_DEST} is up, ping test succeeded.
else
   echo
   echo ${VM_HOST_DEST} unreachable, ping failed: exit script.  >> ${LOGFILE}
   echo ${VM_HOST_DEST} unreachable, ping failed: exit script.
   exit 1
fi

################################
##
## stop vm's if they are running
##

IFS=$'n' # needed to prevent problems with filenames containing spaces in arrays
for VMFILE in $(find ${VM_ROOT_SRC} -type f -name *.vmx)
do
  vmware-cmd ${VMFILE} getstate|grep -q "getstate() = on"
  if [ $? -eq 0 ]; then
     echo "stop ${VMFILE}"
     array0=( "${array0[@]}" "${VMFILE}" ) # add to array, these vm's  will  be started later on
     echo "stop ${VMFILE}" >> ${LOGFILE}
     vmware-cmd ${VMFILE} stop soft >> ${LOGFILE}
  else
     echo "already stopped: ${VMFILE}" >> ${LOGFILE}
     echo "already stopped: ${VMFILE}"
  fi
done

#echo count to 30
sleep 30

##
## start backup
##

echo >> ${LOGFILE}; echo
echo Starting backup:  >> ${LOGFILE}; echo "Starting backup (this is going to take a while…)"; echo
rsync -avu -e ssh ${VM_ROOT_SRC} ${VM_HOST_DEST}:${VM_ROOT_DEST} >> ${LOGFILE}
echo Backup finished >> ${LOGFILE}; echo; echo Backup finished
echo >> ${LOGFILE}; echo

##
## start vm's if they were running
##

#echo "Elements in array0:  ${array0[@]}"
echo; echo >> ${LOGFILE}

for VM in ${array0[@]}
do
 echo "Starting $VM" >> ${LOGFILE}; echo "Starting $VM"
 vmware-cmd "${VM}" start >> ${LOGFILE}
done

##
## Mail
##

sleep 120 # my mailserver is booting now so wait a little bit
echo >> ${LOGFILE}
echo "Sending e-mail to ${EMAILADDRESS}" >> ${LOGFILE}
date >> ${LOGFILE}          # logfile aanvullen
mail -s "VMWare backupscipt finished" ${EMAILADDRESS} <${LOGFILE}

exit 0

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message