Skip to content

Instantly share code, notes, and snippets.

@coreygo
Last active July 2, 2021 05:03
Show Gist options
  • Save coreygo/305dad8739d302f6be03 to your computer and use it in GitHub Desktop.
Save coreygo/305dad8739d302f6be03 to your computer and use it in GitHub Desktop.
Brief guide to running iTunes with a NAS iTunes Media folder.

2021-07-01: Deprecated. This scenario and the hacks involved likely still work, but I'm no longer using this in lieu of simply organizing and tagging with MusicBrainz Picard and using Plex Media Server instead of iTunes.

Keeping iTunes in sync.

Unfortunately storing a TB+ of music is a real challenge on a laptop with only a 256GB SSD. Luckily with lunchy (a launchd wrapper), rsync, automator and a little magic the task is made simpler.

One might want to use this with a Gbps fiber connection and VPN. If you're using a Synology NAS it's entirely possible to configure the VPN, DNS, DHCP, etc directly. Some may prefer however to set up a VPN on the router for better isolation. The latter is my preferred method.

Polishing your gems

OS X Mavericks comes with Ruby 2.0 but you shouldn't touch it, instead you should first install RVM -- a simple \curl -sSL https://get.rvm.io | bash -s stable --autolibs=homebrew --rails will do.

Once installed pop open Terminal or iTerm

$ which ruby
/Users/corey/.rvm/rubies/ruby-2.1.0/bin/ruby

$ which lunchy
/Users/corey/.rvm/gems/ruby-2.1.0/bin/lunchy

Have your lunchy and eat it too

You can then gem install lunchy. With Ruby, RVM, and lunchy working touch ~/Library/LaunchAgents/SynciTunestoNAS.plist. Open the .plist you just created and save with the following paste

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>SynciTunestoNAS</string>
    <key>ServiceDescription</key>
    <string>Sync the iTunes Library to a NAS share as soon as iTunes changes locally.</string>
    <key>LowPriorityIO</key>
      <true/>
    <key>EnableGlobbing</key>
      <true/>
    <key>ProgramArguments</key>
    <array>
      <string>automator</string>
      <string>~/Library/Mobile\ Documents/com~apple~Automator/Documents/Sync\ iTunes\ to\ NAS.workflow/</string>
    </array>
    <key>WatchPaths</key>
    <array>
      <string>~/Music/iTunes/</string>
    </array>
  </dict>
</plist>

Though it appears at the end, the key WatchPaths provided by launchd causes the automator workflow to be started if any modifications are detected to the current user's default iTunes directory. You may also want to review this property list generator for launchd

Automate everything

I store my automator workflows on iCloud for convenience; the document store maps locally to ~/Library/Mobile\ Documents/com~apple~Automator/Documents/. One might choose to use .dotfiles and symlink everything but that's outside the scope of this guide and something I've yet to do.

To create the workflow ⌘ + ␣ and type Automator to run the app from Spotlight. Modify and save your new workflow Sync iTunes to NAS so that it matches this screenshot Sync iTunes to NAS

Here is the rsync command I use

rsync -varz --force --delete --size-only --ignore-existing \
--exclude "iTunes Media" \
--exclude "Mobile Applications" \
--exclude "Previous iTunes Libraries" \
--exclude "Audiobooks" \
--exclude ".DS_Store" \
~/Music/iTunes /Volumes/music/ > ~/Library/Logs/SynciTunestoNAS.log

The initial options are equal to --verbose --archive --recursive --compress; rsync will also --force --delete data on the receiving side, in this case /Volumes/music. The verbose output log is stored in ~/Library/Logs/ for later review if needed.

If you've installed another shell such as zsh or fish be sure to change your shell back to /bin/bash to bypass possible issues.

At this point we've created a launchd .plist for lunchy and an automator workflow that will be launched automagically.

Note that we --exclude "iTunes Media" \ so that rsync doesn't touch any of the actual media files.

Magically link your local system to your NAS

Note I've already set up the NFS server on the Synology NAS. The NFS client in OSX also needs configuring using Autofs in OS 10.9.2. I found this helpful in setting up auto-mount.

First and foremost, add the NAS IP address to the end of /etc/hosts, in my case 192.168.1.111 magnetic.coreygo.com. Modify /etc/auto_master appending /- auto_nfs -nobrowse,nosuid to the end then save (make sure there's one blank line at the very end). Create a local mount point in OSX (normally /Volumes is used but doing so causes conflicts with AFP/SMB) sudo mkdir -p /mnt. If you're only setting up the NFS share for music sudo sh -c 'echo "/../mnt/music -fstype=nfs,nolocks,locallocks,resvport,hard,bg,intr,rw,tcp,nfc nfs://magnetic.coreygo.com:/volume1/music" > /etc/auto_nfs' if however you have multiple NFS shares, create /etc/auto_nfs then add each NFS share, one per line. /../mnt/music -fstype=nfs,nolocks,locallocks,resvport,hard,bg,intr,rw,tcp,nfc nfs://magnetic.coreygo.com:/volume1/music

The extra /.. is a hack to bypass possible issues mounting under /mnt/, I create the shares here because OSX will automagically handle remounting in the background. With /etc/auto_nfs saved change the permissions so autofs won't break by doing sudo chmod 644 /etc/auto_nfs. Next sudo automount -vc to updated the mounts.

With NFS configured, we'll symbolically link the local "iTunes Media" folder to the one on the NAS music share by doing ln -sfn /mnt/music/iTunes\ Media ~/Music/iTunes/.

Now if you run ls -li ~/Music/iTunes/ you should see iTunes Media -> /Volumes/music/iTunes Media which confirms the symlink.

I keep my iTunes Media folder and actual iTunes (library data) separate to maintain sanity and prevent anything scary happening to the media itself.

Conclusion

Woop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment