Keeping two iTunes libraries in sync

From Mac Guides

Jump to: navigation, search
This article or section is based on a forum post written by odinsride.


This guide will show you how to have two exact replicas of your iTunes library on two different Mac. This is useful if, for example, you have one main machine (in my case a Mac Mini), and a portable (e.g. a MacBook Pro).This is going to be an intermediate Mac user guide, but don’t let that put any beginners off. Once the main Automator application is written, it’s fairly foolproof.

Before we start, it is important that you ensure you have recent backups of both machines, should anything go wrong. Next, you’ll want to consolidate all your various tunes onto one Mac. Now delete the empty iTunes folder from your other Mac, as we are going to recreate it using scripting. Before starting ensure that iTunes is not running on either Mac.

Contents

Getting Started

To make things simpler I’m going to use an iMac and MacBook as an example. The iMac has the main iTunes Library we wish to sync. Both Macs should have an account with the same shortname (this allows the iTunes Music Library.xml to work without having to be re-built). So to keep things simple in our example, the Macs’ username is ‘johndoe’.


Setting Up Networking

To start, network your two Macs together using a router or switch. I find it useful to set static IP addresses for both Macs. That way I know which number to use with which Mac. The iMac has an IP of 192.168.1.200, and the MacBook has an IP of 192.168.1.250.

Next go into System Preferences > Sharing and enable File Sharing. If you are using Leopard, you may now connect to your second Mac by looking in the Finder sidebar. Click All, and you should find your other Mac.

If using 10.4 or lower, choose Go > Connect to Server (command+k). Now enter your other Mac’s IP address and click Connect. Log in using your account details and choose the home folder for your account.


Creating the Sync Script

Now comes the fun step - copying your iTunes library from one Mac to another. To do this we are going to use a command called rsync. Start by loading up Terminal (Applications > Utilities > Terminal). If you’ve never used Terminal it may look quite daunting, but it isn’t really.

To use the rsync function, you need to use the following syntax:

rsync [options] [source] [destination]

So that means that we’ll want to use:

rsync -av --force --delete --size-only ~/Music/iTunes /Volumes/johndoe/Music

We use ‘~’ to tell Terminal that we are looking at the current users Home folder. Here’s a list of what the options do:

  • -av: -a basically says to sync everything recursively and preserve things like date modified, -v is for verbose mode (so you get more information in your log file)
  • --force: prevents you from having to confirming overwriting files
  • --delete: if something has been removed from the source, it will delete it on the destination
  • --size-only: skip files that match in size

For a full list of all the commands, you can type ‘man rsync’ in terminal and that’ll show you the rsync manual.

First Sync

You did backup both Macs didn’t you? Good. The first time you run this command, it’ll copy over all your music to your second Mac. Depending on the size of your library and the speed of your network, this could take a long time. I recommend you connect your Macs to your router using Ethernet cables, rather than Airport when you first sync.

As we are using the -v option, you should see all of your files being copied over one at a time. Once this process starts, try not to switch either machine off. Probably best to do this first sync overnight if you have a large library. My library is about 25 GB and it took about 45-60 minutes to copy everything over 100Mbps Ethernet.

If you have to switch either Mac off, ensure you open Terminal and press ctrl+c to stop the current sync. Then you can shut the Mac down safely. Because rsync only updates new files, it should pick up right where it left off.

Automating The Process

So now you have copied over your library, any further syncs should be much quicker, as only modified or new files will be copied. However it would be a bit of a pain to type that command in each time you want to sync!

As long as you have OS X 10.4 or later, you can use Automator to speed up the process. The instructions below are written for 10.5, but they should be similar for 10.4.

Start by opening a new workflow and add in Get Specified Servers. Click on Add and enter the IP address of your other Mac, followed by the shortname.

Choose URL

Now add a Connect to Servers action, and finally a Run Shell Script action. Copy and paste your rsync script into the Run Shell Script action, and thats the main scripting done. As we are not running the script from a Terminal window, it may be useful to include logging so we can see if any errors have occurred.

To do this we need to add a bit more to our shell script. Add the following to the end of your shell script:

> ~/Documents/AutomatorLogs/iTunesMSync.log

Now go to your Documents folder and create a new folder called AutomatorLogs. When the script is run, it should now output a log file stored in your Documents/AutomatorLogs folder.

The final step is to add an Eject Disk action to unmount the previously mounted share point. Your Automator workflow should now look like this:

Completed Workflow


Now save your workflow as an Application in your Applications folder as ‘iTunes Library Sync’, or something equally as descriptive. Any time you want perform a sync, simply run the Application you previously created. A notification will appear in your menu bar to keep you updated with what’s currently happening. If the sync is taking a long time, open up the iTunesMSync.log file.


Limitations

The one main limitation to rsync is that it’s one way only. So in our example above, you can only sync from the iMac to the MacBook. But there is a quick and easy work around. Simply have two different versions of your Automator application. The first will have the script as below:

rsync -av --force --delete --size-only ~/Music/iTunes /Volumes/johndoe/Music > ~/Documents/AutomatorLogs/iTunesMSync.log

And the second will have the source and destination swapped:

rsync -av --force --delete --size-only /Volumes/johndoe/Music/iTunes ~/Music > ~/Documents/AutomatorLogs/iTunesMSync.log

The other option is to split to the two applications, so that one resides on your main Mac, and the other resides on your laptop. That way you’ll know that the application syncs your local library to the other Mac. Just be sure to edit the Get Specified Servers action at the start of your 2nd Automator script.

Syncing With Windows

It is possible to sync between OS X and Windows, however it requires more advanced knowledge. You also need to slightly edit your code to not sync over the iTunes Music Library.xml file, or the Vista/XP thumbnail cache.

Firstly, you need to enable Windows file sharing, and share your iTunes folder. If you don’t know how to do this read the Networking Windows with Mac OS X article first.

Next you need to edit your shell script as below.

 rsync -av --force --delete --stats --size-only -e ssh --exclude="ethumbs_vista.db" --exclude="Thumbs.db" --exclude=".DS_Store" --exclude="iTunes\ Library.xml" /Volumes/iTunes/* ~/Music/iTunes

As you can see the script now excludes the Vista/XP cache files, and also the Mac ‘.DS_Store’ files. We are also excluding the iTunes Library.xml file as the file path is different. Don’t worry about this - iTunes will automatically recreate the xml file when you first launch iTunes.

This code will copy the contents of your PC’s iTunes folder on to your Mac. If you want to reverse the sync, swap over the source and destination file paths.