#!/bin/bash
# ================================================================================
#
# SCHEDULER
#
#   schedule backups.
#   If backup time is reached then
#     - the local dumps for all database types will be performed
#     - transfer of data directories will be started
#
#  This script is added as cron (see /etc/cron.d/)
#
# SYNTAX
#   scheduler.sh [option]
#
# OPTIONS
#   default: no parameter: check jobs/backups.job for scheduling backups
#   -d, --dryrun  check date only; no run of a backup
#   -f, --force:  force incremental backup NOW.
#
# --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch>
# ds - Daniel Schueler <daniel.schueler@iml.unibe.ch>
#
# 2016-11-10  ah,ds  v1.0
# ================================================================================

. `dirname $0`/jobhelper.sh


  typeset -i rcScheduler=0
  typeset -i rcTransfer=0
  bStart=1


# --------------------------------------------------------------------------------
# checks
# --------------------------------------------------------------------------------

  j_requireUser "root"

  # --- read schedulerdata
  j_read


  cat << EOFschedulerinfo

    execution times: $JOBDEF_INC at `_j_getvar ${JOBFILE} "start-time-inc"`
    Full backup    : $JOBDEF_FULL at `_j_getvar ${JOBFILE} "start-time-full"`

    do today = $JOB_DOTODAY

    type = $JOBDEF_TYPE $JOBDEF_AUTO

    starttime = $JOBDEF_STARTTIME
    stopfile = $JOB_DONEFILE

EOFschedulerinfo

  if [ $JOB_DOTODAY -eq 0 ]; then
    echo "ABORT: nothing to do today"
    bStart=0
  fi


  # check: Backup bereits gelaufen?
  ls -l ${JOB_DONEFILE}.* 2>/dev/null
  if [ $? -eq 0 ]; then
    echo
    echo "ABORT: $JOB_DONEFILE was found"
    bStart=0
  else
    echo "Job was not started yet."
  fi


  if [ ${JOBDEF_STARTTIME} -gt ${mytime} ]; then
    echo "INFO: waiting for next run at ${JOBDEF_STARTTIME} ..."
    bStart=0
  fi

  if [ "$1" = "-f" -o  "$1" = "--force" ]; then
    echo FORCE parameter detected.
    echo Overriding settings to make an incremental backup now.

    JOB_DOTODAY=1
    JOBDEF_TYPE=inc
    JOBDEF_STARTTIME=`date +%H%M`
    bStart=1

  fi

  if [ "$1" = "-d" -o  "$1" = "--dryrun" ]; then
     echo DRYRUN parameter detected
     bStart=0
  fi

# --------------------------------------------------------------------------------

  if [ $bStart -eq 1 ]; then

    # ----- local dumps

    echo "INFO: `date` - starting backup ... type $JOBDEF_TYPE - time $JOBDEF_STARTTIME " | tee -a $JOB_LOGFILE
    touch "${JOB_DONEFILE}.01.START"
    cat $JOBFILE >>$JOB_LOGFILE


    echo "INFO: `date` - Making local backups ... ${DIR_SELF}/localdump.sh ALL" | tee -a $JOB_LOGFILE
    ${DIR_SELF}/localdump.sh ALL | tee -a $JOB_LOGFILE
    rcScheduler=$?
    echo "INFO: `date` - local backups were finished" | tee -a $JOB_LOGFILE

    sleep 2

    # ----- transfer

    if [ -x "${DIR_SELF}/transfer.sh" ]; then

      # transfer files
      echo "INFO: `date` - Sending data to storage... ${DIR_SELF}/transfer.sh $JOBDEF_TYPE" | tee -a $JOB_LOGFILE
      touch "${JOB_DONEFILE}.02.STARTTRANSFER"
      ${DIR_SELF}/transfer.sh $JOBDEF_TYPE $JOBDEF_AUTO 2>&1 | tee -a $JOB_LOGFILE
      rcTransfer=$?

    else
      echo "SKIP - transfer.sh not found; all files will be kept on local filesystem only" | tee -a $JOB_LOGFILE
    fi

    rcScheduler=$rcScheduler+rcTransfer


    echo "INFO: `date` - DONE" | tee -a $JOB_LOGFILE
    touch "${JOB_DONEFILE}.03.DONE"

    echo
    echo log for this executed job is
    ls -l $JOB_LOGFILE
    echo

    echo "INFO: `date` - cleanup logs"
    echo find "${DIR_LOGS}" -mtime +28 -delete -print
    find "${DIR_LOGS}" -mtime +28 -delete -print
    echo

    echo STATUS $0 exit with final returncode rc=$rcScheduler | tee -a $JOB_LOGFILE
    echo

  else
    echo "Nothing to do."
  fi

# --------------------------------------------------------------------------------


  exit $rcScheduler


# --------------------------------------------------------------------------------
