Syncing with Rsync/Cron in Mac OS

16 02 2012

With that mess complete, I could now SSH into my Mac Mini from the office using a command like the one below (replacing the X’s with your Time Capsule IP address):

I could now complete a successful rsync sync for the first time as well (command is all one line):

rsync -avz --progress /Users/jv/Desktop/forotherbooks/ jvmini@XXX.XX.X.X:/Users/jmini/Desktop/fromotherbooks

Step 2: Public Key Authentication

Hooray! Since I knew I wanted to end up being able to do this automatically, entering the password each time wasn’t going to be an option. We use public key authentication for a few things in the office, so I had an idea of getting that set up, but if you are new to that game, check out this guide. Here’s the run-down of the commands used:

ssh-keygen -t dsa
scp ~/.ssh/ jvmini@XXX.XX.X.X:.ssh/authorized_keys
ssh jvmini

Step 3: Enter the Cron

Now I had sync set up and working, but the automated part was still missing. For that, I needed cron. Cron is a UNIX tool for scheduling jobs – pretty cool if you want to automate stuff and have it run on schedule. I didn’t have any experience with cron at all, so it took me a little while to get a handle on it. Basically, there are two parts: the script and the cron table.

The script I wrote wasn’t too complicated – essentially sync everything up, and the delete all the old files from the local directory.:

#!/bin/bash # This says which interpreter you want to run the script under (in this case bash)

RSYNC=/usr/bin/rsync # this tells the interpreter where to find rsync (run 'which rsync' to be sure)
SSH=ssh # find SSH in the same place as rsync
LPATH=/Users/jeff/Desktop/otherbooks/ # read: "where is the local computer path you wish to sync?"
RPATH=jeffmini:/Users/jeffmini/Desktop/fromotherbooks # read: "And what about the remote path?"

echo "Syncing and it feels so good" # I put this mostly for myself.

$RSYNC -avz --progress $LPATH $RPATH >> /tmp/output.txt # this is the rsync command, and then '>>' tells the interp to put the output into a text file (like a log)
cd $LPATH # CD into the local path
rm * # delete all the files that are left in there after the sync

I saved this under the title and saved it for now in my Home directory.

Next was setting up the cron table. Cron table is a config file where your list of jobs are for executing. This step was a bit tricky, since the normal command

crontab -e

Wasn’t working properly for whatever reason. I blame Apple but that’s ok. I found a workaround, which is to use the editor Nano, rather than VIM, to edit the crontab file. In retrospect, this makes sense, since Apple has been tried deprecating the use of cron in favor of something called launchd. Anyway, here’s the command for opening the crontable in Nano:

EDITOR=nano crontab -e

This opened up the cron table, so I could add a new line to it. Cron tables (or at least my limited understanding of them) have syntax like so:

* * * * * command to be executed

The asterisks in this case stand for [min (0-59), hour (0-23), day (1-31), month (1-12), day of week (0-7, 0 and 7 are Sunday). A great tutorial for Cron table syntax is over at Admins Choice.

Step 4: Testing

Since I was setting up the cron job to run once a week (2am Sundays), I didn’t want to wait that long to make sure it worked. A one-line addition to my cron table file ran the cron script immediately:

*/1 * * * * /Users/jv/

And then running a tail command in the terminal, to track updates to that “output.txt” file:

tail -f output.txt

And a minute later, voila! Syncing was complete! Now I can dump all the stuff I want to access on the Mac Mini (or archive) without clogging the network, or dumping it into the Time Machine abyss. Enjoy, and let me know if you have any questions!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: