Syncing a media PC with simple tools
This is a guide to creating a setup which keeps a master copy of media stored on your laptop in sync with a media computer using NAS-based storage. This guide deals with Macs, but I expect it would be easy to adapt. Indeed, I post this more for inspiration rather than concrete instructions.
I have a Media mac attached to my TV to play music, movies and iPlayer. It runs Plex, which allows you to scan a folder into it’s music and movie libraries. The catch: Plex needs to be restarted to pick up new files and add them into its library. The Media mac always runs as the “mediacentre” user.
The Media mac is attached to a Drobo disk. This disk is where the media it displays are stored. The Drobo is network attached, so it’s accessible from other computers in the house.
This is where the Master mac comes in. It’s where I rip and buy music and films. It’s Master because it holds the master copy of my media. The master copy is stored on the Master’s local disk, but the Master does mount the drobo disk at /Volumes/Drobo
.
So what I want is for the Media computer to automatically pick up the changes to the Master’s media folder, so I can watch and listen on the TV. With all the moving parts, this isn’t suited to an off-the-shelf solution. Here’s a diagram of the links in the system.
My solution lies in combining small tools together; the Unix Way. We want to be able to execute the following workflow:
- Use Wake on Lan to make sure the Media computer is awake.
- Use rsync to mirror the files from the Master to the Drobo.
- Execute a remote AppleScript app to restart Plex on the Media computer. This causes Plex to re-scan its media library from the updated Drobo.
Conceptually pretty simple, but there are several settings changes we need to make first. The workflow can be done with a simple shell script and AppleScript combo. We’ll see what the script ends up looking like later, but first we need to set up the computers involved.
Enable Wake on LAN
This step is simple. First download wol.py
from here to the Master. I put it at ~/bin/wol.py
. This script implements Wake on LAN in python.
Next, you need to find out the Media computer’s MAC address to plug into wol.py
, so it wakes up the correct computer:
- Open System Preferences on the Media computer.
- Click Network in the third row (on Snow Leopard, at least).
- Click Ethernet on the left.
- Click the Advanced button.
- Click the Ethernet tab. The MAC address is labeled Ethernet ID.
Open wol.py
on the Master and remove the current wake_on_lan
calls and add one with the Media computer’s MAC address. Double check you’ve used the Media computer’s MAC address!
Finally for this stage, you need to make sure Wake on LAN is enabled on the Media computer. Open System Preferences and go to Energy Saver on the Media computer. Then either:
- Snow Leopard: Click Power Adaptor -> Check “Wake for network access”.
- Leopard: Click Advanced -> Check “Wake for network administrator access”.
Activate SSH server on Media and configure password-less connections
To restart Plex, we need to be able to SSH into the Media computer. Turn on the SSH server by:
- Open System Preferences on the Media computer.
- Open Sharing.
- Check Remote Login.
Take a note of the computer name at the top of the Sharing preference pane. Then try to connect in the terminal:
You should be prompted for your password, then see the home directory of the Media computer. Type exit
and hit return to close the connection.
Once you’re this far, it’s quite easy to set up password-less SSH using key-pairs. Try the following tutorial here.
Create an AppleScript to restart Plex.
The next step is to create an AppleScript to restart Plex on the Media computer. This will be executed from the Master over SSH, using the connection we just set up.
The AppleScript to restart Plex is quite simple:
Enter this into the AppleScript Editor (find it via Spotlight) and save it as an application on your Media computer. Make a note of where you save the AppleScript, you’ll need it later. I put it in /Users/mediacentre/bin/restart-plex.app
.
Plex seems to throw an error when it’s opened from AppleScript, so we ignore its responses.
Plex Setup
You need to set up Plex with a media library from the folder on your shared storage, and use Preferences -> Music -> Music Library -> Rescan on startup. This makes sure Plex will update the library when restarted using our AppleScript.
Unfortunately I can’t dig out a tutorial on this for now, so you’re on your own for this step.
One shell script to bind them
Now everything is set up, the shell script to bring it together is quite simple. This goes on the Master computer:
Correct the various paths, usernames and hostnames in the script.
You can see the steps of the workflow about, one command per step. First, we wake up the Media computer, then rsync the media and finally restart Plex.
And finally, Hazel
I didn’t mention this to start with as Hazel isn’t a free tool. It is, however, one of the easier ways to automatically run the sync script when media is added to the Master computer. You can set up a simple rule for the media folder:
That is, when there’s a new folder, which is over ten minutes old to give the album time to rip/download, call the media2drobo script and pop up a notification in Growl to tell me what’s happened.
All quite neat overall. It seems stable in testing, but only a few months of real world use will prove it’s robustness.