Compare commits
4
Commits
7916199657
...
8ed8b69c7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ed8b69c7e | ||
|
|
eeac807158 | ||
|
|
61a92128a6 | ||
|
|
a21993489f |
No files matched your search
Vendored
BIN
Binary file not shown.
@@ -1,40 +1,127 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# Quelle und Ziel definieren
|
||||||
SOURCE="/Users/rob/Recordings/Work/"
|
SOURCE="/Users/rob/Recordings/Work/00_MacOS/"
|
||||||
TARGET="/Volumes/rob/recordings/Progress/Work/"
|
SERVER="bfds"
|
||||||
MOUNT_POINT="/Volumes/rob"
|
REMOTE_PATH="/mnt/bfds-nix/rob/recordings/Progress/Work/00_MacOS/"
|
||||||
SERVER_ADDRESS="smb://bfds/rob"
|
TARGET="$SERVER:$REMOTE_PATH"
|
||||||
|
|
||||||
# Prüfen, ob das Netzlaufwerk gemountet ist
|
# Prüfen, ob SSH-Verbindung zum Server möglich ist
|
||||||
if mount | grep -q "$MOUNT_POINT"; then
|
log "Prüfe SSH-Verbindung zu $SERVER..."
|
||||||
echo "Netzlaufwerk ist gemountet. Backup startet..."
|
if ! ssh -o ConnectTimeout=5 -o BatchMode=yes "$SERVER" "exit" 2>/dev/null; then
|
||||||
else
|
log "Fehler: SSH-Verbindung zu $SERVER fehlgeschlagen"
|
||||||
echo "Netzlaufwerk nicht gefunden. Versuche zu mounten..."
|
notify "Fehler" "Server $SERVER nicht erreichbar!"
|
||||||
osascript -e "mount volume \"$SERVER_ADDRESS\""
|
|
||||||
|
|
||||||
# Nach dem Mounten erneut prüfen
|
|
||||||
if mount | grep -q "$MOUNT_POINT"; then
|
|
||||||
echo "Netzlaufwerk erfolgreich gemountet."
|
|
||||||
else
|
|
||||||
echo "Fehler: Netzlaufwerk konnte nicht gemountet werden."
|
|
||||||
osascript -e 'display notification "Backup fehlgeschlagen! Netzlaufwerk nicht erreichbar." with title "Backup Script"'
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
fi
|
||||||
|
|
||||||
# Sicherung starten
|
# Prüfen, ob Schreibrechte vorhanden sind
|
||||||
rsync -v -r -l -t -D "$SOURCE" "$TARGET"
|
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
|
||||||
|
|
||||||
# -r → Rekursiv kopieren (alle Unterordner & Dateien)
|
log "Remote-Verzeichnis ist bereit für Backup"
|
||||||
# -l → Symbolische Links kopieren
|
|
||||||
# -t → Zeitstempel beibehalten
|
# Sicherung starten
|
||||||
# -D → Geräte- und Sonderdateien kopiere
|
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
|
# Erfolgsmeldung ausgeben
|
||||||
echo "Backup abgeschlossen: $DATE"
|
log "Backup abgeschlossen: $TOTAL_FILES Dateien, $TOTAL_SIZE übertragen"
|
||||||
|
|
||||||
# Optional: Notification für macOS anzeigen
|
# Optional: Notification für macOS anzeigen
|
||||||
osascript -e 'display notification "Backup abgeschlossen!" with title "Backup Script"'
|
notify "Abgeschlossen" "$TOTAL_FILES Dateien übertragen ($TOTAL_SIZE)"
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user