#!/bin/bash # Fix Stuck Time Machine Snapshot # When snapshots can't be deleted due to "Stale NFS file handle" error set -e echo "=== Fix Stuck Time Machine Snapshot ===" echo "" echo "This script fixes the 'Stale NFS file handle' error that prevents" echo "snapshot deletion. This happens when Time Machine has a stale" echo "connection to the backup destination." echo "" # Check current situation echo "📊 Current Status:" FREE_SPACE=$(df / | tail -1 | awk '{print $4}') FREE_SPACE_GB=$(echo "scale=2; $FREE_SPACE / 1024 / 1024" | bc) echo " Free space: ${FREE_SPACE_GB}GB" echo "" SNAPSHOTS=$(tmutil listlocalsnapshots / 2>/dev/null | grep -v "Snapshots for disk" | grep -v "^$") if [ -n "$SNAPSHOTS" ]; then echo " Stuck snapshots found:" echo "$SNAPSHOTS" | sed 's/^/ - /' else echo " ✓ No snapshots found" exit 0 fi echo "" echo "🔧 Solution: Temporarily remove Time Machine destination, delete snapshot, then re-add" echo "" echo "⚠️ IMPORTANT: This will temporarily disconnect Time Machine from your NAS." echo " You'll need to re-add it after the snapshot is deleted." echo "" read -p "Do you want to proceed? (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Cancelled." exit 0 fi echo "" echo "Step 1: Removing Time Machine destination..." echo "" echo " Please do this manually:" echo " 1. Open System Settings → General → Time Machine" echo " 2. Click the 'Remove' or '-' button next to your backup disk" echo " 3. Confirm removal" echo "" read -p " Have you removed the Time Machine destination? (y/n) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo " Please remove it first, then run this script again." exit 1 fi echo "" echo "Step 2: Waiting for Time Machine to disconnect..." sleep 5 echo "" echo "Step 3: Attempting to delete stuck snapshot..." echo "$SNAPSHOTS" | while read snapshot; do if [ -n "$snapshot" ]; then # Extract date part 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." echo " After restart, run: sudo tmutil deletelocalsnapshots $SNAP_DATE" fi fi fi done echo "" echo "Step 4: Checking disk space..." sleep 3 NEW_FREE_SPACE=$(df / | tail -1 | awk '{print $4}') NEW_FREE_SPACE_GB=$(echo "scale=2; $NEW_FREE_SPACE / 1024 / 1024" | bc) FREED_GB=$(echo "scale=2; $NEW_FREE_SPACE_GB - $FREE_SPACE_GB" | bc) echo " New free space: ${NEW_FREE_SPACE_GB}GB" if (( $(echo "$FREED_GB > 0" | bc -l) )); then echo " ✅ Freed: ${FREED_GB}GB" else echo " ⚠️ Space not freed yet - may need restart" fi echo "" echo "Step 5: Re-adding Time Machine destination..." echo "" echo " Now re-add your Time Machine backup:" echo " 1. System Settings → General → Time Machine" echo " 2. Click 'Add Backup Disk...'" echo " 3. Select 'Time Machine Folder' on 'DS224plus.local'" echo " 4. Enter your credentials" echo "" echo " Or use the troubleshooting guide:" echo " See: time_machine_troubleshooting.md" echo ""