nielsmouthaan / ejectify-macos

Ejectify automatically unmounts external volumes when your Mac starts sleeping, and mounts them again after it wakes up.
https://ejectify.app
Other
371 stars 50 forks source link

Automatically unmount volume when Time Machine is done making a backup #19

Closed marcheimendinger closed 1 year ago

marcheimendinger commented 3 years ago

Time Machine disk is often a hard drive and when not backing up, it tends to make Finder lag when opening a new window if the disk was asleep. A useful option to solve this issue would be to auto-unmount the Time Machine disk while not used by the backupd task. And auto-mount it when needed (every hour, maybe this could be customisable in the utility).

nielsmouthaan commented 3 years ago

Thanks for the suggestion 👍.

vordenken commented 2 years ago

This would be really great!

BrianHenryIE commented 2 years ago

I was working on this today and have it working in principle.

I'm reading the output of /usr/bin/log stream --predicate 'subsystem == "com.apple.TimeMachine"' --info using a Process() and Pipe(), using regex to parse the log string, then firing a notification depending on the message content.

2022-05-21 19:28:21.720633-0700 0x10e421   Info        0x186886             75012  0    backupd: (TimeMachine) [com.apple.TimeMachine:General] Completed backup: 2022-05-21-192820
let logParserPattern = #"""
    (?<timestamp>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{6}-\d{4})
    \s+
    (?<thread>0x\w+)
    \s+
    (?<type>\w+)
    \s+
    (?<activity>0x\w+)
    \s+
    (?<pid>\d+)
    \s+
    (?<ttl>\d+)
    \s+
    (?<context>.*?)
    \s
    \[(?<namespace>.*?):(?<namespacecontext>.*?)\]
    \s+
    (?<message>.*)
"""#

I'm still figuring out how to handle "Cleaning Up..." ("thinning") which happens after the Completed backup log message.

2022-05-21 19:28:21.763071-0700 0x10e421   Info        0x186886             75012  0    backupd: (TimeMachine) [com.apple.TimeMachine:BackupThinning] Thinning 2 backups using age-based thinning, expected free space: 674.59 GB actual free space: 674.59 GB trigger 50 GB thin 83.33 GB dates: (
    "2022-05-21-172136",
    "2022-05-21-175757"
)

This doesn't seem to have an explicit "complete" log message but is usually followed by:

2022-05-21 19:35:09.029014-0700 0x10e421   Info        0x186886             75012  0    backupd: (TimeMachine) [com.apple.TimeMachine:General] Mountpoint '/Volumes/8tb' is still valid

but that message is also sometimes logged during cleanup.

So I think I'm going to use tmutil status when I see that to ensure Time Machine is really complete and the disk is really ready to unmount.

Backup session status:
{
    ClientID = "com.apple.backupd";
    Running = 0;
}

If anyone knows a real API I should be using for this (instead of parsing text logs and calling shell commands!) let me know.

I'll take another look at this soon and make a PR.

marcheimendinger commented 2 years ago

Maybe you could have a look at how this tool is doing it: https://github.com/ProsoftEngineering/TimeMachineRemoteStatus

gagarine commented 2 years ago

tmutil let you know if a backup is running or not using tmutil status or tmutil currentphase.

Using tmutil listbackups list all backup made recently and tmutil latestbackup -t give you the date of the last one.

This allow to check if a backup was made recently and unmnunt the disk if that the case and no backup are running. To start the backup right after the disk is mounted tmutil startbackup.

ejectify could mount the disk every X hours and unmount the disk when the backup is done.

The settings could be like "Time Machine Backup" -> "every 1 hours", "every 6 hours", "every 12 hours", "every day"

nielsmouthaan commented 1 year ago

I'm closing this issue as I consider it out-of-scope for Ejectify. I would recommend reaching out to the developer behind TimeMachineEditor and asking if he could add a mechanism to automatically unmount the volume when Time Machine is done making a backup.

BrianHenryIE commented 1 year ago

I've been working on this more recently: BrianHenryIE/UnmountVolumeAfterTimeMachine