#!/bin/bash
# ================================================================================
#
# START BACKUP
#
# For a backup will be done:
#   - local dumps for all database types will be performed
#   - transfer of data directories will be started
#
# This script should be added as cronjob (see /etc/cron.d/)
#
# --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch>
#
# 2022-01-14  ah  v1.0
# ================================================================================

. $( dirname "$0" )/jobhelper.sh


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


# --------------------------------------------------------------------------------
# functions
# --------------------------------------------------------------------------------

function showHelp(){
    self=$(basename "$0")
    cat << EOFhelp

START A BACKUP

This script starts 
  - the database backups and creates dumps locally
  - the file backup (using restic or dupliciity)

SYNTAX:

    $self [OPTIONS]

    default: no parameter:

OPTIONS:

    -d, --dryrun  just show infos, do not start a backup
    -h, --help    Show this help

    The following parameters force the backup type (if using duplicity):
    -a, --auto    force auto (repeat full backup if last full backup is older than ...)
                  This parameter requires a value like "1M" for 1 month
    -f, --full    force full backup (if supported)
    -i, --inc     force incrmenental backup

EOFhelp
}

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



  # --- read schedulerdata
  j_read


while [ "$#" -gt 0 ]; 
do

    case "$1" in
        -h|-\?|--help)
            echo "Param: show help"
            showHelp
            exit 0
            ;;

        -d|--dryrun)
            echo "Param: setting to DRY RUN"
            bStart=0
            ;;
        -a|--auto)
            echo "Param: setting to AUTO $2"
            echo "$2" | grep "^[1-9][0-9]*[a-zA-Z]$" 2>/dev/null
            if [ $? -ne 0 ]; then
                echo "ERROR: the value after $1 must be a number and a letter (for unit); i.e. 1M for 1 month"
                exit 1
            else
                JOBDEF_TYPE=auto
                JOBDEF_AUTO=$2
                bStart=1
                shift 1
            fi

            ;;
        -f|--full)
            echo "Param: setting to FULL"
            JOBDEF_TYPE=full
            JOBDEF_AUTO=
            bStart=1
            ;;
        -i|--inc)
            echo "Param: setting to INC"
            JOBDEF_TYPE=inc
            JOBDEF_AUTO=
            bStart=1
            ;;
        *)
            echo "ERROR: parameter [$1] is unknown"
            showHelp
            exit 1 
    esac
    shift 1

  done

  # update logfile after param overrides
  _j_setLogfile

  # show infos
  cfg_type=$(_j_getvar "${JOBFILE}" "type")
  cfg_full=$(_j_getvar "${JOBFILE}" "full")
  cfg_startfull=$(_j_getvar "${JOBFILE}" "start-time-full")
  
  test -z "${cfg_full}${cfg_startfull}" || cfg_info="INFO: Type is [auto]; ignoring config for full backup: [full = $cfg_full ... start-time-full = $cfg_startfull]"

  JOBDEF_STARTTIME=$(date +%H%M)
  STORAGE_BIN=$( _j_getvar "${STORAGEFILE}" "bin" )

  cat << EOFbackupinfo

    CONFIG:
    Used Backup tool : $STORAGE_BIN
    Backup type      : $(_j_getvar ${JOBFILE} "type")
      $( test "$cfg_type" = "auto" && echo "$cfg_info" )$( test "$cfg_type" = "auto" || echo "INFO: full backup: $cfg_info" )

    EFFECTIVE:
    type             : $JOBDEF_TYPE $JOBDEF_AUTO
    donefile         : $JOB_DONEFILE
    log              : $JOB_LOGFILE

EOFbackupinfo

  j_requireUser "root"

# --------------------------------------------------------------------------------
# start backup
# --------------------------------------------------------------------------------

  if [ $bStart -eq 1 ]; then

    sleep 3

    # ----- 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"
    rcBackup=$?
    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

    rcBackup=$rcBackup+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=$rcBackup | tee -a "$JOB_LOGFILE"
    echo

  else
    echo "Nothing to do."
  fi

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


  exit $rcBackup


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