Working script modified for use with working systemd service

This commit is contained in:
2026-01-15 21:25:26 -06:00
parent 87d3cb86c4
commit b6db471b8a
4 changed files with 29 additions and 13 deletions

View File

@@ -19,10 +19,11 @@
##
#################################################################################
ERRORLOG='/var/log/borg/error.log'
LOGGING=error
# Check if running as root
if [ "$EUID" -ne 0 ]; then
printf "Error: This script must be run as root (or via sudo).\n"
printf "Error: This script must be run as root (or via sudo).\n" | tee -a $ERRORLOG
exit 1
fi
@@ -34,7 +35,7 @@ if [ -f /opt/borg/etc/borg_environment ]; then
# Ensure the variables sourced from the file are exported to child processes
export BORG_REPO BORG_PASSPHRASE
else
printf "Error: /opt/borg/etc/borg_environment not found.\n"
printf "Error: /opt/borg/etc/borg_environment not found.\n" | tee -a $ERRORLOG
exit 1
fi
@@ -54,34 +55,39 @@ borg create \
--compression lz4 \
--exclude-caches \
--exclude 'home/*/.cache/*' \
--exclude 'var/log/journal' \
--exclude 'var/log/borg' \
--exclude 'var/tmp/*' \
--exclude 'var/games' \
--exclude 'home/*/.var/app' \
--exclude 'home/*/Media' \
--progress \
::'{hostname}-{now}' \
/etc \
/home \
/root \
/var \
/opt
/opt \
2> >(tee -a $ERRORLOG >&2)
backup_exit=$?
# 2. Prune old backups
info "Pruning repository"
borg prune \
--verbose \
--list \
--glob-archives '{hostname}-*' \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
--keep-monthly 6 \
2> >(tee -a $ERRORLOG)
prune_exit=$?
# 3. Compact repository
info "Compacting repository"
borg compact
borg compact --$LOGGING 2> >(tee -a $ERRORLOG >&2)
compact_exit=$?
@@ -90,11 +96,11 @@ global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
if [ "${global_exit}" -eq 0 ]; then
info "Backup, Prune, and Compact finished successfully"
info "Backup, Prune, and Compact finished successfully" | tee -a $ERRORLOG
elif [ "${global_exit}" -eq 1 ]; then
info "Backup, Prune, and/or Compact finished with warnings"
info "Backup, Prune, and/or Compact finished with warnings" | tee -a $ERRORLOG
else
info "Backup, Prune, and/or Compact finished with errors"
info "Backup, Prune, and/or Compact finished with errors" | tee -a $ERRORLOG
fi
exit "${global_exit}"