Install and configure btsync in Ubuntu 12.04

2013-08-26
#ubuntu #btsync #howto

This note describes the installation of Bittorrent Sync on Ubuntu. The tutorial was tested on Ubuntu 12.04, but it must work for all Debian-based systems. Read the full btsync manual for advanced information.

§ Download and install btsync

Btsync is provided as a single binary file. We will place it in the /usr/bin directory. So, download the archive with the binary file, extract it, remove the archive, and move the binary:

1wget http://btsync.s3-website-us-east-1.amazonaws.com/btsync_x64.tar.gz
2tar xf btsync_x64.tar.gz
3rm btsync_x64.tar.gz
4sudo mv btsync /usr/bin/btsync

§ Configure shared folder

We will use a configuration file instead of the web interface. Create ~/.sync/config.json (for user username, the <home folder> is /home/username) with the content provided below. If you have several users who want to use btsync, create the config in their home folders.

 1{
 2  "device_name": "<computer name>",
 3  "pid_file" : "<home folder>/.sync/btsync.pid",
 4  "storage_path": "<home folder>/.sync",
 5
 6  "listening_port" : 0,
 7  "check_for_updates" : true,
 8  "use_upnp" : true,
 9  "download_limit" : 0,
10  "upload_limit" : 0,
11  "lan_encrypt_data": true,
12
13  "shared_folders" :
14  [
15    {
16      "secret" : "<secret key>",
17      "dir" : "<dir path>",
18
19      "use_relay_server" : true,
20      "use_tracker" : true,
21      "use_dht" : false,
22      "search_lan" : true,
23      "use_sync_trash" : true
24    }
25  ]
26}

§ Init script

Create the file /etc/init.d/btsync with the content (thanks to Mendel): you need to list the users who will use btsync in the BTSYNC_USERS variable; every user must have the config file from the previous section in their home folder.

 1#!/bin/sh
 2### BEGIN INIT INFO
 3# Provides: btsync
 4# Required-Start: $local_fs $remote_fs
 5# Required-Stop: $local_fs $remote_fs
 6# Should-Start: $network
 7# Should-Stop: $network
 8# Default-Start: 2 3 4 5
 9# Default-Stop: 0 1 6
10# Short-Description: Multi-user daemonized version of btsync.
11# Description: Starts the btsync daemon for all registered users.
12### END INIT INFO
13
14# Source: https://gist.github.com/MendelGusmao/5398362
15
16# Replace with linux users you want to run BTSync clients for
17BTSYNC_USERS="kenarius"
18DAEMON=/usr/bin/btsync
19
20start() {
21  for btsuser in $BTSYNC_USERS; do
22    HOMEDIR=`getent passwd $btsuser | cut -d: -f6`
23    config=$HOMEDIR/.sync/config.json
24    if [ -f $config ]; then
25      echo "Starting BTSync for $btsuser"
26      start-stop-daemon -b -o -c $btsuser -S -u $btsuser -x $DAEMON -- --config $config
27    else
28      echo "Couldn't start BTSync for $btsuser (no $config found)"
29    fi
30  done
31}
32
33stop() {
34  for btsuser in $BTSYNC_USERS; do
35    dbpid=`pgrep -fu $btsuser $DAEMON`
36    if [ ! -z "$dbpid" ]; then
37      echo "Stopping btsync for $btsuser"
38      start-stop-daemon -o -c $btsuser -K -u $btsuser -x $DAEMON
39    fi
40  done
41}
42
43status() {
44  for btsuser in $BTSYNC_USERS; do
45    dbpid=`pgrep -fu $btsuser $DAEMON`
46    if [ -z "$dbpid" ]; then
47      echo "btsync for USER $btsuser: not running."
48    else
49      echo "btsync for USER $btsuser: running (pid $dbpid)"
50    fi
51  done
52}
53
54case "$1" in
55 start)
56start
57;;
58stop)
59stop
60;;
61restart|reload|force-reload)
62stop
63start
64;;
65status)
66status
67;;
68*)
69echo "Usage: /etc/init.d/btsync {start|stop|reload|force-reload|restart|status}"
70exit 1
71esac
72
73exit 0

That’s all. Now make btsync executable, register the init script with update-rc.d, and start the service. Issue as root:

1cd /etc/init.d/
2chmod +x btsync
3update-rc.d btsync defaults
4service btsync start