Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active July 7, 2021 00:16
Show Gist options
  • Save n8henrie/86ab009c72d80a99321671c0c10a3880 to your computer and use it in GitHub Desktop.
Save n8henrie/86ab009c72d80a99321671c0c10a3880 to your computer and use it in GitHub Desktop.
Fix time machine backup
#!/usr/bin/env bash
# http://tonylawrence.com/post/unix/fixing-corrupted-time-machine-backups/
set -euf -o pipefail
set -x
if [[ $(whoami) != 'root' ]]; then
echo "Please run as root" > /dev/stderr
exit 1
fi
SPARSEBUNDLE=${1-"/Volumes/Airport Drive Backup/$(hostname -s).backupbundle"}
[ -e "${SPARSEBUNDLE}" ] || { echo "Can't find $SPARSEBUNDLE"; exit 1; }
PLIST="$SPARSEBUNDLE/com.apple.TimeMachine.MachineID.plist"
echo "Disabling Time Machine"
tmutil disable
echo "Changing file and folder flags"
chflags -R nouchg "$SPARSEBUNDLE"
echo "Attaching sparse bundle"
DISK="$(\
hdiutil attach -nomount -readwrite -noverify -noautofsck "$SPARSEBUNDLE" |
awk '/Apple_HFS/ { print $1 }'\
)"
echo "Repairing volume"
diskutil unmountDisk "$DISK"
/sbin/fsck_hfs -drfy -c 2200 "$DISK"
# diskutil repairVolume "$DISK"
echo "Fixing Properties"
sed -i.bak \
-e '/RecoveryBackupDeclinedDate/{N;d;}' \
-e '/VerificationState/{n;s/2/0/;}' \
"$PLIST"
echo "Unmounting volumes"
hdiutil detach "$DISK"
echo "Enabling Time Machine"
tmutil enable
echo "Starting backup"
tmutil startbackup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment