Files
nas-tools/clean_timemachine_system_files.sh
T

143 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Clean Time Machine System Files
# This script safely removes Time Machine files from system locations
set -e
echo "=== Time Machine System Files Cleanup ==="
echo ""
echo "⚠️ WARNING: This script will delete Time Machine system files."
echo " Make sure you understand what will be deleted."
echo ""
# Check current free space
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 ""
# Ask for confirmation
read -p "Do you want to proceed? This will delete Time Machine system files. (yes/no) " -r
echo ""
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
echo "Cancelled."
exit 0
fi
echo ""
echo "🧹 Cleaning Time Machine system files..."
echo ""
FREED_TOTAL=0
# 1. Delete local snapshots
echo "1. Deleting local snapshots..."
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
echo " Deleting snapshot: $snapshot"
sudo tmutil deletelocalsnapshots "$snapshot" 2>/dev/null || {
echo " ⚠️ Could not delete $snapshot (may require different method)"
}
fi
done
echo " ✓ Local snapshots deleted"
else
echo " ✓ No local snapshots to delete"
fi
echo ""
# 2. Disable local snapshots temporarily (they'll recreate but this helps)
echo "2. Disabling local snapshots (to prevent immediate recreation)..."
sudo tmutil disablelocal 2>/dev/null || echo " ⚠️ Could not disable local snapshots"
echo ""
# 3. Clean MobileBackups (if they exist and are safe to remove)
echo "3. Checking MobileBackups..."
if [ -d "/.MobileBackups" ] || [ -d "/System/Volumes/Data/.MobileBackups" ]; then
echo " ⚠️ MobileBackups directory found."
echo " These are APFS snapshots. They're usually managed automatically."
echo " Deleting them manually can cause issues."
echo " Recommendation: Let macOS manage these automatically."
echo " They will be deleted when space is needed."
read -p " Do you want to try deleting MobileBackups? (yes/no) " -r
if [[ $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
if [ -d "/.MobileBackups" ]; then
echo " Attempting to delete /.MobileBackups..."
sudo rm -rf /.MobileBackups/* 2>/dev/null || echo " ⚠️ Could not delete (may be in use)"
fi
if [ -d "/System/Volumes/Data/.MobileBackups" ]; then
echo " Attempting to delete /System/Volumes/Data/.MobileBackups..."
sudo rm -rf /System/Volumes/Data/.MobileBackups/* 2>/dev/null || echo " ⚠️ Could not delete (may be in use)"
fi
fi
else
echo " ✓ No MobileBackups directory found"
fi
echo ""
# 4. Find and remove orphaned sparse bundles
echo "4. Searching for orphaned Time Machine sparse bundles..."
find /private/var -name "*.sparsebundle" -o -name "*.sparseimage" 2>/dev/null | while read bundle; do
if [ -e "$bundle" ]; then
SIZE=$(sudo du -sh "$bundle" 2>/dev/null | awk '{print $1}')
echo " Found: $bundle ($SIZE)"
read -p " Delete this bundle? (yes/no) " -r
if [[ $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
sudo rm -rf "$bundle" 2>/dev/null && echo " ✓ Deleted" || echo " ⚠️ Could not delete"
fi
fi
done
echo ""
# 5. Clean Time Machine logs (safe, will regenerate)
echo "5. Cleaning Time Machine logs..."
sudo rm -rf /private/var/log/com.apple.TimeMachine* 2>/dev/null || true
echo " ✓ Logs cleaned"
echo ""
# 6. Re-enable local snapshots (they're useful, just need space first)
echo "6. Re-enabling local snapshots..."
sudo tmutil enablelocal 2>/dev/null || echo " ⚠️ Could not re-enable (may already be enabled)"
echo ""
# Check new free space
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 > 0" | bc -l) )); then
echo " Freed: ${FREED_GB}GB"
else
echo " Freed: 0GB (files may be in use or protected)"
fi
echo ""
if (( $(echo "$FREE_SPACE_GB_AFTER < 10" | bc -l) )); then
echo "⚠️ Still low on space. The 100GB+ may be in a protected location."
echo ""
echo "Next steps:"
echo "1. Check macOS Storage Management:"
echo " Apple Menu → About This Mac → Storage → Manage"
echo " Look for 'System Files' and see what's taking space"
echo ""
echo "2. The files may be APFS snapshots that need special handling:"
echo " sudo tmutil listlocalsnapshots /"
echo " # Then delete each one:"
echo " sudo tmutil deletelocalsnapshots <snapshot-name>"
echo ""
echo "3. Check for large files in system locations:"
echo " sudo du -h -d 1 /System/Volumes/Data | sort -hr | head -20"
echo ""
else
echo "✅ Good! You should have enough space for Time Machine now."
fi
echo ""