mirror of
https://git.local.zernis.ch/simon/homeserver.zernis.ch.git
synced 2025-12-16 23:17:29 +01:00
added NFS Backup as an alternative to smb
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
username= {{ smb_username }}
|
||||
password= {{ smb_pass }}
|
||||
83
roles/borgbackup/templates/backup_to_nfs.sh
Normal file
83
roles/borgbackup/templates/backup_to_nfs.sh
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Variablen
|
||||
data_dir="/home/{{ user['name'] }}/docker"
|
||||
nfs_share="{{ nfs_share }}"
|
||||
backup_target_usage_threshold="{{nfs_threshold}}" # Prozentuale Schwellenwert für die Speicherauslastung
|
||||
mount_point="{{ mount_point }}"
|
||||
backup_repository="$mount_point/{{borg_repo}}"
|
||||
borg_password="{{borg_pass}}"
|
||||
email_recipient="{{admin_mail}}"
|
||||
|
||||
# Mounten des Backup-Ziels
|
||||
mount_successful=0
|
||||
mount_output=$(mount -t nfs "$nfs_share" "$mount_point" 2>&1)
|
||||
mount_status=$?
|
||||
|
||||
if [ "$mount_status" -eq 0 ]; then
|
||||
mount_successful=1
|
||||
else
|
||||
echo "Mount fehlgeschlagen: $mount_output" >&2
|
||||
fi
|
||||
|
||||
if [ "$mount_successful" -eq 1 ]; then
|
||||
# Repository erstellen, falls nicht vorhanden
|
||||
mkdir -p "$backup_repository"
|
||||
|
||||
# Alle laufenden Container stoppen
|
||||
running_containers=$(docker ps --format '{{ "{{.Names}}" }}')
|
||||
for container in $running_containers; do
|
||||
docker stop "$container"
|
||||
done
|
||||
|
||||
# Prüfen, ob das Repository existiert, und ggf. erstellen
|
||||
export BORG_PASSPHRASE="$borg_password"
|
||||
if ! borg list "$backup_repository" >/dev/null 2>&1; then
|
||||
borg init --encryption=repokey "$backup_repository"
|
||||
fi
|
||||
|
||||
# Backup mit BorgBackup erstellen
|
||||
backup_result=$(borg create --progress --list --stats --compression lz4 "$backup_repository"::'{hostname}-{now:%Y-%m-%d_%H:%M:%S}' $data_dir 2>&1)
|
||||
backup_status=$?
|
||||
|
||||
# Alle gestoppten Container starten
|
||||
for container in $running_containers; do
|
||||
docker start "$container"
|
||||
done
|
||||
|
||||
# Backup-Integrität überprüfen
|
||||
borg_check_result=$(borg check --repository-only "$backup_repository" 2>&1)
|
||||
|
||||
# Backup-Status und Speicherauslastung prüfen
|
||||
if [ "$backup_status" -eq 0 ]; then
|
||||
backup_result="Backup erfolgreich:\n$backup_result\n\nIntegrität des Backups überprüft:\n$borg_check_result"
|
||||
else
|
||||
backup_result="Backup fehlgeschlagen (Status: $backup_status):\n$backup_result\n\nIntegrität des Backups konnte nicht überprüft werden."
|
||||
fi
|
||||
|
||||
# Retention Policy anwenden
|
||||
borg_prune_result=$(borg prune --keep-daily=7 --keep-weekly=4 --keep-monthly=6 "$backup_repository" 2>&1)
|
||||
|
||||
# Unmounten des Backup-Ziels
|
||||
umount_output=$(umount "$mount_point" 2>&1)
|
||||
umount_status=$?
|
||||
if [ "$umount_status" -ne 0 ]; then
|
||||
echo "Unmount fehlgeschlagen: $umount_output" >&2
|
||||
fi
|
||||
|
||||
# E-Mail senden
|
||||
target_usage=$(df -h "$mount_point" | tail -1 | awk '{ print $5 }')
|
||||
target_usage_number=${target_usage%%%}
|
||||
|
||||
if [ "$target_usage_number" -gt "$backup_target_usage_threshold" ]; then
|
||||
target_usage_warning="\n\nWARNUNG: Die Speicherauslastung des Backup-Ziels beträgt $target_usage und überschreitet den festgelegten Schwellenwert von $backup_target_usage_threshold%."
|
||||
else
|
||||
target_usage_warning=""
|
||||
fi
|
||||
|
||||
hostname=$(hostname)
|
||||
mail_subject="Backup-Bericht: $hostname - $(date +'%Y-%m-%d %H:%M:%S')"
|
||||
mail_body="$backup_result\n"
|
||||
|
||||
echo -e "$mail_body$target_usage_warning" | mail -s "$mail_subject" -r "no-reply@homeserver.zernis.ch" "$email_recipient"
|
||||
fi
|
||||
@@ -23,14 +23,22 @@ else
|
||||
fi
|
||||
|
||||
if [ "$mount_successful" -eq 1 ]; then
|
||||
# Repository erstellen, falls nicht vorhanden
|
||||
mkdir -p "$backup_repository"
|
||||
|
||||
# Alle laufenden Container stoppen
|
||||
running_containers=$(docker ps --format '{{.Names}}')
|
||||
running_containers=$(docker ps --format '{{ "{{.Names}}" }}')
|
||||
for container in $running_containers; do
|
||||
docker stop "$container"
|
||||
done
|
||||
|
||||
# Backup mit BorgBackup erstellen
|
||||
# Prüfen, ob das Repository existiert, und ggf. erstellen
|
||||
export BORG_PASSPHRASE="$borg_password"
|
||||
if ! borg list "$backup_repository" >/dev/null 2>&1; then
|
||||
borg init --encryption=repokey "$backup_repository"
|
||||
fi
|
||||
|
||||
# Backup mit BorgBackup erstellen
|
||||
backup_result=$(borg create --progress --list --stats --compression lz4 "$backup_repository"::'{hostname}-{now:%Y-%m-%d_%H:%M:%S}' $data_dir 2>&1)
|
||||
backup_status=$?
|
||||
|
||||
@@ -59,7 +67,7 @@ if [ "$mount_successful" -eq 1 ]; then
|
||||
echo "Unmount fehlgeschlagen: $umount_output" >&2
|
||||
fi
|
||||
|
||||
# E-Mail senden
|
||||
# E-Mail senden
|
||||
target_usage=$(df -h "$mount_point" | tail -1 | awk '{ print $5 }')
|
||||
target_usage_number=${target_usage%%%}
|
||||
|
||||
@@ -69,5 +77,9 @@ if [ "$mount_successful" -eq 1 ]; then
|
||||
target_usage_warning=""
|
||||
fi
|
||||
|
||||
mail_subject="Backup-Bericht: $(date +'%Y-%m-%d %H:%M:%S')"
|
||||
mail_body="$backup_result\n
|
||||
hostname=$(hostname)
|
||||
mail_subject="Backup-Bericht: $hostname - $(date +'%Y-%m-%d %H:%M:%S')"
|
||||
mail_body="$backup_result\n"
|
||||
|
||||
echo -e "$mail_body$target_usage_warning" | mail -s "$mail_subject" -r "no-reply@homeserver.zernis.ch" "$email_recipient"
|
||||
fi
|
||||
2
roles/borgbackup/templates/cifs.j2
Normal file
2
roles/borgbackup/templates/cifs.j2
Normal file
@@ -0,0 +1,2 @@
|
||||
username={{ smb_username }}
|
||||
password={{ smb_pass }}
|
||||
Reference in New Issue
Block a user