mirror of
https://git.local.zernis.ch/simon/homeserver.zernis.ch.git
synced 2025-12-16 06:07:29 +01:00
added "borgbackup" role
This commit is contained in:
73
roles/borgbackup/templates/backup_to_smb.sh
Normal file
73
roles/borgbackup/templates/backup_to_smb.sh
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Variablen
|
||||
data_dir="{{ data_dir }}"
|
||||
samba_share="{{ smb_share }}"
|
||||
samba_credentials="{{ smb_credentials }}"
|
||||
backup_target_usage_threshold="{{smb_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 cifs "$samba_share" "$mount_point" -o credentials="$samba_credentials" 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
|
||||
# Alle laufenden Container stoppen
|
||||
running_containers=$(docker ps --format '{{.Names}}')
|
||||
for container in $running_containers; do
|
||||
docker stop "$container"
|
||||
done
|
||||
|
||||
# Backup mit BorgBackup erstellen
|
||||
export BORG_PASSPHRASE="$borg_password"
|
||||
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
|
||||
|
||||
mail_subject="Backup-Bericht: $(date +'%Y-%m-%d %H:%M:%S')"
|
||||
mail_body="$backup_result\n
|
||||
Reference in New Issue
Block a user