How I Sync Application Preferences through Dropbox
I use Dropbox to sync and backup most of my stuff. Out of the box, Dropbox makes it easy to sync documents, photos, music etc.
However, I also wanted it to sync application preferences between my Macs. This can be done in a rather simple way, using a Launch Agent and a simple Ruby script.
This is a basic overview of how it works:
- The Launch Agent monitors your Preferences folder and a mirrored folder in your Dropbox
- Whenever a file in one of these folders is changed, the Ruby script will be run
- The script copies newly modified files from your Preferences folder to the Dropbox and vise-versa
Let’s take a look at how to implement this.
Folders
We need to setup a Dropbox folder for mirroring the preferences. I created a folder called Preferences in my dropbox. Also, I have a subfolder called Scripts for storing the Ruby script and a copy of my Launch Agent for installation on other computers.
~/Dropbox/Preferences~/Dropbox/Preferences/Scripts
If you want to use other folders for your setup, make sure to correct the paths in both the Ruby script and the Launch Agent.
The Ruby Script
This script performs the actual mirroring of modified files. Any preference files that should be synced must be included in the preference_files array. As you can see, this script is set up to sync preferences for the TextMate, Transmit and Terminal applications.
The script should be placed in the file ~/Dropbox/Preferences/Scripts/copy_plists.rb.
The Launch Agent
The Launch Agent is responsible for monitoring the preference folders. It consists of a plist file, represented as XML in the following code section.
Install the Launch Agent by copying the XML into the following file:
~/Library/LaunchAgents/com.dropbox.preferences.plist
Make sure to replace all [USERNAME] entries in the XML with your actual Mac OS X username. If you have Xcode Tools installed, you can also edit this file using the Property List Editor application.
That’s it. I have been using this setup for about a year, and it has been pretty robust this far. Got any ideas for improvement?
Note: This setup is only necessary when you need to sync individual plist files. If you want to sync entire folders (e.g. ~/Library/Application Support/Textmate), you can create a symbolic link to the folder inside your dropbox.
Update April 23
Tumblr’s questions feature doesn’t allow the author to reply. Answering some questions related to this post:
“Why not just move the Preferences folder to your Dropbox then make a symlink?”
This should work as well, however I found that I only wanted to synchronize settings for a few apps. Some apps have settings, which you’d like to have different between systems.
“What would be benefit of having it this over making the actual plist file to a symbolic link and copying it to the dropbox folder?”
This was also my initial approach. The problem is that a lot of applications recreates the
plistfile instead of writing into the existing file, thus removing the symbolic link. That’s basically why this setup was needed at all.“I don’t know anything about Ruby or Rails, but did you really need to put the ‘Copied to’ line in an if/else statement? And is ‘elsif’…?”
Sure, the ruby script could be refined and made more slick. I just needed a quick solution and it works, so that’s good enough for now :)
“Does it work to symlink the plist files into your Dropbox? If not, what about a hard link? Seems the script should definitely be unnecessary.”
Unfortunately, Dropbox will overwrite the symbolic link with a regular file upon update. I haven’t tested hard links yet, but please let me know if you do. For more information on symbolic- and hard linking files in your Dropbox, and the related issues, see: http://wiki.dropbox.com/TipsAndTricks/SyncOtherFolders#OSXandLinux