#!/bin/bash
#
# Build   : 09.01.2013
# Version : v1.0
# Author  : avladulescu@bfproject.ro
#
# Node Controller settings backup script
#

STORAGE="10.20.30.10"
LOG_FILE="/var/log/engine-backup.log"
MOUNT="/datastore01"

LOG () { echo "`date +%H:%M:%S' '%x`  $@ " >> ${LOG_FILE} ; }

LOG "Script wake-up, cheking storage server ip connection"

#check storage is responding
if [ `ping -c 2 -n -i 0.1 -W 1 -q ${STORAGE} > /dev/null 2>&1 ; echo $?` -eq "0" ]; then

 LOG "Storage server IP ${STORAGE} configured is responsive"
 LOG "Checking remote volume is mounted on local system"

 #check remote path is mounted
 if [ `df | grep ${MOUNT} > /dev/null 2>&1 ; echo $?` -eq 0 ]; then

  LOG "Volume is mounted, creating temporary directory to store data"
  mkdir -p /tmp/engine-backup
  LOG "Dumping engine database"
  cd /tmp/engine-backup
  pg_dump -C -E UTF8 --column-inserts --disable-dollar-quoting --disable-triggers -U postgres --format=p -f engine.sql engine && wait
  cd /
  LOG "Saving CA directory"
  tar zcf /tmp/engine-backup/ovirt-engine.tgz /etc/pki/ovirt-engine
  LOG "Wrapping up dump arhive"
  tar zcf /tmp/engine-backup-`date +%d%m%Y`.tgz /tmp/engine-backup
  rm -rf /tmp/engine-backup
  cp /tmp/engine-backup-`date +%d%m%Y`.tgz /home/backup/
  mv -f /tmp/engine-backup-`date +%d%m%Y`.tgz ${MOUNT}/nas01.BackupZone/node-controller/
  LOG "Archive has been placed on the storage server for backup"

 else

  LOG "Failed to start backup as remote volume is not mounted"

 fi
else

 LOG "Cannot reach storage IP ${STORAGE} configured, exiting"

fi

exit 0

#EOF

