I spent about 4 hours last night finalizing my backup solution for my home network. That is, at least until I set up a raid 5 in the new year.

I already had my server backing up my music every night to a 40 gigger I had lying around, but I also wanted to backup my home folder on the Mac. I had been using a program called iBackup for the past few months to backup to an external drive, but I have a server, so I reasoned that my backups should all be stored there.

So, I wrote a script to back up my home folder. That was pretty simple using rsync and ssh. But, I wanted to be able to backup even when the mac was sleeping. So, I did some research and found a program for linux called wakeonlan. It’s a simple program that sends a ‘magic packet’ to a specified mac address. This worked perfectly. When I ran that program, the mac woke right up.

So, now I could schedule a cron job on the server to wake the mac up, and rsync my home directory via ssh. Great! Well, now I got to thinking that I’d like the Mac to go back to sleep after the backup. You know, save on energy, prolong the life of my Mac, etc.

Well, this proved to be more complicated than you’d think. Actually putting the mac to sleep was fairly easy. I just had to write a 3 line script, but then when I put the Mac to sleep, I lose the connection to my share on the server. I had to write an automator script that would connect to that share when it woke back up.

The problem with that automator script is that I had no way causing it to run when the Mac woke up. So, I dug around and found a program called Sleepwatcher that executes a script in my home directory just before the Mac goes to sleep, and one just after it wakes up.

Now I could run the automator script from the command line to automatically mount my network share when the Mac woke. I also wrote a script that would disconnect from that network share and close iTunes (iTunes hates not knowing where its music is) before the Mac went to sleep.

All of this was definitely worth it. Backups happen seamlessly, so I can put them out of my mind completely. If anybody wants the scripts I wrote to do this, or some help figuring it out, let me know.