Skip to content

Instantly share code, notes, and snippets.

@joshuata
Created January 30, 2019 02:14
Show Gist options
  • Save joshuata/00bc8ef27c0fc7e573a0fa863fc39216 to your computer and use it in GitHub Desktop.
Save joshuata/00bc8ef27c0fc7e573a0fa863fc39216 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
if [[ $(whoami) != 'root' ]]; then
exit 1
fi
read -r -p 'Is this a Samba share? [Y/n] ' SMB
read -r -p 'Enter Time Machine Hostname: ' HOSTNAME
read -r -p 'Enter Share: ' SHARE
read -r -p 'Enter Username: ' USERNAME
TM_NAME=$(hostname -s | sed -e 's/-/ /g')
MOUNT=/Volumes/TimeMachine
SPARSEBUNDLE=$MOUNT/$TM_NAME.sparsebundle
PLIST=$SPARSEBUNDLE/com.apple.TimeMachine.MachineID.plist
echo ""
echo "Disabling Time Machine"
tmutil disable
echo "Mounting volume"
mkdir -p $MOUNT
if [[ "$SMB" =~ ^([nN][oO]|[nN])+$ ]]
then
mount_afp -i "afp://$USERNAME@$HOSTNAME/$SHARE" "$MOUNT"
else
mount_smbfs "smb://$USERNAME@$HOSTNAME/$SHARE" "$MOUNT"
fi
function cleanup {
if [ ! -z ${DISK-} ]; then
echo "Detaching disk image"
hdiutil detach "$DISK"
fi
echo "Unmounting Share"
umount "$MOUNT"
echo "Re-Enabling Time Machine"
tmutil enable
}
trap cleanup EXIT
echo "Changing file and folder flags"
chflags -R nouchg "$SPARSEBUNDLE"
echo "Attaching sparse bundle"
DISK=$(hdiutil attach -nomount -noverify -noautofsck "${SPARSEBUNDLE}" | grep Apple_HFS | cut -f 1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
echo "$DISK"
read -r -p "Press enter to continue"
echo "Repairing volume"
diskutil repairvolume $DISK
echo "Fixing Properties"
cp "$PLIST" "$PLIST.backup"
sed -e '/RecoveryBackupDeclinedDate/{N;d;}' \
-e '/VerificationState/{n;s/2/0/;}' \
"$PLIST.backup" \
> "$PLIST"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment