18

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?

3
  • I would ask why not just rsync the original - I ask this as far as I understand rsync does not understand the hard link directories so that you will end up taking multiple copies of every file.
    – mmmmmm
    Dec 19, 2010 at 19:59
  • Do you mean with each successive backup that is performed, Time Machine will generate a backup with hard links to the original files that have been backed up already? I ask this because I'm not that familiar with Time Machine. If that is the case, then yes I'd copy the original. But I am pretty sure that rsync has an option that will cause it to ignore recreating hard links.
    – whitneyz
    Dec 19, 2010 at 22:35
  • 1
    Yes that is how TM works - rsync can understand hard file links but I don't think t understands hard directory links which are unique to OSX since 10,5
    – mmmmmm
    Dec 20, 2010 at 10:37

7 Answers 7

4

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.

3
  • 1
    ... pgrep backupd-helper Dec 19, 2010 at 21:44
  • 1
    I found a more elegant way! TIME_MACHINE_PID=$! wait $TIME_MACHINE_PID
    – whitneyz
    Dec 19, 2010 at 22:36
  • 1
    @whitneyz please could you elaborate on TIME_MACHINE_PID=$! wait $TIME_MACHINE_PID maybe in a separate answer? Thank you. Aug 4, 2011 at 11:41
22

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

On newer OS, there is an undocumented command tmutil currentphase that shows the name of the current phase.

2
  • Yes - tmutuil is so full of win, it's hard to express how nice it is to peek and poke at the backup status, stats, and drive things programatically.
    – bmike
    Jun 3, 2012 at 19:13
  • Crazy that it's apparently undocumented!
    – Mattie
    Oct 16, 2013 at 19:26
5

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

4

tmutil status|grep -c "Running = 1"

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

3

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.

1
  • Note that even if there's no .inProgress directory, Time Machine may still be busy preparing for or tidying up after a backup (e.g. removing old backups).
    – gidds
    Nov 2, 2019 at 14:53
1

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

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .