Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Mark Dec 19 '10 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 '10 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 –  Mark Dec 20 '10 at 10:37

7 Answers 7

up vote 4 down vote accepted

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.

share|improve this answer
1  
... pgrep backupd-helper –  Jason Salaz Dec 19 '10 at 21:44
1  
I found a more elegant way! TIME_MACHINE_PID=$! wait $TIME_MACHINE_PID –  whitneyz Dec 19 '10 at 22:36
    
@VxJasonxV nice. Thank you. –  Harv Dec 20 '10 at 0:26
1  
@whitneyz please could you elaborate on TIME_MACHINE_PID=$! wait $TIME_MACHINE_PID maybe in a separate answer? Thank you. –  Graham Perrin Aug 4 '11 at 11:41

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

tmutil status|grep -c "Running = 1"

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

share|improve this answer

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

share|improve this answer
    
Oh, wow. Thanks so much for mentioning this. –  ayaz Jun 3 '12 at 18:51
    
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 '12 at 19:13
    
Crazy that it's apparently undocumented! –  zigg Oct 16 '13 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.