Files
nas-tools/cleanup_ghost_snapshot.sh
T

94 lines
3.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Cleanup Ghost Time Machine Snapshot
# Sometimes snapshots appear in the list but don't actually exist or take space
set -e
echo "=== Cleanup Ghost Time Machine Snapshot ==="
echo ""
# Check current free space
FREE_SPACE=$(df / | tail -1 | awk '{print $4}')
FREE_SPACE_GB=$(echo "scale=2; $FREE_SPACE / 1024 / 1024" | bc)
echo "Current free space: ${FREE_SPACE_GB}GB"
echo ""
# List snapshots
SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$")
if [ -z "$SNAPSHOTS" ]; then
echo "✅ No snapshots found - all clean!"
exit 0
fi
echo "Found snapshot(s):"
echo "$SNAPSHOTS" | sed 's/^/ - /'
echo ""
# The snapshot might be a ghost entry. Let's try several methods:
echo "Attempting cleanup methods..."
echo ""
# Method 1: Try deleting with date format
echo "Method 1: Delete by date..."
echo "$SNAPSHOTS" | while read snapshot; do
if [ -n "$snapshot" ]; then
# Extract date: com.apple.TimeMachine.2025-12-31-101235.backup -> 2025-12-31-101235
SNAP_DATE=$(echo "$snapshot" | sed 's/.*\.\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{6\}\)\.backup/\1/')
if [ -n "$SNAP_DATE" ]; then
echo " Trying: sudo tmutil deletelocalsnapshots $SNAP_DATE"
if sudo tmutil deletelocalsnapshots "$SNAP_DATE" 2>/dev/null; then
echo " ✅ Success!"
else
echo " ⚠️ Failed (may be ghost entry)"
fi
fi
fi
done
echo ""
# Method 2: Thin all snapshots (aggressive)
echo "Method 2: Aggressive thinning..."
sudo tmutil thinlocalsnapshots / 1000000000 1 2>&1 || true
echo " ✓ Thinning completed"
echo ""
# Method 3: Check if it's actually taking space
echo "Method 3: Checking if snapshot actually exists..."
APFS_SNAPSHOTS=$(diskutil apfs listSnapshots / 2>/dev/null | grep -i "timemachine" || echo "")
if [ -z "$APFS_SNAPSHOTS" ]; then
echo " ️ Snapshot not found in APFS snapshot list"
echo " This confirms it's likely a ghost entry in Time Machine's database"
echo ""
echo " The snapshot entry may persist in Time Machine's database but:"
echo " - It's not taking up space (space was already freed)"
echo " - It will likely clear when Time Machine successfully backs up"
echo " - Or it may clear on the next macOS update/restart"
fi
echo ""
# Final check
sleep 2
REMAINING=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$" | wc -l | tr -d ' ')
if [ "$REMAINING" -gt 0 ]; then
echo "⚠️ Snapshot still listed, but this is likely a ghost entry."
echo ""
echo "✅ Good news: Your space was already freed (${FREE_SPACE_GB}GB free)!"
echo ""
echo "The ghost entry should clear when:"
echo " 1. Time Machine successfully completes a backup"
echo " 2. macOS updates"
echo " 3. Or it may persist but won't cause issues"
echo ""
echo "You can safely ignore it - it's not taking up space."
else
echo "✅ Snapshot successfully removed!"
fi
echo ""