Update Scripts/borg.sh

This commit is contained in:
dataprolet 2025-05-21 19:16:23 +02:00
parent 8af245fda6
commit 2e179867a2

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eou pipefail #set -eou pipefail
set -x set -x
# Definiere Funktion # Definiere Funktion
@ -8,7 +8,7 @@ run_borgbackup () {
# Error-Funktion mit Gotify-Benachrichtigung # Error-Funktion mit Gotify-Benachrichtigung
notify-error () { notify-error () {
sh ~/.scripts/gotify.sh "Borg Backup Desktop" "Borg-Backup hatte einen Fehler." sh /home/arne/.scripts/gotify.sh "Borg Backup Desktop" "Borg-Backup hatte einen Fehler."
} }
# Erstelle Lock-File # Erstelle Lock-File
@ -27,7 +27,7 @@ run_borgbackup () {
log="/home/arne/.borg/logs/$(date +%Y-%m-%d_%H:%M).log" log="/home/arne/.borg/logs/$(date +%Y-%m-%d_%H:%M).log"
# Adresse des Repositories festlegen # Adresse des Repositories festlegen
repo="" # Hier Repository einfügen repo="<Hier Repository einfügen>"
# Standard streams in Logdatei umleiten # Standard streams in Logdatei umleiten
exec >> >(tee -i "${log}") exec >> >(tee -i "${log}")
@ -38,33 +38,28 @@ run_borgbackup () {
# Backup erstellen (niceness = 2, verbose, zeige Statistiken, Kompression = automatisch & zstd) # Backup erstellen (niceness = 2, verbose, zeige Statistiken, Kompression = automatisch & zstd)
# https://borgbackup.readthedocs.io/en/stable/usage/create.html # https://borgbackup.readthedocs.io/en/stable/usage/create.html
nice -n 19 ionice -c 3 borg create -v --stats -C auto,zstd \ if nice -n 19 ionice -c 3 borg create -v --stats -C auto,zstd \
${repo}::'{now:%Y-%m-%d_%H:%M}' \ ${repo}::'{now:%Y-%m-%d_%H:%M}' \
/home/arne/Daten/Bilder \ /home/arne/Daten/Bilder \
/home/arne/Daten/Backups \ /home/arne/Daten/Backups \
/home/arne/Daten/Dokumente \ /home/arne/Daten/Dokumente \
/home/arne/Daten/Videos \ /home/arne/Daten/Videos \
/home/arne/Daten/VMs \ /home/arne/Daten/VMs; then
|| notify-error
# Desktop-Benachrichtigung, dass das Backup beendet ist # Desktop-Benachrichtigung, dass das Backup beendet ist
sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \ sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
notify-send -t 5000 'BorgBackup' 'Backup finished. Start Pruning..' notify-send -t 5000 'BorgBackup' 'Backup finished. Start Pruning..'
# Zeitstempel für die Logdatei (Start) # Zeitstempel für die Logdatei (Start)
echo echo && echo "Started pruning backups: $(date)" && echo
echo "Started pruning backups: $(date)"
echo
# Behalte 7 tägliche, 4 wöchentliche, 6 monatliche und 1 jährliche Backup(s). # Behalte 5 tägliche, 3 monatliche und 2 jährliche Backup(s).
# https://borgbackup.readthedocs.io/en/stable/usage/prune.html # https://borgbackup.readthedocs.io/en/stable/usage/prune.html
# Zeitmessung für die Logdatei # Zeitmessung für die Logdatei
time borg prune --stats --list --save-space -d=5 -m=3 -y=2 ${repo} if time borg prune --stats --list --save-space -d=5 -m=3 -y=2 ${repo}; then
# Zeitstempel für die Logdatei (Ende) # Zeitstempel für die Logdatei (Ende)
echo echo && echo "Finished pruning: $(date)"
echo "Finished pruning: $(date)"
# Desktop-Benachrichtigung, dass die Bereinigung beendet ist # Desktop-Benachrichtigung, dass die Bereinigung beendet ist
sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \ sudo -u arne DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
@ -75,15 +70,35 @@ run_borgbackup () {
/usr/bin/find /home/arne/.borg/logs/* -mtime +30 -exec sudo -u arne env XDG_RUNTIME_DIR="/run/user/$(id -u arne)" \ /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 {} \; /usr/bin/rmtrash -rf {} \;
# Lösche alte temporäre Datei (Wozu nochmal?)
#find /home/arne/.borg/ -maxdepth 1 -type f ! -name "*.lock" ! -name "*.sh" ! -name "*last-run-$(date +%Y-%m-%d)*" -exec rm {} +
# Lege temporäre Datei an, damit Borg nur einmal täglich läuft # Lege temporäre Datei an, damit Borg nur einmal täglich läuft
touch "/home/arne/.borg/last-run-$(date +%Y-%m-%d)" touch "/home/arne/.borg/last-run-$(date +%Y-%m-%d)"
else
notify-error && exit 1
fi
else
notify-error && exit 1
fi
} }
if ! [[ -f "/home/arne/.borg/borg.lock" ]] || \ # Prüfe Lockfile if [[ -f "/home/arne/.borg/borg.lock" ]]; then
! [[ -f "/home/arne/.borg/last-run-$(date +%Y-%m-%d)" ]] || \ # Prüfe, ob heute schon lief echo "Backup läuft gerade! Beende.."
! pgrep -x "cs2" > /dev/null || \ # Prüfe, ob CS2 läuft exit 0
! [[ "$EUID" -ne 0 ]]; then # Prüfe, ob als root gestartet elif [[ -f "/home/arne/.borg/last-run-$(date +%Y-%m-%d)" ]]; then
# Führe Funktion aus und verhindere Ruhezustand etc. echo "Backup lief heute schon. Beende.."
# https://www.freedesktop.org/software/systemd/man/latest/systemd-inhibit.html exit 0
systemd-inhibit bash -c "$(declare -f run_borgbackup); run_borgbackup" elif pgrep -x "cs2" > /dev/null; then
echo "CS läuft gerade. Beende"
exit 0
elif [[ "$EUID" -ne 0 ]]; then
echo "Skript muss als root laufen. Beende.."
exit 0
fi
# Führe Funktion aus und verhindere Ruhezustand etc.
# https://www.freedesktop.org/software/systemd/man/latest/systemd-inhibit.html
if systemd-inhibit bash -c "$(declare -f run_borgbackup); run_borgbackup"; then
sh /home/arne/.scripts/gotify.sh "Borg Backup Desktop" "Backup erfolgreich fertig gestellt.."
fi fi