Running rsync on network connect

2

1

I have one Mac which is always on and is my main computer. I also have a MacBook and I'm trying to sync my iPhoto library. So I can successfully use rsync to sync files. I'm using a cron to have it run once a day.

In reality the MacBook isn't always on, so I'm looking for a way to run rsync when ever the two computers are connected on the same wifi network. So I'm guessing the best place is to somehow run rsync when the airport is connected. Whats the best way?

Alex

Posted 2010-04-13T19:25:35.537

Reputation: 119

Answers

2

Since rsync only changes what needs changing, it wouldn't hurt to simply have your cron job run once per hour. If there is little or nothing to copy over, it will take very little time. But you wouldn't want the job to hang trying, so you could create a small shell script to check if your MacBook is up and, if it is, then have it run the rsync command. Just in case there is a problem and the rsync job hangs or takes longer than an hour, you want to make sure you don't start a new instance when the previous one is still running. Here's a sample script (which I'll call MyRsyncJob and which you'll have to revise):

[ -s /var/run/MyRsyncJob ] && {
  PID=$(cat /var/run/MyRsyncJob)
  echo "MyRsyncJob is still running (PID $PID)" >&2
  exit 1
}
ping  -c 1 -o MacBook >&- || exit 1 # Exit if MacBook isn't reachable
trap 'rm /var/run/MyRsyncJob' 0 15 # Remove run file upon exit or TERM signal
echo $$ >> /var/run/MyRsyncJob     # Put PID into run file
rsync -zave ssh /path/to/my/iphoto/library/ MacBook:/path/to/destination/directory

I hope that helps.

Marnix A. van Ammers

Posted 2010-04-13T19:25:35.537

Reputation: 1 978