Update Scripts/borg.sh

This commit is contained in:
dataprolet 2025-05-21 12:44:02 +02:00
parent 379899af93
commit 8af245fda6

View File

@ -1,63 +1,89 @@
#!/usr/bin/env bash
# My personal script to take Borg backups
# Expecting an already initialized repository
set -eou pipefail
set -x
# 0. Gotify-Benachrichtigung, falls Fehler
err() {
curl "https://gotify.dataprolet.de/message?token=XXXXXXXXXX" -F "title=Borg Backup Desktop" \
-F "message=Borg-Backup hatte einen Fehler." -F "priority=5"
# Definiere Funktion
run_borgbackup () {
# Error-Funktion mit Gotify-Benachrichtigung
notify-error () {
sh ~/.scripts/gotify.sh "Borg Backup Desktop" "Borg-Backup hatte einen Fehler."
}
# Erstelle Lock-File
# https://www.putorius.net/lock-files-bash-scripts.html
exec 100>"/home/arne/.borg/borg.lock" || exit 1
flock -n 100 || exit 1
trap 'rm -f /home/arne/.borg/borg.lock' EXIT
# Desktop-Benachrichtigung, dass das Backup startet
sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
notify-send -t 5000 'BorgBackup' 'Backup started..' && \
sudo -u '#1000' XDG_RUNTIME_DIR=/run/user/1000 \
paplay /usr/share/sounds/ocean/stereo/dialog-information.oga
# Verzeichnis für die Logdatei festlegen
log="/home/arne/.borg/logs/$(date +%Y-%m-%d_%H:%M).log"
# Adresse des Repositories festlegen
repo="" # Hier Repository einfügen
# Standard streams in Logdatei umleiten
exec >> >(tee -i "${log}")
exec 2>&1
# Passwort für Repository exportieren
export BORG_PASSCOMMAND='cat "/etc/borgpass"'
# Backup erstellen (niceness = 2, verbose, zeige Statistiken, Kompression = automatisch & zstd)
# https://borgbackup.readthedocs.io/en/stable/usage/create.html
nice -n 19 ionice -c 3 borg create -v --stats -C auto,zstd \
${repo}::'{now:%Y-%m-%d_%H:%M}' \
/home/arne/Daten/Bilder \
/home/arne/Daten/Backups \
/home/arne/Daten/Dokumente \
/home/arne/Daten/Videos \
/home/arne/Daten/VMs \
|| notify-error
# Desktop-Benachrichtigung, dass das Backup beendet ist
sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
notify-send -t 5000 'BorgBackup' 'Backup finished. Start Pruning..'
# Zeitstempel für die Logdatei (Start)
echo
echo "Started pruning backups: $(date)"
echo
# Behalte 7 tägliche, 4 wöchentliche, 6 monatliche und 1 jährliche Backup(s).
# https://borgbackup.readthedocs.io/en/stable/usage/prune.html
# Zeitmessung für die Logdatei
time borg prune --stats --list --save-space -d=5 -m=3 -y=2 ${repo}
# Zeitstempel für die Logdatei (Ende)
echo
echo "Finished pruning: $(date)"
# Desktop-Benachrichtigung, dass die Bereinigung beendet ist
sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
notify-send -t 5000 'BorgBackup' 'Pruning finished.'
# Logdateien löschen, die älter sind als 30 Tage
command -v rmtrash >/dev/null || echo "rmtrash ist nicht installiert."
/usr/bin/find /home/arne/.borg/logs/* -mtime +30 -exec sudo -u arne env XDG_RUNTIME_DIR="/run/user/$(id -u arne)" \
/usr/bin/rmtrash -rf {} \;
# Lege temporäre Datei an, damit Borg nur einmal täglich läuft
touch "/home/arne/.borg/last-run-$(date +%Y-%m-%d)"
}
# 1. Desktop-Benachrichtigung, dass das Backup startet
sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send -t 5000 'BorgBackup' 'Backup started..'
# 2. Verzeichnis für die Logdatei festlegen
log="/home/$user/.borg/logs/"$(date +"%Y-%m-%d_%H:%M")".log"
# 3. Adresse des Repositories festlegen
repo= # Fill in your repositories address here
# 4. Standard streams in Logdatei umleiten
exec >> >(tee -i ${log})
exec 2>&1
# 5. Passwort für Repository exportieren
export BORG_PASSCOMMAND='cat "/etc/borgpass"'
# 6. Backup erstellen (niceness = 2, verbose, zeige Statistiken, Kompression = automatisch & zstd)
# https://borgbackup.readthedocs.io/en/stable/usage/create.html
nice -n 19 ionice -c 3 borg create -v --stats -C auto,zstd \
$repo::'{now:%Y-%m-%d_%H:%M}' \
/home/$user/Daten \
|| err
# 7. Desktop-Benachrichtigung, dass das Backup beendet ist
sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send -t 5000 'BorgBackup' 'Backup finished.'
# 8. Leerzeichen für die Logdatei
echo
# 9. Desktop-Benachrichtigung, dass das Repository bereinigt wird
# https://borgbackup.readthedocs.io/en/stable/usage/prune.html
sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send -t 5000 'BorgBackup' 'Pruning backups..'
# 10. Zeitstempel für die Logdatei (Start)
echo "Started pruning backups: $(date)"
echo
# 11. Behalte 7 tägliche, 4 wöchentliche, 6 monatliche und 1 jährliche Backup(s).
# Zeitmessung für die Logdatei
time borg prune --stats --list --save-space -d=5 -m=3 -y=1 $repo
# 12. Leerzeile für die Logdatei
echo
# 13. Zeitstempel für die Logdatei (Ende)
echo "Finished pruning: $(date)"
# 14. Desktop-Benachrichtigung, dass die Bereinigung beendet ist
sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send -t 5000 'BorgBackup' 'Pruning finished'
# 15. Logdateien löschen, die älter sind als 30 Tage
/usr/bin/find /home/$user/.borg/logs/* -mtime +30 -exec sudo -u $user env XDG_RUNTIME_DIR="/run/user/$(id -u $user)" /usr/bin/rmtrash -rf {} \;
if ! [[ -f "/home/arne/.borg/borg.lock" ]] || \ # Prüfe Lockfile
! [[ -f "/home/arne/.borg/last-run-$(date +%Y-%m-%d)" ]] || \ # Prüfe, ob heute schon lief
! pgrep -x "cs2" > /dev/null || \ # Prüfe, ob CS2 läuft
! [[ "$EUID" -ne 0 ]]; then # Prüfe, ob als root gestartet
# Führe Funktion aus und verhindere Ruhezustand etc.
# https://www.freedesktop.org/software/systemd/man/latest/systemd-inhibit.html
systemd-inhibit bash -c "$(declare -f run_borgbackup); run_borgbackup"
fi