NOTE: Use these steps for APFS backups. If you have an HFS backup, use: https://gist.github.com/gadgetmies/3e1336d6d8733ecc199a5123671efd33
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.
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.
tmutil destinationinfo | egrep '^Name ' | sed 's#^Name *: ##g' | head -1 | xargs -n 2 -I {} sh -c "diskutil info \"{}\"" | grep "Volume UUID" | awk '{print $3}'
Edit fstab by running vifs
and add the following line to the file with your UUID:
UUID=YOUR-UUID-HERE none apfs rw,noauto
Run sudo automount -vc
to make the changes take effect.
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
PASSPHRASE=`security find-generic-password -l "$TM_DRIVE_NAME" -w | xxd -r -p`
diskutil apfs unlockVolume "$UUID" -passphrase "$PASSPHRASE"
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