Wrapperscript around ghettoVCB

This script is handy if you want to backup a (free) ESXi server without console access. You set this script up in a cron of a seperate server, which will initiate the backups. It heavily relies on ghettoVCB, a great tool! If you’re looking for a more professional tool, check out esxpress (~1k / esx host).

#!/bin/bash
# Heavily depend on http://communities.vmware.com/docs/DOC-8760

# Backup Variables
WORK=/root/esx-backup
LOG=/root/esx-backup/logs       # log directory (local)
RTMP=/tmp                       # temp directory (remote)
BACKUPALL=YES                   # YES to backup all VMS, and ignore the "serverlist"

# Mailing Variables
SUBJECT="Myhost- ghettoVCB"
EMAIL="sender@kvaes.be"

cd $WORK

# KILL OFF OLD PROCESSES
for PID in $(ps -ef | grep -v "grep" | grep -v "$$" | grep -i "backup" | awk '{ print $2 }')
do
  KILL=`kill $PID`
done

# START BACKUP

for SERVER in $(ls -l | grep ^d | grep -v logs | awk '{print $8}'); do
        DATE=`date +"%Y%m%d"`
        echo "STARTING - $SERVER" >> $LOG/$DATE-$SERVER.log
        date >> $LOG/$DATE-$SERVER.log
        if [ "$BACKUPALL" = "YES" ] ; then
                GENERATE=`ssh root@$SERVER -C vim-cmd vmsvc/getallvms | awk '{ print $2 " " $3 }' | grep -v "Name File" > $SERVER/serverlist`
        fi
        COPY=`scp $SERVER/* root@$SERVER:$RTMP >> $LOG/$DATE-$SERVER.log`
        RIGHTS=`ssh root@$SERVER -C chmod 755 $RTMP/ghettoVCB.sh`
        BACKUP=`ssh root@$SERVER -C $RTMP/ghettoVCB.sh $RTMP/serverlist >> $LOG/$DATE-$SERVER.log`
        echo "ENDED - $SERVER" >> $LOG/$DATE-$SERVER.log
        echo "BACKUP FAILED" > $LOG/latest-$SERVER.log
        grep -v "Clone" $LOG/$DATE-$SERVER.log > $LOG/latest-$SERVER.log
        mailx -s "$SUBJECT" "$EMAIL" < $LOG/latest-$SERVER.log
done
Advertisements