#!/bin/bash
#
# this script runs a series of daily backup files, one after another
#
# These should be the only parameters requiring alteration
# the root filename for the appropriate configuration files
# in $EBU_CONF_DIR
CONF_ROOT="weekly"
# the email subject for report emails - to have date added
EMAIL_SUBJ="Avatar Weekly Backup Report"
# the email address (or addresses with comma separation) to send it to
EMAIL=dlane@egressive.com
#
# directories...
EBU_DIR=/etc/ebu
EBU_CMD=$EBU_DIR/ebu
EBU_CONF_DIR=$EBU_DIR/conf
LOGS=/var/log
LOG=$LOGS/ebu.log
# determine today's date
DATE=`date   '+%Y-%m-%d-%a'`
# determine today's date
TIME=`date   '+%H-%M-%S'`
# email program
MAIL=/bin/mail
# a timestamp for logging purposes
TIMESTAMP=`date  '+%Y-%m-%d %H:%M.%S'`
# temporary holding point for email
TMP_EMAIL=/tmp/tmp_email_$DATE.$TIME
#  build list of backup configuration files
CONFS=`ls -1 $EBU_CONF_DIR/$CONF_ROOT*.conf`
#
# function to direct a message...
message() {
  echo "$0: $TIMESTAMP $@" >> $LOG
  if test -f $TMP_EMAIL ; then
    echo "$0: $TIMESTAMP $@" >> $TMP_EMAIL
  fi
}
#
# create the temporary email file
create_tmp_email() {
  touch $TMP_EMAIL
  if test -f $TMP_EMAIL ; then
    message "created temporary email  $TMP_EMAIL"
  else
    message "failed to create temporary email  $TMP_EMAIL"
  fi
}
#
# send the contents of the temporary file to the
# designated report recipient
send_email_report() {
  if test -f $TMP_EMAIL ; then
    message "sending email report to $EMAIL"
    $MAIL -s "$EMAIL_SUBJ" $EMAIL < $TMP_EMAIL
    rm $TMP_EMAIL
    if test -f $TMP_EMAIL ; then
      message "failed to remove temporary email  $TMP_EMAIL"
    else
      message "successfully removed temporary email  $TMP_EMAIL"
    fi
    message "email report successfully sent"
  fi
}
#
# insert a blank line into the log and on the console
insert_blank() {
  echo "" >> $TMP_EMAIL
}
#
#  The Functional Part of the Script
#
#  build the email for the report
create_tmp_email
#  run backup for each file
for CONF in $CONFS
do
  $EBU_CMD -b -c $CONF -e $TMP_EMAIL
  # put a space in the email to separate tasks
  insert_blank
done
#
#  send the resulting email
send_email_report