#!/bin/bash # Logfile definieren LOGFILE="$HOME/Library/Logs/backup-recordings.log" MAX_LOG_SIZE=$((3 * 1024 * 1024)) # 3MB in Bytes # Funktion zur Überprüfung und Rotation des Logfiles rotate_log() { if [ -f "$LOGFILE" ]; then local size=$(stat -f%z "$LOGFILE" 2>/dev/null || echo 0) if [ "$size" -ge "$MAX_LOG_SIZE" ]; then # Behalte nur die letzten 50% des Logs local keep_lines=$(($(wc -l < "$LOGFILE") / 2)) tail -n "$keep_lines" "$LOGFILE" > "${LOGFILE}.tmp" mv "${LOGFILE}.tmp" "$LOGFILE" fi fi } # Funktion für Log-Ausgaben mit Zeitstempel log() { local message="[$(date '+%Y-%m-%d %H:%M:%S')] $1" echo "$message" | tee -a "$LOGFILE" } # Funktion für macOS Benachrichtigungen notify() { local subtitle="$1" local message="$2" osascript -e "display notification \"$message\" with title \"Backup Recordings\" subtitle \"$subtitle\"" } # Funktion zum Formatieren von Bytes in menschenlesbare Größe format_bytes() { local bytes=$1 if [ "$bytes" -lt 1024 ]; then echo "${bytes} B" elif [ "$bytes" -lt 1048576 ]; then awk "BEGIN {printf \"%.1f KB\", $bytes/1024}" elif [ "$bytes" -lt 1073741824 ]; then awk "BEGIN {printf \"%.1f MB\", $bytes/1048576}" else awk "BEGIN {printf \"%.2f GB\", $bytes/1073741824}" fi } # Log rotieren falls nötig rotate_log # Quelle und Ziel definieren SOURCE="/Users/rob/Recordings/Work/00_MacOS/" SERVER="bfds" REMOTE_PATH="/mnt/bfds-nix/rob/recordings/Progress/Work/00_MacOS/" TARGET="$SERVER:$REMOTE_PATH" # Prüfen, ob SSH-Verbindung zum Server möglich ist log "Prüfe SSH-Verbindung zu $SERVER..." if ! ssh -o ConnectTimeout=5 -o BatchMode=yes "$SERVER" "exit" 2>/dev/null; then log "Fehler: SSH-Verbindung zu $SERVER fehlgeschlagen" notify "Fehler" "Server $SERVER nicht erreichbar!" exit 1 fi log "SSH-Verbindung erfolgreich" # Prüfen, ob das Remote-Zielverzeichnis existiert log "Prüfe Remote-Zielverzeichnis..." if ! ssh "$SERVER" "test -d \"$REMOTE_PATH\""; then log "Fehler: Remote-Verzeichnis existiert nicht: $REMOTE_PATH" notify "Fehler" "Zielverzeichnis nicht gefunden!\n$REMOTE_PATH" exit 1 fi # Prüfen, ob Schreibrechte vorhanden sind if ! ssh "$SERVER" "test -w \"$REMOTE_PATH\""; then log "Fehler: Keine Schreibrechte für Remote-Verzeichnis: $REMOTE_PATH" notify "Fehler" "Keine Schreibrechte!\n$REMOTE_PATH" exit 1 fi log "Remote-Verzeichnis ist bereit für Backup" # Sicherung starten log "Starte rsync..." RSYNC_OUTPUT=$(mktemp) log "Befehl: rsync -av --stats --no-owner --no-group \"$SOURCE\" > \"$RSYNC_OUTPUT\" \"$TARGET\"" rsync -av --stats --no-owner --no-group "$SOURCE" "$TARGET" > "$RSYNC_OUTPUT" 2>&1 # Rsync-Output komplett ins Logfile schreiben cat "$RSYNC_OUTPUT" >> "$LOGFILE" # -a → Archive-Modus (erhält Zeitstempel, Links, etc.) # -v → Verbose # --stats → Statistiken anzeigen # --no-owner → Besitzer nicht übertragen # --no-group → Gruppe nicht übertragen # Statistiken auslesen - rsync gibt mehrere Formate aus, teste beide TOTAL_FILES=$(grep -E "Number of (regular )?files transferred:" "$RSYNC_OUTPUT" | awk '{print $NF}' | tr -d ',') if [ -z "$TOTAL_FILES" ]; then # Alternativer Versuch TOTAL_FILES=$(grep "files transferred:" "$RSYNC_OUTPUT" | awk '{print $(NF)}' | tr -d ',') fi # Dateigröße in Bytes auslesen # macOS rsync gibt aus: "Total transferred file size: 86137390 B" TOTAL_BYTES=$(grep "Total transferred file size:" "$RSYNC_OUTPUT" | awk '{print $5}') # Konvertiere String in Integer TOTAL_BYTES=$((TOTAL_BYTES)) # Formatiere Größe in menschenlesbares Format TOTAL_SIZE=$(format_bytes "$TOTAL_BYTES") # Fallback falls keine Werte gefunden wurden [ -z "$TOTAL_FILES" ] && TOTAL_FILES="0" # Temporäre Datei löschen rm -f "$RSYNC_OUTPUT" # Setze Besitzer und Rechte auf dem Server log "Setze Besitzer und Rechte..." ssh "$SERVER" "chown -R smbuser:smbuser \"$REMOTE_PATH\" && chmod -R 777 \"$REMOTE_PATH\" && find \"$REMOTE_PATH\" -type f -exec chmod 666 {} \;" 2>&1 | tee -a "$LOGFILE" # Erfolgsmeldung ausgeben log "Backup abgeschlossen: $TOTAL_FILES Dateien, $TOTAL_SIZE übertragen" # Optional: Notification für macOS anzeigen notify "Abgeschlossen" "$TOTAL_FILES Dateien übertragen ($TOTAL_SIZE)"