#!/bin/bash
export BORG_REPO=ssh://u335861@u335861.your-storagebox.de:23/./backups/node2
# Euer Passwort muss hier hinterlegt werden.
export BORG_PASSPHRASE='KWsP5eUoS2fd2ZxXWcHgcYMJqt7rekmP'


LOG='/var/log/borg/backup.log'
exec > >(tee -i ${LOG})
exec 2>&1

info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup unterbrochen >&2; exit 2' INT TERM

info "Start backup"

# Hier wird das Backup erstellt, passt das so an wie Ihr das gerne haben möchtet
borg create                                     \
    --stats                                     \
    --compression lz4                           \
    ::'{hostname}-{now}'                        \
    /root                                       \
    /etc                                        \
    /var/www                                    \
    /home                                       \
    /var/lib/pterodactyl                        \
    --exclude /var/lib/pterodactyl/volumes      \
    --exclude /dev                              \
    --exclude /proc                             \
    --exclude /sys                              \
    --exclude /var/run                          \
    --exclude /run                              \
    --exclude /lost+found                       \
    --exclude /mnt                              \
    --exclude /var/lib/lxcfs

backup_exit=$?

info "Loeschen von alten Backups"
# Automatisches löschen von alten Backups
borg prune                          \
    --prefix '{hostname}-'          \
    --keep-daily    14              \
    --keep-weekly   4               \
    --keep-monthly  6

prune_exit=$?

# Informationen ob das Backup geklappt hat.
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))

if [ ${global_exit} -eq 0 ]; then
    info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
    info "Backup and/or Prune finished with warnings"
else
    info "Backup and/or Prune finished with errors"
fi

exit ${global_exit}
