Skip to content
Snippets Groups Projects
backup.sh 7.97 KiB
#!/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
# 2022-02-10  ah  v1.1  handle local dumps only; improve infos
# 2022-11-04  ah  v1.2  rename hooks
# ================================================================================

. $( dirname "$0" )/jobhelper.sh
. `dirname $0`/inc_bash.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 duplicity)

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 
    -o, --older   duplicity type=auto: repeat full backup if last full 
                  backup is older than ... like "1M" for 1 month
    -f, --full    force full backup (if supported)
    -i, --inc     force incrmenental backup

EOFhelp
}

# --------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------

  # --- read backup config data
  j_read

  # --- handle params
  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"
            JOBDEF_TYPE=auto
            bStart=1
            ;;
        -o|--older)
            echo "Param: setting to AUTO on duplicity"
            echo "$2" | grep "^[1-9][0-9]*[a-zA-Z]$" 2>/dev/null
            if [ $? -ne 0 ]; then
                >&2 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
                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
            ;;
        *)
            >&2 echo "ERROR: parameter [$1] is unknown"
            showHelp
            exit 1 
    esac
    shift 1

  done

  # --- update logfile after param overrides
  _j_setLogfile

# --------------------------------------------------------------------------------
# show infos
# --------------------------------------------------------------------------------

  # TODO:
  # check localdump plugins ... what database types are detected

  # --- infos for transfer


  STORAGE_BASEDIR=$(_j_getvar "${STORAGEFILE}" "storage")

  if [ -z "$STORAGE_BASEDIR" ]; then
    echo "INFO: no value for backup target (in storage = ...)"
    echo "This is not handled as an error. I will run local backup dumps only; no transfer."
  else
    STORAGE_BIN=$( _j_getvar "${STORAGEFILE}" "bin" )
    cfg_type=$(_j_getvar "${STORAGEFILE}" "type")
    cfg_full=$(_j_getvar "${STORAGEFILE}" "full")
    cfg_startfull=$(_j_getvar "${STORAGEFILE}" "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)

    cat << EOFbackupinfo

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

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

      target           : $( echo ${STORAGE_BASEDIR} | sed 's#:[^:]*@#:**********@#' )

EOFbackupinfo
    test -z "$STORAGE_BASEDIR" || (
      echo "      dirs to transfer : "
      j_getDirs2Backup | sed "s#^#                         #g"
    )
  fi | tee -a "$JOB_LOGFILE"
  echo

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

  j_requireUser "root"

  if [ $bStart -eq 1 ]; then

    j_notify "Start" "Starting a backup run..."
    _j_runHooks "100-before-backup"

    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=$?
    test $rcBackup -gt 0 && j_notify "db dumps" "rc=$rcBackup" $rcBackup

    echo "INFO: $(date) - local backups were finished" | tee -a "$JOB_LOGFILE"
    echo
    sleep 2

    # ------------------------------------------------------------
    # transfer
    # ------------------------------------------------------------

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

      # transfer files
      # cat "$STORAGEFILE" >>"$JOB_LOGFILE"
      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"
      "${DIR_SELF}"/transfer.sh $JOBDEF_TYPE "$JOBDEF_AUTO" 2>&1
      rcTransfer=$?

    else
      echo                                                         | tee -a "$JOB_LOGFILE"
      echo "SKIP TRANSFER - No backup of dirs to a backup target." | tee -a "$JOB_LOGFILE"
      echo                                                         | tee -a "$JOB_LOGFILE"
    fi

    rcBackup=$rcBackup+rcTransfer

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

    # ------------------------------------------------------------
    # cleanup
    # ------------------------------------------------------------

    echo
    echo "INFO: $(date) - compress older logs"
    echo find "${DIR_LOGS}" -name "*.log" -mtime +2 -print -exec gzip {} \;
    color cmd 
    find "${DIR_LOGS}" -name "*.log" -mtime +2 -print -exec gzip {} \;
    color reset
    echo

    echo "INFO: $(date) - cleanup logs"
    echo find "${DIR_LOGS}" -mtime +28 -delete -print
    color cmd
    find "${DIR_LOGS}" -mtime +28 -delete -print
    color reset
    echo

    # ------------------------------------------------------------
    # finish
    # ------------------------------------------------------------

    echo STATUS $0 exit with final returncode rc=$rcBackup | tee -a "$JOB_LOGFILE"
    echo
    echo "log for this executed job is"
    ls -l "$JOB_LOGFILE"
    echo

  else
    echo "Nothing to do."
  fi

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


  j_notify "Done" "Backup was finished. rc=$rcBackup. See log for details: $JOB_LOGFILE" $rcBackup
  exit $rcBackup


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