Compare commits

..

No commits in common. "main" and "macOS-snapcleaner_1.0.2" have entirely different histories.

View file

@ -1,10 +1,11 @@
#!/bin/zsh #!/bin/zsh
set -e -u set -e -u
set -o pipefail 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" export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
is_running() { is_running() {
@ -21,36 +22,39 @@ wait_until_tm_idle() {
while is_running; do while is_running; do
sleep 2 sleep 2
(( i++ )) (( i++ ))
[[ $i -gt 150 ]] && log "Timeout: TM still running. Abort" && break [[ $i -gt 150 ]] && log "Timeout: TM noch Running, breche Warten ab." && break
done done
set -e set -e
log "TM jetzt idle (oder Timeout erreicht)."
return 0 return 0
} }
delete_all_snapshots() { delete_all_snapshots() {
log "Snapshots before deletion (/):" log "Snapshots vor Delete (/):"
/usr/bin/tmutil listlocalsnapshots / >>"$LOGF" 2>&1 || log "listlocalsnapshots(/): none/error" /usr/bin/tmutil listlocalsnapshots / >>"$LOGF" 2>&1 || log "listlocalsnapshots(/): none/error"
log "Delete all local snapshots at / …" log "Lösche alle lokalen Snapshots auf / …"
set +e set +e
/usr/bin/tmutil deletelocalsnapshots / >>"$LOGF" 2>&1 /usr/bin/tmutil deletelocalsnapshots / >>"$LOGF" 2>&1
local rc=$? local rc=$?
set -e set -e
log "Deletion successfull. (rc=${rc})" log "Fertig gelöscht. (rc=${rc})"
} }
# Make sure the logfile exists # Sicherstellen, dass Logfile existiert (läuft als root kein sudo nötig)
[[ -f "$LOGF" ]] || { : > "$LOGF"; /bin/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"
# Wait until Timemachine has finished # Warten bis Time Machine fertig ist
wait_until_tm_idle wait_until_tm_idle
# Wait a moment until the the snapshot is in index # Ein Hauch warten, bis der Snapshot sicher im Index steht
sleep 10 sleep 10
# Delete all local snapshots # Dann alle lokalen Snapshots löschen
delete_all_snapshots delete_all_snapshots
fi fi
done < <( done < <(