Installing BitTorrent Sync on a QNAP NAS

Recently I have been looking for a way to synchronise files between several machines, including my (OSX) laptop and a Linux-based network attached storage appliance. After looking at various alternatives, I settled on
BitTorrent Sync, the file synchronisation system based on the BitTorrent file transfer protocol. This essentially works much like Dropbox (in that it automatically synchronises the contents of one or more directories between computers), only without there being a centralised server accessing or storing the data; synchronisation is done directly between peers, and the files are encrypted end to end.

One drawback of Dropbox (notwithstanding the still open question of privacy; can one prove that they're not compelled by secret directive to funnel everyone's documents to the NSA as they're uploaded, just in case?) has been the paucity of clients for more unusual systems, such as, for example, the NAS I mentioned. While it's fine for synchronising PCs, Macs and x86-based Linux boxes (their Linux client, whilst preferring a GUI, can be coaxed into working from the command line), it does not cover the new generation of lightweight servers and storage devices, which are often based on ARM CPUs. As I have such a server (a QNAP TS-412), I have been looking for a way to keep some files synchronised between it and other machines, including a MacBook running OSX. BitTorrent Sync seems to work quite nicely, though setting it up on the NAS box can require some thought.

First, I installed BitTorrent Sync on my MacBook, which was a straightforward operation, much like any other application. Running the app prompted me to create a folder, with a random secret to be used for synchronising it with other machines. (BitTorrent Sync, as far as I understand it, uses a hash of the secret for finding shares in a central repository and the secret for accessing them from the serving machine itself.) Next, I downloaded the ARM Linux binary, which has been designed partly for installation on any ARM-based NAS one can get a shell on. (One could also run it on a device such as a Raspberry Pi or a CubieBoard, assuming that one runs a Linux distribution on that.)

Aside: concerning the QNAP TS-412

The TS-412 is a network attached storage appliance, or essentially a small ARM-based Linux box with four removable hard drive bays, which, out of the box, is configured to act as a storage appliance. However, unlike some other units (such as the Iomega StorCenter IX2 I used previously, which, whilst jailbreakable, was not designed with hackability in mind), there is an officially sanctioned means of installing user packages, enable a login shell and using the device as a general-purpose Linux box (albeit one of limited CPU power). QNAP have a packaging system named QPKG, which is mostly used for installing monolithic packaged applications (such as various kinds of servers); one QPKG package, however, is another packaging system named ipkg, which is more similar in philosophy to Debian's APT. Once you install ipkg, you can log into the NAS, run processes and install software (both from ipkg and elsewhere). The range of packages ipkg gives you is broad, including most of the things you'd find in a mainstream Linux distribution.

Installing btsync on the TS-412

BitTorrent conveniently provide an ARM Linux binary of btsync. Unusually for precompiled Linux syncing clients, it does not assume that the user is running a desktop Ubuntu machine and wants a nice graphical interface, which would have made it less than useful on NAS boxes. btsync thoughtfully eschews a traditional GUI altogether, but instead contains a web server and a web-based interface which runs on port 8888.

To play nice with the QNAP system, we make a virtual QPKG package for btsync and put its files there; from now on, all files related to btsync (though not, of course, the folders it synchronises) will live in a subdirectory of the QPKG working directory, which is at /share/MD0_DATA/.qpkg/:

Running btsync from here will start the daemon and create a .sync directory it uses to store its state. The daemon will be running as whichever user you start it as, and any files it creates will be owned by that user; you probably don't want this to be the case, so you will need to make it run as your personal user ID. You can achieve this by changing the executable's ownership and setting the setuid bit:

We can then start btsync and, with a web browser, go to port 8888 on the NAS device, and add shared folders through the web UI. Another option is to configure directories entirely through the configuration file. Whichever option we use, it is recommended to create a configuration file; without one, the web interface has no password, which could be a problem if anyone who isn't trusted might have access to the local network. So we create one, with:

Making btsync start automatically on boot

We want btsync to start automatically whenever the NAS reboots. On a normal UNIX system, we'd put a script in /etc/init.d, though this won't work on the TS-412. According to this QNAP wiki page, there is a recommended way of adding automatically starting applications, by creating a virtual QPKG package entry; we do this by adding something like the following lines to /etc/config/qpkg.conf:

This file will run a script named btsync.sh on startup; this script should behave like an init.d script, i.e., accepting one argument, which is start, stop or restart, as an operation to perform. Our script, therefore, will look like:

We make this executable (chmod +x /share/MD0_DATA/.qpkg/btsync/btsync.sh ), and from then on, can start the server using btsync.sh start. Finally, we reboot the NAS and log in to check that btsync has started: