Skip to content

Instantly share code, notes, and snippets.

@DamnedFacts
Last active September 5, 2019 03:50
Show Gist options
  • Save DamnedFacts/85f2c33fc9782431314fe113cf8aab29 to your computer and use it in GitHub Desktop.
Save DamnedFacts/85f2c33fc9782431314fe113cf8aab29 to your computer and use it in GitHub Desktop.
A quick script to repair Time Machine sparsebundles. This should be a fix to the dreaded "To improve reliability, Time Machine must create a new backup for you" error.
#!/bin/bash
# From: https://jd-powered.net/notes/fixing-your-time-machine-backup/
ext=${1##*.}
if [[ "${ext}" != "sparsebundle" ]] || [[ -z ${1} ]]; then
echo "A sparsebundle disk image is required."
exit 1
fi
if [ ! -d "${1}" ]; then
echo "tm_repair.sh: ${1}: No such file or directory"
exit 1
fi
cmd="chflags -R nouchg \"${1}\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Unlocking disk image with 'chflags' failed."
exit $?
fi
cmd="hdiutil attach -nomount -noverify -noautofsck \"${1}\" | awk '/Apple_HFS/{ print \$1 }'"
echo Executing: ${cmd}
dev=`eval ${cmd}`
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Attaching disk image with 'hdiutil' failed."
exit $?
fi
cmd="fsck_hfs -drfy \"${dev}\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Checking disk with 'fsck_hfs' failed."
exit $?
fi
cmd="hdiutil detach \"${dev}\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Detaching disk image with 'hdiutil' failed."
exit $?
fi
cmd="plutil -replace VerificationDate -date `TZ=Zulu date +"%Y-%m-%dT%TZ"` \"${1}/com.apple.TimeMachine.MachineID.plist\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Updating 'VerificationDate' in ${1}/com.apple.TimeMachine.MachineID.plist failed."
exit $?
fi
cmd="plutil -replace VerificationState -integer 0 \"${1}/com.apple.TimeMachine.MachineID.plist\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Updating 'VerificationState' in ${1}/com.apple.TimeMachine.MachineID.plist failed."
exit $?
fi
cmd="plutil -remove RecoveryBackupDeclinedDate \"${1}/com.apple.TimeMachine.MachineID.plist\""
echo Executing: ${cmd}
eval ${cmd}
if [ $? -eq 1 ]; then
echo "tm_repair.sh: Removing 'RecoveryBackupDeclinedDate' in ${1}/com.apple.TimeMachine.MachineID.plist failed."
exit $?
fi
echo "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment