Install and configure btsync in Ubuntu 12.04

2013-08-26
#ubuntu #btsync #howto

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

§ Download and install btsync

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

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

§ Configure shared folder

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

{
  "device_name": "<computer name>",
  "pid_file" : "<home folder>/.sync/btsync.pid",
  "storage_path": "<home folder>/.sync",

  "listening_port" : 0,
  "check_for_updates" : true,
  "use_upnp" : true,
  "download_limit" : 0,
  "upload_limit" : 0,
  "lan_encrypt_data": true,

  "shared_folders" :
  [
    {
      "secret" : "<secret key>",
      "dir" : "<dir path>",

      "use_relay_server" : true,
      "use_tracker" : true,
      "use_dht" : false,
      "search_lan" : true,
      "use_sync_trash" : true
    }
  ]
}

§ Init script

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

#!/bin/sh
### BEGIN INIT INFO
# Provides: btsync
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Multi-user daemonized version of btsync.
# Description: Starts the btsync daemon for all registered users.
### END INIT INFO

# Source: https://gist.github.com/MendelGusmao/5398362

# Replace with linux users you want to run BTSync clients for
BTSYNC_USERS="kenarius"
DAEMON=/usr/bin/btsync

start() {
  for btsuser in $BTSYNC_USERS; do
    HOMEDIR=`getent passwd $btsuser | cut -d: -f6`
    config=$HOMEDIR/.sync/config.json
    if [ -f $config ]; then
      echo "Starting BTSync for $btsuser"
      start-stop-daemon -b -o -c $btsuser -S -u $btsuser -x $DAEMON -- --config $config
    else
      echo "Couldn't start BTSync for $btsuser (no $config found)"
    fi
  done
}

stop() {
  for btsuser in $BTSYNC_USERS; do
    dbpid=`pgrep -fu $btsuser $DAEMON`
    if [ ! -z "$dbpid" ]; then
      echo "Stopping btsync for $btsuser"
      start-stop-daemon -o -c $btsuser -K -u $btsuser -x $DAEMON
    fi
  done
}

status() {
  for btsuser in $BTSYNC_USERS; do
    dbpid=`pgrep -fu $btsuser $DAEMON`
    if [ -z "$dbpid" ]; then
      echo "btsync for USER $btsuser: not running."
    else
      echo "btsync for USER $btsuser: running (pid $dbpid)"
    fi
  done
}

case "$1" in
 start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: /etc/init.d/btsync {start|stop|reload|force-reload|restart|status}"
exit 1
esac

exit 0

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

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