Question

I'm writing a script that will rsycnc my Time Machine directory to a remote server using ssh. I've switched from a shell script to an AppleScript and then back to an Apple script and I don't really care what kind of script the solution will require.

I've had success starting the backup process with this little snippet of code.

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

What I'm trying to do now is have an rsync command performed once that backup is completed. Is there any clean way in AppleScript to check to see if the backup process is completed? Or is there a good "hook" that you can use in bash to check to see if it's completed?

Was it helpful?

Solution

Here's one way - I know it's nowhere near a "proper" solution, but I imagine it would work.

ps ax | grep "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper" | grep -v 'grep'

What it does is search the process list for the backupd-helper process, then filters out the grep command itself from showing up. If the command returns > 0 results, the backupd-helper process is still active. If not, the process has ended, and so you might assume it's done.

OTHER TIPS

tmutil status is the Lion way now that we have a nice tool for this sort of query.

You might also start the backup with tmutil startbackup -b. -b blocks the command until the backup has finished.

Absence of backup-related processes is not assurance that a Time Machine backup is complete

On the volume to which Time Machine writes its backups: alongside the …/Latest/… directory, consider:

  • the ….inProgress/… bundle.

If that bundle exists, a backup is incomplete.

tmutil status|grep -c "Running = 1"

returns '1' when running, '0' when not.

The following work for me...


Script backup_status:

    #!/usr/bin/env bash
    #
    # Determines if Time Machine is running and works with older Mac OS x versions
    #
    if [ x == x`type -P tmutil` ]; then
            # Older OSx
            count=`ps -ef | grep -v grep | grep -c 'CoreServices/backupd'`
            # Should also detect /Volume/Time Machine …/Latest/… directory ….inProgress/… bundle existence here to make it tight.
    else
            # OSx 10.7+
            count=`tmutil status | grep -c "Running = 1"`
    fi

    if [ $count == 0 ]; then
            echo stopped
    else
            echo running
    fi

Script backup_wait:

    #!/usr/bin/env bash

    #
    # Waits for Time Machine backup to complete
    #
    while [ `backup_status` == running ] ; do
            sleep 2
    done

tmutils only exists from 10.7 on. Before that you can use, as mentioned, /System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper to manually run Time Machine.

You can then use wait to wait until the process is finished. Without arguments it will simply wait; if you provide the PID you can get its return value (exit status).

Particularly, wait $! will wait for the last process started in that shell. However, as Lauri mentioned, this wait will return before time machine is finished because backupd-helper finishes before the backup is done. I was not able to find any process name that indicated that the backup was not complete.

Looking at the contents of /var/logs/system.log (or via the Console), I noticed that when the backup is finished, the last thing that is done is to eject the time machine disk image. Sure enough I checked that (this is OS X 10.6.8) during the backup there is a /Volumes/Time Machine Backups. Thus you can simply check if the directory exists. When it no longer does, Time Machine is finished.

I am not sure if this will work for everyone since I've followed the directions here to backup to a Windows machine on the network.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top