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
# 20061226
#
# backup vmware's virtual machines to a remote computer
# 1. check if backupserver is running
# 2. stop the vm's that are running
# 3. local copy the vm files
# 4. start only the vm's that were running
# 5. remote copy of the backup created in step 3
########################################

### 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="alpedhuez.r71.nl"
    VM_HOST_REMOTE_DEST="bonette.r71.nl"
    VM_ROOT_SRC="/DATA/vmware/vmmachines/"   # use trailing backslash, it's used in the rsync command
    VM_LOCAL_BACKUP_DEST="/mnt/usbdrive01/Backup/vmware/vmmachines"
    VM_ROOT_REMOTE_DEST="/DATA/vmware/vmmachines"
    EMAILADDRESS="your_email_account@yourdomain.com"
    VM_REMOTE_DEST="${VM_HOST_REMOTE_DEST}:${VM_ROOT_REMOTE_DEST}"

###################################################################
### 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}

################################
##
## 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 local backup:  >> ${LOGFILE}; echo "Starting local backup (this is going to take a while…)"; echo

# Local rsync copy command (to a local disk)
rsync -avu ${VM_ROOT_SRC} ${VM_LOCAL_BACKUP_DEST} >> ${LOGFILE}

echo Local backup finished >> ${LOGFILE}; echo; echo Local 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

# check if remote server is up using return value: 1 is failed (down), 0 is success (up)
echo; echo >> ${LOGFILE}
echo Starting remote backup procedure.; echo Starting remote backup procedure. >> ${LOGFILE}

ping ${VM_HOST_REMOTE_DEST} -c2|grep -q " 0% packet loss"
if [ $? -eq 0 ]; then
   echo
   echo ${VM_HOST_REMOTE_DEST} is up, ping test succeeded.  >> ${LOGFILE}
   echo ${VM_HOST_REMOTE_DEST} is up, ping test succeeded.

   # Copy local backup to a remote server.
   echo Copy started..; echo Copy started.. >> ${LOGFILE}
   rsync -avu -e ssh ${VM_LOCAL_BACKUP_DEST}/ ${VM_REMOTE_DEST} >> ${LOGFILE}

else
   echo
   echo ${VM_HOST_REMOTE_DEST} unreachable, ping failed: no remote backup.  >> ${LOGFILE}
   echo ${VM_HOST_REMOTE_DEST} unreachable, ping failed: no remote backup.

   echo "wait until the mailserver is running so a report can be send."
   sleep 120 # my mailserver is booting now so wait a little bit
   #exit 1
fi

##
## Mail
##

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