borg-backup/borg-list-backups.sh

298 lines
6.1 KiB
Bash
Executable File

#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
if [[ -f "${working_dir}/conf/borg-backup-nc.conf" ]] ; then
conf_file="${working_dir}/conf/borg-backup-nc.conf"
else
conf_file="${working_dir}/conf/borg-backup.conf"
fi
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
backup_date="$(date +%Y-%m-%d-%H%M)"
# ----------
# Base Function(s)
# ----------
clean_up() {
# Perform program exit housekeeping
blank_line
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; then
rm -rf "$LOCK_DIR"
exit $1
fi
}
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 [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; then
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
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; 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
fi
# ----------
# Default values
# ----------
DEFAULT_SSH_USER="borg"
DEFAULT_SSH_PORT=22
DEFAULT_BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup'
# ----------
# Read Configurations from $conf_file
# ----------
if [[ -z "${BORG_HOST}" ]] || [[ -z "${BORG_REPO}" ]] || [[ -z "${BORG_PASSPHRASE}" ]] ; then
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
fatal "No configuration file '$conf_file' present!"
fi
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}"
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
# List Borg Backups
#
echo -e " \033[1mDate\033[33GBorg Backup ID\033[m\n"
if [[ ${BORG_HOST} =~ ^127 ]] || [[ "${BORG_HOST,,}" =~ "localhost" ]] ; then
while read -r _borg_id _date_weekday _date_date _date_time _time_stamp_borg_backup || [[ -n $_line ]] ; do
echo -e " ${_date_weekday} ${_date_date} ${_date_time} [ \033[33m${_borg_id}\033[m ]"
done < <(borg list ${BORG_REPO} 2> /dev/null)
else
while read -r _borg_id _date_weekday _date_date _date_time _time_stamp_borg_backup || [[ -n $_line ]] ; do
echo -e " ${_date_weekday} ${_date_date} ${_date_time} [ \033[33m${_borg_id}\033[m ]"
done < <(borg list ${BORG_URL} 2> /dev/null)
fi
clean_up 0