#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" if [[ -f "${working_dir}/conf/borg2-backup-nc.conf" ]] ; then conf_file="${working_dir}/conf/borg2-backup-nc.conf" else conf_file="${working_dir}/conf/borg2-backup.conf" fi LOCK_DIR="/tmp/${script_name%%.*}.LOCK" log_file="${LOCK_DIR}/${script_name%%.*}.log" # ---------- # Base Function(s) # ---------- clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" echo "" echo -e " \033[31m\033[1mScript was interupted\033[m!" else echo " [ Fatal ]: $*" echo "" echo " Script was terminated...." fi echo "" clean_up 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ]: $*" else echo "[ Error ]: $*" fi } warn (){ echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" else echo "[ Warning ]: $*" fi echo "" } warn_only_terminal () { if $terminal ; then echo "" echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" echo "" fi } info (){ if $terminal ; then echo "" echo -e " [ \033[32m\033[1mInfo\033[m ] $*" echo "" fi } ok (){ if $terminal ; then echo "" echo -e " [ \033[32m\033[1mOk\033[m ] $*" echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ]" fi } echo_warn() { if $terminal ; then echo -e "\033[75G[ \033[33mwarn\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]" fi } echo_wait(){ if $terminal ; then echo -en "\033[75G[ \033[5m\033[1m...\033[m ]" fi } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } blank_line() { if $terminal ; then echo "" fi } # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ------------- # - Job is already running? # ------------- # - If job already runs, stop execution.. # - if mkdir "$LOCK_DIR" 2> /dev/null ; then ## - Remove lockdir when the script finishes, or when it receives a signal trap "clean_up 1" SIGHUP SIGINT SIGTERM else datum="$(date +"%d.%m.%Y %H:%M")" msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.." echo "" echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running." echo "" echo -e " Exiting now.." echo "" for _email in ${alert_email_arr[@]} ; do echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \ | sendmail -F "Error `hostname -f`" -f $sender_address $_email done exit 1 fi # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" echo "" fi # ---------- # Default values # ---------- DEFAULT_SSH_USER="borg" DEFAULT_SSH_PORT=22 DEFAULT_BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup' DEFAULT_BORG_FUSE_MOUNT_DIR="/mnt/borg-fuse-mount" # ---------- # Read Configurations from $conf_file # ---------- if [[ -f "$conf_file" ]]; then source "$conf_file" else fatal "No configuration file '$conf_file' present!" fi # ---------- # Set Parameter values # ---------- #if [[ -z "${BORG_PASSPHRASE}" ]] ; then # fatal "Borg Passphrase (BORG_PASSPHRASE) NOT found!" #fi if [[ -z "${BORG_HOST}" ]] ; then fatal "Backup Hosth (var BORG_HOST) not given!" fi if [[ -z "${BORG_REPO}" ]] ; then fatal "Borg Repository Path (var BORG_REPO) not given!" fi [[ -z "${HTTP_USER}" ]] && HTTP_USER="${DEFAULT_HTTP_USER}" [[ -z "${SSH_USER}" ]] && SSH_USER="${DEFAULT_SSH_USER}" [[ -z "${SSH_PORT}" ]] && SSH_PORT="${DEFAULT_SSH_PORT}" [[ -z "${BORG_RSH}" ]] && BORG_RSH="${DEFAULT_BORG_RSH}" [[ -z "${BORG_FUSE_MOUNT_DIR}" ]] && BORG_FUSE_MOUNT_DIR="${DEFAULT_BORG_FUSE_MOUNT_DIR}" BORG_URL="ssh://${SSH_USER}@${BORG_HOST}:${SSH_PORT}${BORG_REPO}" export BORG_PASSPHRASE export BORG_REPO export BORG_RSH # --- # Some automatic “answerers” (if set, they automatically answer confirmation questions): # --- # For 'Warning: The repository at location … was previously located at ..' # BORG_RELOCATED_REPO_ACCESS_IS_OK=yes export BORG_RELOCATED_REPO_ACCESS_IS_OK echononl "Create Restore Directory '${BORG_FUSE_MOUNT_DIR}'." if [ ! -d "${BORG_FUSE_MOUNT_DIR}" ]; then mkdir -p "${BORG_FUSE_MOUNT_DIR}" > ${log_file} 2>&1 if [[ $? -gt 0 ]] ; then echo_failed error "$(cat "${log_file}")" fatal "No restore directory '${BORG_FUSE_MOUNT_DIR}' present!" else echo_ok fi else echo_skipped fi blank_line # List Borg Backups # ( source /root/bin/borg2-backup/borg2-list-backups.sh ) echo "" echo -e " \033[33m--------------------------------------------\033[m" echo " Which Borg Archiv shoulid be cedeleted?" echo "" echo " Give a Borg Backup ID from above list" echo "" echo -n " Borg Backup ID:: " read BORG_BACKUP_ID echo -e " \033[33m--------------------------------------------\033[m" echo "" echononl "Delete archive '${BORG_REPO}::${BORG_BACKUP_ID}''.." borg2 --remote-path borg2 --repo ${BORG_URL} delete -a ${BORG_BACKUP_ID} >> "${log_file}" 2>&1 if [[ $? -gt 0 ]] ; then echo_failed error "$(cat "${log_file}")" else echo_ok fi clean_up 0