MacOS – Help Unmouting disk if UUID is mounted until a Specific Time

backupdiskutilmacosmounttime-machine

Every morning I have the same routine.

I come into work and plug in my peripherals including my Time Machine Backup. I have my Time Machine scheduled to only run one daily backup at 3:30pm. I would like to run a script or a .plist LaunchDeamon to check all inserted external hard drives automatically and unmount if the

UUID = 32E89C07-FFDB-36F3-8CE8-5DC0560AFEC4  

And then mount it at 3:15pm. I just do not want my drive to be plugged in all day just to use it for 30 mins to back everything up.

I know this could all be resolved if I just manually plug in the drive at 3:15. However, some days I am in a meeting or away from the desk and what-not.

I was able to find documentation on how to auto eject the device at a specific time so I do not have to remember to safely remove the drive.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
            <string>com.diskejector.Backup</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/sbin/diskutil</string>
            <string>unmountDisk</string>
            <string>32E89C07-FFDB-36F3-8CE8-5DC0560AFEC4</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
                <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
    </dict>
</plist>

I would like some help with auto launching a .sh script that could also include the previous code I have to eject the drive at the end of the day.

Best Answer

There is no shell script needed if you use two launch agents.

You need the UUID of your Time Machine backup drive:

diskutil info $VolumeName | grep "Disk / Partition UUID:"

Replace $VolumeName by the actual volume name of your TM drive. If the name contains spaces use double quotes (e.g. "Time Machine backup drive".

Create two files usr.tmvolume.unmount.plist and usr.tmvolume.mount.plist in ~/Library/LaunchAgents/:

nano ~/Library/LaunchAgents/usr.tmvolume.mount.plist

with the content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>usr.tmvolume.mount</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/diskutil</string>
        <string>mount</string>
        <string>$UUID</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>15</integer>
            <key>Minute</key>
            <integer>15</integer>
        </dict>
    </array>
</dict>
</plist>

and

nano ~/Library/LaunchAgents/usr.tmvolume.unmount.plist

with the content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>usr.tmvolume.unmount</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/diskutil</string>
        <string>unmount</string>
        <string>$UUID</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>8</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>15</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
    </array>
</dict>
</plist>

Replace $UUID in both plists by the UUID of your Time Machine volume found previously.

Load both plists with:

launchctl load ~/Library/LaunchAgents/usr.tmvolume.*

Now the TM backup drive will be unmounted at 8:30am, mounted at 3:15pm and unmounted again at 3:45pm. Adjust the dates as needed.