Guide: Keeping Your Server’s Music in Sync with Your Main Mac/PC

Posted by Kevin Hanson | Posted in Apple, Linux, Technology | Posted on 21-12-2009-05-2008

1

If you have read some of my recent posts about setting up a Thin Client or Sheevaplug to act as home server, capable of distributing music across the house with Squeezebox Server, acting as a NAS, or running cool apps like Tonido (here are my first impressions!), then perhaps you have wondered about the best way to get your personal music collection on that server. Or once it is on the server, what happens if you want to add more? How do you keep music on the server in sync with your laptop? I hope this article will give you a great solution to that answer! I’m very happy with how it has been working for me. All you need is RSYNC, SSH, and CRON!The first thing we need is to prepare the machine that currently has all of your music on it. It needs SSH and RSYNC installed. If you have a linux PC, you probably already have that installed. If not, just do apt-get install sshd ssh rsync. If you have a Mac with OS X 10.6 Snow Leopard, the preferred platform :-) , then you already have these things installed, so worry not! And if you have a Windows PC, well it’s a bit more work, which I won’t be detailing in this post, but you need two pieces of free software: Deltacopy (a free windows implementation of rsync) and OpenSSH for Windows. We need RSYNC because it allows for the syncing of two folders, and we need SSH so we can get remote access into the host PC. To enable remote access on the Mac, you just check the box in System Preferences, under Sharing, below:

So if you have a Mac, you’re finished setting everything up on that machine. If you have a Windows or Linux computer, make sure to enable SSH and RSYNC. Then you’re good to go. There is one more thing you can do, and it’s optional. You can create a public/private key pair and put the public key on the Mac / PC, and the private key on the server. This will allow you to authenticate without a password, which means you don’t have to type your password in a script, which is nice, in case someone else looks at it or you want to show it to someone.

Moving on the server, now we need to set up a script that will sync all of the music over! Feel free to use mine below:

#!/bin/bash
#This script will sync iTunes music over SSH from your Mac / PC to your
#linux server!
EXCLUDES='*.m4v Mobile?Applications Cache Movies Ringtones Podcasts *.itlp *.ipa Previous?iTunes?Libraries Automatically?Add?to?iTunes'
USER=khanson
SERVER=10.0.1.100
ITUNES=/Users/username/Music/iTunes
DESTINATION=/media/music-space/
PARSEDEXCLUDES=`echo $EXCLUDES | awk '{n=split($0,ARRAY," ");for (i in ARRAY) printf "--exclude "ARRAY[i]" "}'`
rsync -atz $PARSEDEXCLUDES --delete -e ssh $USER@$SERVER:$ITUNES $DESTINATION
chmod 777 -R $DESTINATION

I’m not a shell scripting expert, but I managed to write this. Let’s walk through it and go over what each line does. The first line is a list of files to exclude when syncing over to the server. I use a question mark to represent the spaces, as sometimes rsync has issue with escaped spaces. Keep in mind that if you do the same, question marks are treated as wildcards. It really shouldn’t matter in this case, though. The list includes my podcasts folder, video files, iTunes LP files, etc. You can modify this for your own use by adding or removing more things to exclude. Then you need to give it the user, IP address of the server, path to your iTunes folder, and a destination to sync to. Then I have my line of code that parses through the string of excluded file types an puts them in the proper “–exclude PATTERN” format that rsync requires.  Then the next line is the rsync line that strings together all the information I set as a variable. You’ll notice the “–delete” parameter. This tells rsync to delete files that are no longer present on the macbook. Without this flag, if I were to delete songs in iTunes, they would still sit on the server, which is annoying. Note that I don’t specify a password in this line. This is due to the fact that I set up that public / private key authentication I mentioned above. If you wanted to use the password, you could just change it to $USER:$PASSWORD@$SERVER:$ITUNES . And the final line of the script changes the permissions of the music. The reason for this is actually due to an issue with Squeezebox Server right now, which is detailed / argued about at length here. Because the Squeezebox Server runs under a different user, it doesn’t, by default, have permission to view the folder we just copied over. So in order to fix that issue, the last line gives read / write priveleges for anyone to every file in my music folder. And voila! That’s it! Well, there is one more thing… This script will happily sync my music over, but how do I keep it in sync so I don’t need to manually run this script every so often?

That’s where we use cron! Cron is a task scheduler for linux, and it can execute any commands you give it any frequency you specify. It is most likely already installed on your server, but if it’s not, go ahead and install it with apt-get install cron. To edit the list of cron jobs scheduled, type crontab -e. An editor will open, and you’ll have a text file you can edit. You need to add a line to run the script we just wrote. Here’s an example of mine:

# m h  dom mon dow   command
0,15,30,45 * * * *   bash /etc/init.d/itunes-sync.sh

The first five parameters you specify on a line tell it the frequency (minute, hour, day of month, month, day of year) to execute and the last parameter is the command to execute. So in this case, I am telling it to run every fifteen minutes of every day. So that means that as long as my MacBook Pro is on the network, my music server will never be more than 15 minutes out of sync :-) . Oh and if I take my MacBook off the network, my rsync script just fails and life goes on. It’s a very elegant solution. Save your crontab file, and you’ll be good to go. At any time if you want a list of cronjobs that you have scheduled printed out, you can just type crontab -l.

And now you’re pretty much set! There is one final thing that I recommend setting, as a precaution. Because this script expects the MacBook to be at the same IP Address every time, you can set up something known as DHCP reservations through your router, in my case an Apple Time Capsule. You give it the MAC address of a device / computer, and it makes sure to reserve a specific IP Address for that computer. So in my case, my MacBook is always going to be at 10.0.1.100.

So that’s it! That’s the complete guide to syncing your music with rsync over SSH to your server. I hope you find it as useful as I do! Feel free to post comments if you have suggestions, and I’ll happily make some updates / changes. Thanks for visiting!

Related posts:

  1. Guide: Setting Up Your Sheevaplug to be the Ultimate Low Power All-Purpose Server
  2. Howto: Building a Squeezebox Server for under $100… Yes, it can be done…
  3. $100 Squeezebox Server – Can it be done?
  4. Tonido: Sheevaplug Installation and First Impressions
  5. Quick Guide: Installing VMware Tools with Fedora 12

Comments posted (1)

[...] it into a NAS. You can make it a Squeezebox Server for your music (article on this coming soon! UPDATE: here’s a guide I JUST wrote about keeping your music on your server in sync with your …). The possibilities really are endless. This is such an elegant, low powered device. I think the [...]

Write a comment