Skip to content

Instantly share code, notes, and snippets.

@gadgetmies
Last active September 28, 2023 06:53
Show Gist options
  • Save gadgetmies/3e1336d6d8733ecc199a5123671efd33 to your computer and use it in GitHub Desktop.
Save gadgetmies/3e1336d6d8733ecc199a5123671efd33 to your computer and use it in GitHub Desktop.
Prevent unmount issues with Time Machine backup disk that is "permanently" connected to a USB dock (HFS version)

NOTE: Use these steps for HFS backups. If you have an APFS backup, use: https://gist.github.com/gadgetmies/d83382bc74aa5bc60dc24d04fd4d3605

I got frustrated with the "Disk Not Ejected Properly" warnings I often got when disconnecting my laptop from the USB dock because I had not unmounted the Time Machine backup disk first. Decided it's probably best to do something about the issue before the disk gets damaged.

The solution is rather simple and has three parts:

  • Store the disk encryption password in your keychain and always allow it's use for mounting the disk
  • Prevent automatic mounting of the backup disk to prevent it being mounted when disconnecting the laptop from the dock
  • Create an application that mounts the disk, runs the Time Machine backups and unmounts the disk when done

The application can be launched manually (e.g. from the MacOS dock) or be scheduled to run every now and then.

Always allow use of encryption password from keychain

If your backup disk is encrypted (and it should be), to store the password in keychain for automatically using it when mounting the disk, unmount / eject the disk using Finder or Disk Utility, and remount it. Insert your password and select always allow option from the popup asking for permission accessing the encryption password stored in the keychain. It probably would be possible to add a step in the Automator script for mounting the disk and entering the encryption password (i.e. diskutil coreStorage unlockVolume <UUID>), but I keep the password in the keychain to make things simple.

Prevent automatic mounting of the backup disk

Find out the backup disk UUID

tmutil destinationinfo | egrep '^Name  ' | sed 's#^Name  *: ##g' | head -1 | xargs -n 2 -I {} sh -c "diskutil info \"{}\"" | grep "Volume UUID" | awk '{print $3}'

Add an entry to fstab

Edit fstab by running vifs and add the following line to the file with your UUID:

UUID=YOUR-UUID-HERE none hfs rw,noauto

Run sudo automount -vc to make the changes take effect.

Create an application for mounting, running the backup and unmounting

Create a Automator application and add a "Run Shell Script" action.

Add the following to the action:

TM_DRIVE_NAME=$(tmutil destinationinfo | egrep '^Name  ' | sed 's#^Name  *: ##g' | head -1)
UUID=$(diskutil info "$TM_DRIVE_NAME" | grep "Volume UUID" | awk '{print $3}')

STATUS=$(tmutil currentphase)

if [[ "$STATUS" != "BackupNotRunning" ]]
then
	echo "$NAME: Time Machine status is '$STATUS'. Should be 'BackupNotRunning'." >>/dev/stderr
	exit 0
fi

MNTPNT="/Volumes/$TM_DRIVE_NAME"

if [[ -d "$MNTPNT" ]]
then
	echo "$NAME: '$MNTPNT' is already mounted".
else
	diskutil mount "$UUID"
fi

if [[ ! -d "$MNTPNT" ]]
then
	echo "$NAME: Failed to mount '$MNTPNT'." >>/dev/stderr
	exit 0
fi

TM_DRIVE_ID=$(tmutil destinationinfo | egrep '^ID  ' | sed 's#^ID  *: ##g' | head -1)
tmutil startbackup --block --destination "$TM_DRIVE_ID"

EXIT="$?"

if [[ "$EXIT" == "0" ]]
then
	echo "$NAME: Finished successfully at `timestamp`."
else
	echo "$NAME: Finished UN-successfully (Exit = $EXIT) at `timestamp`."
fi

while [[ -d "$MNTPNT" ]]
do
	# this will try to unmount the drive as long as it is mounted
	diskutil unmount "$UUID"
	sleep 10
done

Save, run and enjoy!

Sources: https://talk.macpowerusers.com/t/any-way-to-automate-a-time-machine-backup-mount-backup-unmount/16758/10?utm_source=pocket_saves https://discussions.apple.com/docs/DOC-7942

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