This commit is contained in:
Pipo 2025-09-27 17:26:05 +02:00
parent 2cac218237
commit ae7a71574a
2 changed files with 32 additions and 12 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -1,40 +1,60 @@
#!/bin/zsh #!/bin/zsh
set -euo pipefail set -e -u
set -o pipefail # zsh: pipefail so wirklich aktiv
LOGF="/var/log/snapclean.log" LOGF="/var/log/snapclean.log"
log() { print -r -- "$(date '+%F %T') $*" >> "$LOGF"; } log() { print -r -- "$(date '+%F %T') $*" >> "$LOGF"; }
# minimaler PATH für launchd-Umgebung
export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
is_running() { is_running() {
/usr/bin/tmutil status 2>/dev/null | /usr/bin/grep -Eq '"Running" = 1|Running = 1' if /usr/bin/tmutil status 2>/dev/null | /usr/bin/grep -Eq '"Running" = 1|Running = 1'; then
return 0 # running
else
return 1 # idle
fi
} }
wait_until_tm_idle() { wait_until_tm_idle() {
set +e
local i=0 local i=0
while is_running; do while is_running; do
sleep 2 sleep 2
(( i++ )) (( i++ ))
[[ $i -gt 150 ]] && log "Timeout: TM still running. Waiting until it's finished." && break [[ $i -gt 150 ]] && log "Timeout: TM noch Running, breche Warten ab." && break
done done
set -e
log "TM jetzt idle (oder Timeout erreicht)."
return 0
} }
delete_all_snapshots() { delete_all_snapshots() {
log "Deletes all local Snapshots at / …" log "Snapshots vor Delete (/):"
/usr/bin/tmutil deletelocalsnapshots / || true /usr/bin/tmutil listlocalsnapshots / >>"$LOGF" 2>&1 || log "listlocalsnapshots(/): none/error"
log "Finished deleting."
log "Lösche alle lokalen Snapshots auf / …"
set +e
/usr/bin/tmutil deletelocalsnapshots / >>"$LOGF" 2>&1
local rc=$?
set -e
log "Fertig gelöscht. (rc=${rc})"
} }
# Make sure logfile exists # Sicherstellen, dass Logfile existiert (läuft als root kein sudo nötig)
[[ -f "$LOGF" ]] || { sudo touch "$LOGF"; sudo chmod 644 "$LOGF"; } [[ -f "$LOGF" ]] || { : > "$LOGF"; /bin/chmod 644 "$LOGF"; }
# KEINE Pipe → keine Subshell-Probleme
while IFS= read -r line; do while IFS= read -r line; do
# nur die konkrete Zeile matchen, die du gepostet hast
if print -r -- "$line" | /usr/bin/grep -q \ if print -r -- "$line" | /usr/bin/grep -q \
"com.apple.TimeMachine:LocalSnapshotManagement] Created Time Machine local snapshot"; then "com.apple.TimeMachine:LocalSnapshotManagement] Created Time Machine local snapshot"; then
log "Event erkannt: $line" log "Event erkannt: $line"
# Short break until the snapshot is in index # Warten bis Time Machine fertig ist
sleep 2
# Waiting until TM is finished
wait_until_tm_idle wait_until_tm_idle
# delete all local snapshots # Ein Hauch warten, bis der Snapshot sicher im Index steht
sleep 10
# Dann alle lokalen Snapshots löschen
delete_all_snapshots delete_all_snapshots
fi fi
done < <( done < <(