A simple set of steps to create a simple script that is run every hour to automatically eject your TimeMachine disk after backup. Specifically, the script as written will eject the disk if a backup was completed today.
Why? I hate having to remember doing it manually, and have corrupted a hard-drive by removing it physically without ejecting it first.
timeMachineScript.sh
:
- Change line 9: use name the UUID of your backup harddisk (Assuming the back up disk is mounted and named as "Backups of ..." --
diskutil list | grep "Backups of" | awk '{print $NF}' | xargs diskutil info | grep "Volume UUID:"
command can be used to display the UUID of the backup disk) - Run
chmod +x timeMachineScript.sh
- Move the file to a location of your choice (e.g.
~/bin/timeMachineScript.sh
) - (tmutil: latestbackup requires Full Disk Access privileges) Give
timeMachineScript.sh
Full Disk Access rights by openning the System Settings > Privacy & Security > Full Disk Access window and dragging & dropping thetimeMachineScript.sh
file into it. - This modification assumes that the disk is encyrpted and its password is registered in the master keychain.
security
binary will ask permission to access this login item (which can be find by searching the UUID in Keychain Access.app). Basically the script will query the password of the backup disk everytime it runs so that it can mount the disk.
com.username.timeMachineScript.plist
:
- Change the name of this file: use your Mac username (e.g.
com.micah.timeMachineScript.plist
) - Change line 6: use your Mac username (e.g.
com.micah.timeMachineScript
) - Change line 9: use the path to the bash script chosen for
timeMachineScript.sh
- Modify lines 16 and 18 to your liking or remove lines 15 through 18 altogether if you don't want logging
- Move the file to
/Users/username/Library/LaunchAgents
(whereusername
is replaced with your Mac username) - Run
launchctl load ~/Library/LaunchAgents/com.username.timeMachineScript.plist
(whereusername
is replaced with your Mac username)
Use it at your own risk.