#!/bin/bash # Fix Stuck Time Machine Backup # When Time Machine is stuck trying to connect to NAS and consuming space set -e echo "=== Fix Stuck Time Machine Backup ===" echo "" # Check Time Machine status TM_STATUS=$(tmutil status 2>/dev/null) echo "📊 Current Time Machine Status:" echo "$TM_STATUS" | grep -E "BackupPhase|Running" | sed 's/^/ /' echo "" # Check if it's stuck if echo "$TM_STATUS" | grep -q "Running = 1"; then PHASE=$(echo "$TM_STATUS" | grep "BackupPhase" | sed 's/.*BackupPhase = \([^;]*\).*/\1/') echo "⚠️ Time Machine is RUNNING and stuck in phase: $PHASE" echo "" echo " This is why your space disappeared:" echo " - Time Machine created a local snapshot before backing up" echo " - It's trying to connect to NAS but can't" echo " - The snapshot is holding your deleted file space" 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 "" read -p "Stop the stuck Time Machine backup? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then echo "" echo "🛑 Stopping Time Machine backup..." # Stop the backup sudo tmutil stopbackup 2>/dev/null || { echo " ⚠️ Could not stop via tmutil, trying alternative method..." # Kill the backupd process (more aggressive) sudo killall backupd 2>/dev/null || true sleep 2 } echo " ✓ Backup stopped" echo "" # Wait a moment echo "⏳ Waiting for processes to clean up..." sleep 5 # Check if it stopped NEW_STATUS=$(tmutil status 2>/dev/null | grep "Running" || echo "Running = 0") if echo "$NEW_STATUS" | grep -q "Running = 0"; then echo " ✅ Time Machine backup stopped" else echo " ⚠️ Backup may still be running, may need to restart" fi echo "" # Now try to delete the snapshot echo "🗑️ Attempting to delete local 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 " ✅ Snapshot deleted!" else echo " ⚠️ Still stuck - may need to remove Time Machine destination first" fi fi fi done fi echo "" echo "📊 Checking disk space..." sleep 3 NEW_FREE=$(df / | tail -1 | awk '{print $4}') NEW_FREE_GB=$(echo "scale=2; $NEW_FREE / 1024 / 1024" | bc) FREED_GB=$(echo "scale=2; $NEW_FREE_GB - $FREE_SPACE_GB" | bc) echo " Before: ${FREE_SPACE_GB}GB free" echo " After: ${NEW_FREE_GB}GB free" if (( $(echo "$FREED_GB > 0" | bc -l) )); then echo " ✅ Freed: ${FREED_GB}GB" else echo " ⚠️ Space not freed yet" fi echo "" echo "=== Next Steps ===" echo "" echo "1. If space is still not freed, remove Time Machine destination:" echo " System Settings → Time Machine → Remove backup disk" echo " Then delete snapshot: sudo tmutil deletelocalsnapshots " echo "" echo "2. Fix Time Machine connection issue:" echo " See: time_machine_troubleshooting.md" echo " Common fixes:" echo " - Clear credentials in Keychain Access" echo " - Unmount any manual mounts" echo " - Re-add Time Machine destination" echo "" echo "3. After fixing connection, re-add Time Machine:" echo " System Settings → Time Machine → Add Backup Disk" echo "" else echo "Cancelled." fi else echo "✅ Time Machine is not running" echo "" echo "If space is still missing, check for snapshots:" echo " tmutil listlocalsnapshots /" echo "" fi echo ""