first commit

This commit is contained in:
Christoph 2024-07-02 00:36:18 +02:00
commit 308afabc03
8 changed files with 1810 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/BAK/*
*.log
/conf/*.conf
*.swp

View File

@ -0,0 +1,9 @@
# --------------------
# Borg Documentation:
#
# see: https://borgbackup.readthedocs.io/en/stable/
#
# Borg Backup Home:
#
# see: https://www.borgbackup.org/
# --------------------

136
README.prerequisites Normal file
View File

@ -0,0 +1,136 @@
# ==========
# Preparation / Prerequisites
# ==========
BORG_HOST="o26.oopen.de"
BORG_REPO="/backup/cl-fm"
BORG_PASSPHRASE='wweK/m.xV-g3oI-7WM/pejTP'
SSH_USER="borg"
SSH_PORT=22
SSH_IDENTITY_FILE="/root/.ssh/id_ed25519-borg-backup"
export BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup'
export SSH_USER
export BORG_PASSPHRASE
# ---
# see:
#
# https://www.c-rieger.de/nextcloud-borg-backup-zur-hetzner-storage-box
# https://borgbackup.readthedocs.io/en/stable/quickstart.html
# ---
# ==========
# Preparations Server
# ==========
# preparation on the backup server (o26.oopen.de)
# ===============================================
#
# 1. On Backup Server create a user which is used to push the backups to the server
#
# backup-user: borg
# backup-group: borg
#
#
# 2 Create a backup repository:
#
# client-identifier: cl-fm
#
# mkdir -p /backup/<client-identifier>
#
#
# 3. Backup user must have full write permissions to the backup repostitories
#
# chown <backup-user>:<backup-group> /data/backup/<client-identifier>
#
# 4. Prevent repository directory from being deleted
#
# chattr +i /data/backup/<client-identifier>
#
mkdir -p "${BORG_REPO}"
chown ${SSH_USER}:${SSH_USER} "${BORG_REPO}"
chattr +i "$(dirname "${BORG_REPO}")"
# ==========
# Preparations Client
# ==========
# preparation on the backup client (cl-fm.oopen.de
# ================================================
#
# 1. Install Borg Backup (on the client) using 'apt install'
#
# apt install -y -t stable-backports borgbackup python3-llfuse
#
#
# 2. Generate a SSH Key (as root without passphrase) to connect to the backup server:
#
# ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519-borg-backup
#
# Note:
# Dont't forget pusching the key to the servers authorized_key file of
# the remote backup user
#
#
# 3. create file /root/.ssh/config ti store ssh connection parameters
#
# BORG_HOST="o26.oopen.de"
# SSH_USER="borg"
# SSH_IDENTITY_FILE="/root/.ssh/id_ed25519-borg-backup"
#
# cat << EOF > /root/.ssh/config
# host ${BORG_HOST}
# User ${SSH_USER}
# IdentityFile ${SSH_IDENTITY_FILE}
# StrictHostKeyChecking no
# LogLevel FATAL
# EOF
#
apt install -y -t stable-backports borgbackup python3-llfuse
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519-borg-backup
cat << EOF > /root/.ssh/config
host ${BORG_HOST}
User ${SSH_USER}
IdentityFile ${SSH_IDENTITY_FILE}
StrictHostKeyChecking no
LogLevel FATAL
EOF
# Initialize backup
# =================
#
# export SSH_USER="borg"
# export BORG_HOST="o26.oopen.de"
# export SSH_PORT="22"
# export BORG_PASSPHRASE="wweK/m.xV-g3oI-7WM/pejTP"
#
# export BORG_REPO="/data/backup/cl-fm"
#
# borg init --encryption=repokey ssh://${SSH_USER}@${BORG_HOST}:${SSH_PORT}${BACKUP_REPOSITORY}
#
# Outpu of of borg initializing was:
#
# By default repositories initialized with this version will produce security
# errors if written to with an older version (up to and including Borg 1.0.8).
#
# If you want to use these older versions, you can disable the check by running:
# borg upgrade --disable-tam ssh://borg@o26.oopen.de:22//data/backup/cl-fm
#
# See https://borgbackup.readthedocs.io/en/stable/changes.html#pre-1-0-9-manifest-spoofing-vulnerability
# for details about the security implications.
#
# IMPORTANT: you will need both KEY AND PASSPHRASE to access this repo!
# If you used a repokey mode, the key is stored in the repo, but you should back it up separately.
# Use "borg key export" to export the key, optionally in printable format.
# Write down the passphrase. Store both at safe place(s).
#
borg init --encryption=repokey ssh://${SSH_USER}@${BORG_HOST}:${SSH_PORT}${BORG_REPO}

654
borg-backup-nc.sh Executable file
View File

@ -0,0 +1,654 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
backup_date="$(date +%Y-%m-%d-%H%M)"
startTime=$(date +%s)
currentDate=$(date --date @"$startTime" +"%Y%m%d_%H%M%S")
currentDateReadable=$(date --date @"$startTime" +"%d.%m.%Y - %H:%M:%S")
# ----------
# Base Function(s)
# ----------
clean_up() {
# Perform program exit housekeeping
sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ maintenance:mode --off > /dev/null 2>&1
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
if [ -d "${BORG_LOG_DIR}" ]; then
echo "" >> ${BORG_LOG_FILE}
echo -e "# $*" >> ${BORG_LOG_FILE}
echo "#" >> ${BORG_LOG_FILE}
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
if [ -d "${BORG_LOG_DIR}" ]; then
echo "[ Error ]: $*" >> ${BORG_LOG_FILE}
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
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
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_HTTP_USER="www-data"
DEFAULT_BORG_LOG_DIR="/var/log/borg/"
DEFAULT_SSH_USER="borg"
DEFAULT_SSH_PORT=22
DEFAULT_BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup'
DEFAULT_NC_DB_BACKUP_DIR="/data/backup/sql"
DEFAULT_BACKUP_OTHER_DIRECTORIES="/etc /home /root /var/spool/cron"
# ----------
# 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 "${NC_PATH}" ]] ; then
fatal "Nextcloud's DocumentRoot directory 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_LOG_DIR}" ]] && BORG_LOG_DIR="${DEFAULT_BORG_LOG_DIR}"
BORG_LOG_FILE="${BORG_LOG_DIR}/${backup_date}.log"
[[ -z "${NC_DB_BACKUP_DIR}" ]] && NC_DB_BACKUP_DIR="${DEFAULT_NC_DB_BACKUP_DIR}"
[[ -z "${BACKUP_OTHER_DIRECTORIES}" ]] && BACKUP_OTHER_DIRECTORIES="${DEFAULT_BACKUP_OTHER_DIRECTORIES}"
BORG_URL="ssh://${SSH_USER}@${BORG_HOST}:${SSH_PORT}${BORG_REPO}"
export BORG_PASSPHRASE
export BORG_REPO
# ---
# Some automatic 'answerers' (if set, they automatically answer confirmation questions):
# ---
# For 'Warning: The repository at location … was previously located at ..'
#
# This has to be set when the repository has been created by user and
# the script is called by cron
#
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
# For "Warning: The repository at location ... was previously located at ..."
#
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
PHP_COMMAND="$(which php)"
if [[ -z "${PHP_COMMAND}" ]] ; then
if [[ -f "/usr/local/php/bin/php" ]] ; then
PHP_COMMAND="/usr/local/php/bin/php"
fi
fi
if [[ -z "${PHP_COMMAND}" ]] ; then
fatal "No PHP command found!"
fi
if [ ! -d "${BORG_LOG_DIR}" ]; then
echononl "Create borg log directory.."
mkdir -p "${BORG_LOG_DIR}" > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "No log directory '${BORG_LOG_DIR}' present!"
else
echo_ok
echo "mkdir -p \"${BORG_LOG_DIR}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
fi
fi
if [ ! -d "${NC_DB_BACKUP_DIR}" ]; then
echononl "Create DB backup directory.."
mkdir -p "${NC_DB_BACKUP_DIR}" > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "No log directory '${NC_DB_BACKUP_DIR}' present!"
else
echo_ok
echo "mkdir -p \"${NC_DB_BACKUP_DIR}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
fi
fi
echo "# ----------" >> ${BORG_LOG_FILE}
echo -e "# Running script '${script_name}' on $(date +%Y.%m.%d) at $(date +%H:%M) h .." >> ${BORG_LOG_FILE}
echo "# ----------" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
echo "# Script parameter settings:" >> ${BORG_LOG_FILE}
echo "#" >> ${BORG_LOG_FILE}
echo "HTTP_USER=\"${HTTP_USER}\"" >> ${BORG_LOG_FILE}
echo "SSH_USER=\"${SSH_USER}\"" >> ${BORG_LOG_FILE}
echo "SSH_PORT=\"${SSH_PORT}\"" >> ${BORG_LOG_FILE}
echo "BORG_HOST=\"${BORG_HOST}\"" >> ${BORG_LOG_FILE}
echo "BORG_REPO=\"${BORG_REPO}\"" >> ${BORG_LOG_FILE}
echo "BORG_PASSPHRASE='${BORG_PASSPHRASE}'" >> ${BORG_LOG_FILE}
echo "BORG_RSH=\"${BORG_RSH}\"" >> ${BORG_LOG_FILE}
echo "BORG_LOG_DIR=\"${BORG_LOG_DIR}\"" >> ${BORG_LOG_FILE}
echo "BORG_URL=\"${BORG_URL}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
echo "NC_PATH=\"${NC_PATH}\"" >> ${BORG_LOG_FILE}
echo "NC_DB_BACKUP_DIR=\"${NC_DB_BACKUP_DIR}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
echo "PHP_COMMAND=\"${PHP_COMMAND}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
echo "# ---" >> ${BORG_LOG_FILE}
echo "# Gather some NC settings.." >> ${BORG_LOG_FILE}
echo "# ---" >> ${BORG_LOG_FILE}
# Gather NC settings
#
echononl "Get NextCloud data directory .."
cat << EOF >> ${BORG_LOG_FILE}
NC_DATA_DIR="\$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get datadirectory)"
EOF
NC_DATA_DIR="$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get datadirectory 2> "${log_file}")"
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "Cannot determin NextCloud data directory!"
else
echo_ok
fi
echononl "Get NextCloud DB name .."
cat << EOF >> ${BORG_LOG_FILE}
NC_DB="\$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbname)"
EOF
NC_DB="$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbname 2> "${log_file}")"
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "Cannot determin NextCloud DB name!"
else
echo_ok
fi
echononl "Get NextCloud DB user .."
cat << EOF >> ${BORG_LOG_FILE}
NC_DB_USER="\$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbuser)"
EOF
NC_DB_USER="$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbuser 2> "${log_file}")"
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "Cannot determin NextCloud DB user!"
else
echo_ok
fi
echononl "Get NextCloud DB password .."
cat << EOF >> ${BORG_LOG_FILE}
NC_DB_PASS="\$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbpassword)"
EOF
NC_DB_PASS="$(sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ config:system:get dbpassword 2> "${log_file}")"
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
fatal "Cannot determin NextCloud DB password!"
else
echo_ok
fi
echo "" >> ${BORG_LOG_FILE}
echo "NC_DATA_DIR=\"${NC_DATA_DIR}\"" >> ${BORG_LOG_FILE}
echo "NC_DB=\"${NC_DB}\"" >> ${BORG_LOG_FILE}
echo "NC_DB_USER=\"${NC_DB_USER}\"" >> ${BORG_LOG_FILE}
echo "NC_DB_PASS=\"${NC_DB_PASS}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
blank_line
# Set maintance mode on
#
echononl "Set maintance mode of NC to 'on'."
echo "sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ maintenance:mode --on" >> ${BORG_LOG_FILE}
sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ maintenance:mode --on > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
blank_line
# Create database backup
#
echononl "Create database backup .."
cat << EOF >> ${BORG_LOG_FILE}
mysqldump --single-transaction --routines \\
-h localhost \\
-u${NC_DB_USER} \\
-p${NC_DB_PASS} \\
-e ${NC_DB} > ${NC_DB_BACKUP_DIR}/${NC_DB}-${backup_date}.sql
EOF
mysqldump --single-transaction --routines \
-h localhost \
-u${NC_DB_USER} \
-p${NC_DB_PASS} \
-e ${NC_DB} > ${NC_DB_BACKUP_DIR}/${NC_DB}-${backup_date}.sql 2> ${log_file}
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
echononl "Create borg backup.."
cat << EOF >> ${BORG_LOG_FILE}
borg create \\
--verbose \\
--filter AME \\
--list \\
--stats \\
--show-rc \\
--compression lz4 \\
--exclude-caches \\
--exclude 'home/*/.cache/*' \\
--exclude '/root/.cache/*' \\
--exclude '/root/.config/*' \\
--exclude '/root/.local/*' \\
--exclude 'var/tmp/*' \\
\\
${BORG_URL}::${backup_date} \\
${NC_DB_BACKUP_DIR} \\
${NC_PATH} \\
${NC_DATA_DIR} \\
${BACKUP_OTHER_DIRECTORIES}
EOF
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--exclude 'home/*/.cache/*' \
--exclude '/root/.cache/*' \
--exclude '/root/.config/*' \
--exclude '/root/.local/*' \
--exclude 'var/tmp/*' \
\
${BORG_URL}::${backup_date} \
${NC_DB_BACKUP_DIR} \
${NC_PATH} \
${NC_DATA_DIR} \
${BACKUP_OTHER_DIRECTORIES} \
> ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
cat ${log_file} >> ${BORG_LOG_FILE}
fi
echononl "Pruning repository.."
cat << EOF >> ${BORG_LOG_FILE}
borg prune \\
--list \\
--show-rc \\
--keep-daily 7 \\
--keep-weekly 4 \\
--keep-monthly 6 \\
--keep-yearly 1 \\
${BORG_URL}
EOF
borg prune \
--list \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--keep-yearly 1 \
${BORG_URL} \
> ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
cat ${log_file} >> ${BORG_LOG_FILE}
fi
blank_line
# Set maintance mode off
#
echononl "Set maintance mode of NC to 'off'."
echo "sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ maintenance:mode --off" >> ${BORG_LOG_FILE}
sudo -u ${HTTP_USER} ${PHP_COMMAND} ${NC_PATH}/occ maintenance:mode --off > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
# Remove DB local backup
#
echononl "Remove local DB backup.."
echo "rm ${NC_DB_BACKUP_DIR}/${NC_DB}-${backup_date}.sql" >> ${BORG_LOG_FILE}
rm ${NC_DB_BACKUP_DIR}/${NC_DB}-${backup_date}.sql > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
# Duration of backup
endTime=$(date +%s)
endDateReadable=$(date --date @"$endTime" +"%d.%m.%Y - %H:%M:%S")
duration=$((endTime-startTime))
durationSec=$((duration % 60))
durationMin=$(((duration / 60) % 60))
durationHour=$((duration / 3600))
durationReadable=$(printf "%02d hours %02d minutes %02d seconds" $durationHour $durationMin $durationSec)
echo -e "\n\n###### Server backup finished on ${endDateReadable} (${durationReadable}) ######\n" >> ${BORG_LOG_FILE}
clean_up 0

283
borg-list-backups.sh Executable file
View File

@ -0,0 +1,283 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/borg-backup-nc.conf"
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 [[ -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}"
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"
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})
clean_up 0

315
borg-mount-archiv.sh Executable file
View File

@ -0,0 +1,315 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/borg-backup-nc.conf"
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/borg-backup/borg-list-backups.sh
)
echo ""
echo -e " \033[33m--------------------------------------------\033[m"
echo " Which Borg Archiv shoul be mounted?"
echo ""
echo -e " \033[33mNote:\033[m it is an read-only fuse filesystem!"
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 "Mount archive '${BORG_REPO}::${BORG_BACKUP_ID}' to '${BORG_FUSE_MOUNT_DIR}'.."
borg mount ${BORG_URL}::${BORG_BACKUP_ID} ${BORG_FUSE_MOUNT_DIR} >> "${log_file}" 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
clean_up 0

277
borg-umount-archiv.sh Executable file
View File

@ -0,0 +1,277 @@
#!/usr/bin/env bash
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/borg-backup-nc.conf"
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 "Unmount fuse filesystem mounted at '${BORG_FUSE_MOUNT_DIR}'.."
borg umount ${BORG_FUSE_MOUNT_DIR} >> "${log_file}" 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
clean_up 0

View File

@ -0,0 +1,132 @@
#-----------------------------
# Settings for script borg-backup-nc.sh
#-----------------------------
# HTTP_USER
#
# User under which the webserver is running
#
# Defaults to:
#
# HTTP_USER="www-data"
#
#HTTP_USER="www-data"
# SSH_USER
#
# The (remote) user under which the SSH session for the
# data transfer is running.
#
# Defaults to:
#
# SSH_USER="borg"
#
#BACKUP_USER="borg"
# SSH_PORT
#
# The port under which the SSH service is accessible
#
# Defaults to:
#
# SSH_PORT=22
#
#SSH_PORT=22
# BORG_HOST
#
# The (remote) host on which the backups are saved.
#
# !! No Defaults - this parameter is mandatory
#
#BORG_HOST="<Fully-Qualified Host Name>"
# BORG_RSH
#
# The command, which is used instead of ssh.
#
# This can be used to specify ssh options, such as a custom identity file
# ssh -i /path/to/private/key. See man ssh for other options.
#
# Using the --rsh CMD commandline option overrides the environment variable.
#
# Defaults to:
# BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup'
#
#BORG_RSH='ssh -i /root/.ssh/id_ed25519-borg-backup'
# BORG_PASSPHRASE
#
# Password of the Borg Repository (which was used while initializing)
#
# !! No Defaults - this parameter is mandatory
#
#BORG_PASSPHRASE='borg-password>'
# BORG_REPO
#
# The repository location on the (remote) backup host.
#
# !! No Defaults - this parameter is mandatory
#
#BORG_REPO=<path-to-the-borg-repository>
# BORG_LOG_DIR
#
# Log to this directory
#
# Defaults to:
#
# BORG_LOG_DIR="/var/log/borg"
#
#BORG_LOG_DIR="/var/log/borg"
# ---
# NC related parameters
# ---
# NC_PATH
#
# Path to the Nextcloud (NC) installation directory (mostly the DocumenRoot
# directory)
#
# !! No Defaults - this parameter is mandatory
#
#NC_PATH="<Path-to-the-NC-installation-directory>"
# NC_DB_BACKUP_DIR
#
# Directory in which a current database dump is stored and transferred to the repository
#
# Defaults to:
#
# NC_DB_BACKUP_DIR="/data/backup/sql"
#
#NC_DB_BACKUP_DIR="/data/backup/sql"
# ---
# Other Backups
# ---
# BACKUP_OTHER_DIRECTORIES
#
# Directories other than NextCloud that should also be backed up
#
# Defaults to:
#
# BACKUP_OTHER_DIRECTORIES="/etc /home /root /var/spool/cron"
#
# - Blank separated list of directories -
#
#BACKUP_OTHER_DIRECTORIES="/etc /home /root /var/spool/cron"