Files
nas-tools/emergency_free_space.sh
T

105 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Emergency Free Space - When Time Machine is stuck and consuming space
# This script provides the quickest way to free space
set -e
echo "=== Emergency Free Space Fix ==="
echo ""
echo "Time Machine is stuck and holding your deleted file space."
echo ""
FREE_SPACE_BEFORE=$(df / | tail -1 | awk '{print $4}')
FREE_SPACE_GB_BEFORE=$(echo "scale=2; $FREE_SPACE_BEFORE / 1024 / 1024" | bc)
echo "Current free space: ${FREE_SPACE_GB_BEFORE}GB"
echo ""
echo "🔧 Solution: Remove Time Machine destination to free the stuck snapshot"
echo ""
echo "The snapshot is stuck because Time Machine can't connect to your NAS."
echo "Removing the destination will allow the snapshot to be deleted."
echo ""
read -p "Have you removed the Time Machine destination in System Settings? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "Please do this now:"
echo "1. Open System Settings → General → Time Machine"
echo "2. Click the '-' button to remove the backup disk"
echo "3. Confirm removal"
echo ""
echo "Then run this script again."
exit 1
fi
echo ""
echo "⏳ Waiting for Time Machine to fully disconnect..."
sleep 5
# Kill any remaining backup processes
echo "🛑 Stopping any remaining Time Machine processes..."
sudo killall backupd 2>/dev/null || true
sleep 2
# Try to delete the snapshot now
echo ""
echo "🗑️ Attempting to delete stuck snapshot..."
SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$")
if [ -n "$SNAPSHOTS" ]; then
echo "$SNAPSHOTS" | while read snapshot; do
if [ -n "$snapshot" ]; then
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 " Deleting: $snapshot"
if sudo tmutil deletelocalsnapshots "$SNAP_DATE" 2>/dev/null; then
echo " ✅ Successfully deleted!"
else
echo " ⚠️ Still stuck. Try restarting your Mac."
fi
fi
fi
done
else
echo " ✅ No snapshots found"
fi
echo ""
echo "📊 Checking results..."
sleep 3
FREE_SPACE_AFTER=$(df / | tail -1 | awk '{print $4}')
FREE_SPACE_GB_AFTER=$(echo "scale=2; $FREE_SPACE_AFTER / 1024 / 1024" | bc)
FREED_GB=$(echo "scale=2; $FREE_SPACE_GB_AFTER - $FREE_SPACE_GB_BEFORE" | bc)
echo ""
echo "=== Results ==="
echo " Before: ${FREE_SPACE_GB_BEFORE}GB free"
echo " After: ${FREE_SPACE_GB_AFTER}GB free"
if (( $(echo "$FREED_GB > 15" | bc -l) )); then
echo " ✅ Successfully freed: ${FREED_GB}GB"
echo ""
echo " Your 20GB from deleted files should now be available!"
elif (( $(echo "$FREED_GB > 0" | bc -l) )); then
echo " ✅ Freed: ${FREED_GB}GB"
echo " (May need restart to free remaining space)"
else
echo " ⚠️ Space not freed yet"
echo ""
echo " Try restarting your Mac - this often releases stuck snapshots."
fi
echo ""
echo "=== Next Steps ==="
echo ""
echo "1. After space is freed, fix Time Machine connection:"
echo " See: time_machine_troubleshooting.md"
echo ""
echo "2. Common fixes for NAS connection:"
echo " - Clear credentials: Keychain Access → search 'DS224plus' → delete"
echo " - Re-add Time Machine: System Settings → Time Machine → Add Backup Disk"
echo ""
echo "3. Once connection is fixed, Time Machine will work properly."
echo ""