Skip to content

Instantly share code, notes, and snippets.

@ahknight
Last active August 9, 2023 20:25
Show Gist options
  • Save ahknight/dec202583a910756c6d9 to your computer and use it in GitHub Desktop.
Save ahknight/dec202583a910756c6d9 to your computer and use it in GitHub Desktop.
When Time Machine asks you to delete your backups and start over, ignore it. Run this and then start a backup again.
#!/bin/bash -x
# Generally based on ideas found at:
# http://www.garth.org/archives/2011,08,27,169,fix-time-machine-sparsebundle-nas-based-backup-errors.html
#
# Reduced the ideas there down to their essentials.
# 1. Unlock the image.
# 2. Reset the saved failure in the backup metadata.
# 3. Verify/fix the filesystem.
# Take the arg. You did provide an arg, right?
IMAGE="$1"
if [ -z "$IMAGE" ]; then echo "usage: $0 image_path"; exit; fi
# Repair the stupid file lock.
chflags -v nouchg "$IMAGE"
chflags -v nouchg "$IMAGE/token"
chflags -v nouchg "$IMAGE/bands"
# Fix the plists
/usr/libexec/PlistBuddy -c "Delete :RecoveryBackupDeclinedDate" "$IMAGE/com.apple.TimeMachine.MachineID.plist"
/usr/libexec/PlistBuddy -c "Set :VerificationState 0" "$IMAGE/com.apple.TimeMachine.MachineID.plist"
# Start bailing on errors (can't set earlier due to PlistBuddy)
set -e
# Attach the image.
DEV=`hdiutil attach -nomount -noverify -noautofsck "$IMAGE" | awk '/HFS/ {print $1}'`
echo "$IMAGE -> $DEV"
# Fix the FS.
fsck_hfs -fy -c 8gb "$DEV"
# Detach it.
hdiutil detach "$DEV"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment