diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000000000000000000000000000000000000..4973127f18f2df069e73d3d146ea2a0c6a689776 --- /dev/null +++ b/backup.sh @@ -0,0 +1,213 @@ +#!/bin/bash +# ================================================================================ +# +# START BACKUP +# +# 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 +# backup.sh [option] +# +# OPTIONS +# default: no parameter: start an incremental backup +# -f, --full force a full backup +# -d, --dryrun check date only; no run of a backup +# +# -------------------------------------------------------------------------------- +# 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 + JOB_DOTODAY=1 + JOBDEF_TYPE=auto + JOBDEF_AUTO=$2 + bStart=1 + shift 1 + fi + + ;; + -f|--full) + echo "Param: setting to FULL" + JOB_DOTODAY=1 + JOBDEF_TYPE=full + JOBDEF_AUTO= + bStart=1 + ;; + -i|--inc) + echo "Param: setting to INC" + JOB_DOTODAY=1 + JOBDEF_TYPE=inc + JOBDEF_AUTO= + bStart=1 + ;; + *) + echo "ERROR: parameter [$1] is unknown" + showHelp + exit 1 + esac + shift 1 + + done + JOBDEF_STARTTIME=$(date +%H%M) + STORAGE_BIN=$( _j_getvar "${STORAGEFILE}" "bin" ) + + cat << EOFbackupinfo + + DEPRECATED: execution times: $JOBDEF_INC at `_j_getvar ${JOBFILE} "start-time-inc"` + DEPRECATED: Full backup : $JOBDEF_FULL at `_j_getvar ${JOBFILE} "start-time-full"` + + DEPRECATED: do today = $JOB_DOTODAY + + Used Backup tool: $STORAGE_BIN + type = $JOBDEF_TYPE $JOBDEF_AUTO + + starttime = $JOBDEF_STARTTIME + donefile = $JOB_DONEFILE + +EOFbackupinfo + + j_requireUser "root" + +echo "DEBUG $0 ... ABORT"; exit 1 + +# -------------------------------------------------------------------------------- + + 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 + 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 + + +# -------------------------------------------------------------------------------- diff --git a/backup.sh.banner b/backup.sh.banner new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391