Detect if Time Machine is active

Hello again! I’m working on my second script, which ironically should be more simple than the first.

I am doing some research on Time Machine, and I’ve developed a little script that will turn it on, launch a backup, and then turn it off. However, it is also vital that I can time how long it took for the backup to complete. I’ve got some code in place for this, but it stops at 20 seconds… apparently no matter what(?)

My thought is that if I could somehow check if the Time Machine process is running, I could tell the script to not stop. I’d be more than open to any other suggestions, however.

Here’s what I have so far:


set startTime to (get current date)
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool true" --Turns Time Machine on
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper -auto" --Tells Time Machine to begin backing up
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool false" --Finally, turns Time Machine back off
set endTime to (get current date)
set duration to endTime - startTime
set hrs to duration div hours
set mins to duration div minutes
set secs to duration mod minutes
display dialog "The backup took " & hrs & " hours, " & mins & " minutes and " & secs & " seconds."

As always, any help is much appreciated. I need to figure this out as soon as possible.

Hi,

it’s not necessary to turn Time Machine on and off. This line runs a backup manually


do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper"

To check the status either poll the existence of the process backupd with something like this


on checkBackupdStatus()
	try
		(do shell script "/bin/ps -arxo state,comm | /usr/bin/grep backupd")
		return true
	on error
		return false
	end try
end checkBackupdStatus

or check the Backups.backupdb:[computername] folder on the backup volume.
During the backup there’s a file with extension .inProgress

The problem with do shell script “/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper” is that it freezes your script for a while.

It’s looking to me like the backupd process continues to run for a while after Time Machine has long since finished backing up, so I am not entirely sure what to do. Is backupd-helper a separate entity from backupd?

ok, this avoids the problem


do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper > /dev/null 2>&1 &"

Backupd-helper is the executable of backupd

Could I somehow employ the Unix time command? Something like

set theTimer to (do shell script "time /System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper > /dev/null 2>&1 &")

I tried that, putting theTimer into a dialog box, but no luck; it would be nice if it was that easy. However, the dialog box appears before the Time Machine backup even finishes; I’m wondering if it’s even possible to get an accurate time of how long the backup takes.

I hate to bump my own topic, but I am still bashing my head against the wall with this project. Does anyone have any ideas on how to time a Time Machine backup? Something I’m doing just isn’t working.

Actually I described a way in post #2

In my tool TimeMachineScheduler I’m using the mentioned line


do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper"

which waits until the command has been finished.
Before calling this line I read the date of the latest backup on the backup volume (/Volumes/myBackupVolume/Backups/backupdb/[computername]).
Then the script checks when the file xyz.inProgress has gone and the original of the “latest” alias has changed.

I appreciate your help very much, StefanK, but I admit my understanding of AppleScript may just not be good enough to understand what to do with what you posted.

Currently, I have:


set startTime to (get current date)
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper" --Tells Time Machine to begin backing up

repeat
	if checkbackupstatus is true then
		delay 0.1
		set endTime to (get current date)
		set duration to endTime - startTime
		set hrs to duration div hours
		set mins to duration div minutes
		set secs to duration mod minutes
		display dialog "The backup took " & hrs & " hours, " & mins & " minutes and " & secs & " seconds."
	end if
	if checkbackupstatus is false then
		display dialog "The Time Machine process doesn't seem to be running."
	end if
end repeat

--SUBROUTINES

--checkBackupStatus subroutine by StefanK of macscripter.net, who is officially awesome
on checkBackupdStatus()
	try
		(do shell script "/bin/ps -arxo state,comm | /usr/bin/grep backupd")
		return true
	on error
		return false
	end try
end checkBackupdStatus

This launches the backup successfully, but I never get any dialog box or anything afterwards; evidently the script just quits.

easiest version


property backupVolume : "myVolume"
property timeoutValue : 15 * minutes

set startTime to (get current date)
set currentHost to computer name of (system info)
set backupFolder to (backupVolume & ":" & "Backups.backupdb" & ":" & currentHost)
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper" --Tells Time Machine to begin backing up
set t to 0
repeat
	if not ((do shell script "/bin/ls " & quoted form of POSIX path of backupFolder) contains "inProgress") or t > timeoutValue then exit repeat
	delay 1
	set t to t + 1
end repeat
if t > timeoutValue then
	display dialog "Backup seems to be still in progress. Please check file with extension \".inProgress\""
else
	set endTime to (get current date)
	set duration to endTime - startTime
	set hrs to duration div hours
	set mins to duration div minutes
	set secs to duration mod minutes
	display dialog "The backup took " & hrs & " hours, " & mins & " minutes and " & secs & " seconds."
end if

This is potentially a dumb question, but I’ve spent the last half hour fighting it - is there any reason why using a network share in place of “myVolume” would cause this script to not work (giving a ‘no such file or directory’ error)?

Yes, it is. As you gave no further information, I assumed you’re using just a local volume.
The Time Capsule for example uses a sparse image, but in this case you could check the existence of the mounted image

My apologies; the volume is stored on a Time Machine server on the network, accessed (of course) with AFP.

I am thinking of just telling Finder to mount the drive, and then to use that volume name as myVolume. Then, when the script finishes, the drive could be unmounted. Does that make sense?

the script should work with a normal AFP share, but it could be, that you have some special characters
in the computer name, which are resolved by the OS. Take a look at the backup volume and try to hard-code the path
to the backup folder

No matter what I do, it’s failing out at

 if not ((do shell script "/bin/ls " & quoted form of POSIX path of backupFolder)

Saying that there’s no such file or directory. I have tried entering the IP of the system followed by the path to the directory, I’ve tried entering the hostname, and I’ve tried about five variants of each - no luck. It seems like something is wrong other than the mounting of the drive, because even though the script fails the backup completes successfully. It’s apparently failing when trying to look for the inprogress file.

can you please open the backup folder on your shared volume (the one with the yyyy-mm-dd-HHMMSS folders) and command-click in the title of the window.
You will see the path. As mentioned before probably you have special characters in the host name (e.g. the OS will change Fooruman’s computer to Foorumans-computer)

Though it’s located on a server, it just shows up as Volumes/Backups for the path of the .sparsebundle. The sparsebundle’s name is dkschroeder-m-25’s Mac mini_############.sparsebundle, which gets mounted also in /Volumes. The #s represent my MAC address, of course.

I did try replacing the special characters with their POSIX equivalents, and had no luck.

Thanks so much for all the help, StefanK, it seems like this is almost working.

My Time Capsule backup has the path /Volumes/Backup of [hostname]/Backups.backupdb/[hostname]/

Sorry, there are several options to use Time Machine, it’s very difficult to guess the exact configuration

PS: It’s strongly recommended to avoid any special character in the host names

Maybe this is a better solution, or at least one that can let me start doing my work:

Is there a way for the script to just find out what the currently set backup volume is and use that? In other words, it would have to be manually set.