Parses Time Machine logs and tmutil
output. The goal is to automatically unmount USB drives after backups complete: BrianHenryIE/UnmountVolumeAfterTimeMachine
$ /usr/bin/tmutil
$ /usr/bin/log stream --predicate 'subsystem == "com.apple.TimeMachine"' --info
let tmUtil = TmUtil()
// Parses `tmutil destinationinfo`
let destinationInfo = tmUtil.destinationInfo()
if let volume = destinationInfo?.destinations.first?.mountPoint {
print("Time machine drive mounted at: \(volume)")
}
let tmUtil = TmUtil()
// Parses `tmutil status`
let status = tmUtil.status()
if let running = status?.running {
print("Time machine is\(running ? "" : " not") currently running.")
}
TimeMachineLog.shared.addObserver(
self,
selector: #selector(unmountVolume),
name: Notification.Name.TimeMachineLogAfterCompletedBackup,
object: nil
)
.package( url: "https://github.com/BrianHenryIE/SwiftTimeMachine", branch: "master" )
You might want to pin that to a commit in case I change things. I don't write Swift in my day job (but this was mostly test driven).
Sometimes does thinning
Completed backup: 2022-05-21-120849
Mountpoint '/Volumes/8tb' is still valid
Thinning 3 backups using age-based thinning, expected free space: 678.7 GB actual free space: 678.7 GB trigger 50 GB thin 83.33 GB dates: (
"2022-05-18-145930",
"2022-05-18-155714",
"2022-05-18-165813"
)
Mountpoint '/Volumes/8tb' is still valid
Doesn't always do thinning
Completed backup: 2022-05-21-175757
Mountpoint '/Volumes/8tb' is still valid
Mountpoint '/Volumes/8tb' is still valid
This project currently uses Swift OS Log Stream which tail
s and parses /usr/bin/log
. Instead of that, it should use OSLogStore, particularly because it currently needs admin access, or better yet, DistributedNotificationCenter.
DistributedNotificationCenter.default().addObserver(self, selector: #selector(handleNotifications), name: nil, object: nil)
"You have to filter the notifications related to Time Machine. You can also observe specific notifications via the name parameter"
tmutil
documentation. There might be better official documentation elsewhere, but this is where I learned about the -X
flag to output the response as a plist/XML.
Deep dive into Time Machine logs.
More detailed Time Machine notes
Discussion of Time Machine with many tmutil
command examples.