Skip to content

Instantly share code, notes, and snippets.

@krispayne
Last active March 20, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krispayne/8b5380b08ab20b100970f001c3f5f0e5 to your computer and use it in GitHub Desktop.
Save krispayne/8b5380b08ab20b100970f001c3f5f0e5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Mount a network time machine sparsebunble who's claimed to have gotten corrupt and no longer backs up.
# I do this from a different mac than the one doing the backups.
if [[ $(whoami) != 'root' ]]; then
echo "Must be run as root, dude."
exit 1
fi
read -p 'Enter path of TimeMachine Server (i.e. 123.45.6.7/TimeMachine): ' TM_PATH
read -p 'Enter sparsebundle name: ' TM_NAME
read -p 'Enter Username: ' USERNAME
read -s -p 'Enter Password: ' PASSWORD
echo " "
echo "For encrypted backups you will be asked for the password to the disk after chflags is run"
echo " "
echo "---------------------"
MOUNT=/Volumes/TimeMachine
SPARSEBUNDLE="$MOUNT"/"$TM_NAME".sparsebundle
PLIST="$SPARSEBUNDLE"/com.apple.TimeMachine.MachineID.plist
echo "Mounting volume"
mkdir "$MOUNT"
mount_afp afp://"$USERNAME":"$PASSWORD"@"$TM_PATH" "$MOUNT"
echo "Changing file and folder flags. This can take some time..."
chflags -R nouchg "$SPARSEBUNDLE"
echo "Attaching sparse bundle"
DISK=`hdiutil attach -nomount -readwrite -noverify -noautofsck "$SPARSEBUNDLE" | grep Apple_HFS | cut -f 1`
echo "$DISK"
echo "Repairing volume. This WILL take some time..."
#diskutil repairVolume $DISK
/sbin/fsck_hfs -p "$DISK"
/sbin/fsck_hfs -fry "$DISK"
echo "Fixing Properties"
cp "$PLIST" "$PLIST.backup"
sed -e '/RecoveryBackupDeclinedDate/{N;d;}' \
-e '/VerificationState/{n;s/2/0/;}' \
"$PLIST.backup" \
> "$PLIST"
echo "Unmounting volumes"
hdiutil detach /dev/"$DISK"
umount "$MOUNT"
exit 0
@krispayne
Copy link
Author

krispayne commented Mar 17, 2017

This has worked reasonably well for me. It's gathered from several different blog posts with steps guiding people how to do it. I should go and find those again to give credit.

When I was committing/sanitizing, I added the read for the IP/path to the Time Machine share. I was using a hardcoded value before, but this should work.

@krispayne
Copy link
Author

krispayne commented Mar 17, 2017

it also has trouble dismounting sometimes. :-/

@krispayne
Copy link
Author

diskutil unmountDisk $DISK
hdiutil detach /dev/$DISK

These might be better that the umount commands in the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment