Keeping iTunes in Sync

Notification Image I just bought my second Apple computer ever, a brand new 13″ Macbook Air maxed out to the MAX! For the first time I am having to look at my setup in a new light. I am so use to having all my information on one computer, just doing backups and using Dropbox as my personal versions system. Now I have to consider what information I want on each computer and what information I want to sync across both. The Macbook Air will become my primary computer but I want my iTunes library on both because the secondary computer will remain at home, always on, and accessible by the AppleTV. For most situations, my free Dropbox account has been more than enough for my syncing needs. I am starting to question that now since I definitely don’t have enough space for syncing my iTunes library. My first thought was to pay the $10 a month for premium Dropbox account but I am cheap and I wanted to at least explore cheaper options. My second thought was to automate this job with a shell script.

I started by considering the conditions I would want this sync to run so it wasn’t using unnecessary resources.

  • Only run the job at home
  • Only run if the secondary computer was running
  • Exclude the Movies directory
  • Must be two-way sync
  • Log output and errors to a log file

I used the command line airport utility to check the currently connected SSID, if it matched my home network then it knew I was at home. I then pinged my secondary computer, if it responded, the script continued, if it didn’t respond the script simply closed. I then threw in some Growl notifications to indicate when a sync had been started and when it had completed so I didn’t accidentally turn off my computer during the process. Finally I ran rsync to compare and synchronize each directory, emulating two-way sync to ensure that both computers had the same data. I also skipped the Movies directory because I do not want all of my movies on my Macbook Air’s SSD. I save those for the home computer that will serve content up to the AppleTV.

DISCLAIMER

Before we get to the code, don’t blame me if you fuck up and delete your iTunes library. I started with my library on BOTH computers. I used rsync to move the library to my new computer since rsync will copy the files initially anyway but you can use whatever you want for the initial move. Even though I just wrote/used this script and it worked like a champ, I have to say I know what I am doing, if you are unsure about what you are doing, then for fuck sake back up your library or use rsync’s –dry-run option FIRST and read the log to see what changes would have been made.

OK. So let’s make it all work.

First you will have to have SSH enabled on the secondary Mac and you will need an RSA key on the server so you can log in without typing in a password each time. Instead of explaining how to do this, I will provide you with this link to an amazing website that will walk you through the whole process. I promise it only takes a minute, its really easy. Here is the link: SSH Keygen. It doesn’t show you how to enable SSH on your Mac, but if you can’t handle that then close this window and do not continue.

The Code

Below you will find a link to download the script. Be sure to utilize the download link and not just copy the embedded code, that is just for illustration of this article.

Take a look at the code, download it, and continue reading.

Once the script is downloaded, open it up and customize the variables

  • REMOTE_USER – The user account on the second computer
  • HOME_SSID – The name of your wireless network
  • DIR – The full file path of your local iTunes directory
  • SECONDARY – The computer name of your secondary computer

Save and close the script.

Make it executable by running this from the terminal

chmod +x sync.sh

Get it to Run Automatically

I wanted to run the script hourly but you can run it whenever you want. I thought about using cron but decided on launchd since it is the more powerful younger sibling to cron.

I saved the sync.sh script in to the directory: ~/Library/LaunchScripts/ (make this folder if not there)

I created a new launched plist file in ~/Library/LaunchAgents/ called com.binaryghost.SynciTunes.plist you can of course, name this whatever you want.

Here is the code inside the plist file, be sure to change the path to your script:

Once you have the script and the plist file in the correct place, just reboot the computer to let launchd load your new plist config and away it goes :)

I also created an Alfred command so I could quickly launch it manually if need be.

alfred

Enjoy!