Search

Debian - Setup your Server as a Permanent BTSync Peer

Contents[Hide]

dropcap-debian-btsync

BitTorrent Sync is a new p2p tool which opens some fantastic possibilities. It may revolutionize the way you'll handle your private file sharing as it allows you to share & synchronize your files across multiple devices (PC, tablet, smartphone, ...) on the net in almost real time.

Some examples of the possibilities opened by BTSync are described in the article BTSync - How peer-to-peer may Simplify your Life .

Like with any p2p network, BTSync needs to get some accessible peers to start synchronisation.

So, to allow synchronisation to be done at any time, you can setup a permanent peer on a Debian server by installing BTSync on it. As your server is always connected, it will handle all your devices connexions whenever they need it.

This article explains how to install a BTSync client on a Debian server to make it a permanent BTSync network peer. Any device (PC, smartphone, tablet, ...) will then be able to synchronize any time. You've then created your self hosted Dropbox or Google Drive replacement.

It has been tested on a Debian 7 Amd64 server.

1. Installation of BTSync binaries

First thing to do is to download and install latest binary from http://syncapp.bittorrent.com/1.4.111/.

You need to select the .tar.gz according to your architecture.

As it will be used as a service, it should be placed under /usr/local/sbin.

Terminal
# wget -O btsync_x64.tar.gz http://syncapp.bittorrent.com/1.4.111/btsync_x64-1.4.111.tar.gz
# tar -xf btsync_x64.tar.gz -C /usr/local/sbin btsync

2. Create Daemon User

To avoid any security issue, we'll make sure that BTSync daemon will run with a user with limited privileges. We will follow the principle of least privilege.

So we should create a btsync user and a btsync group that will be used to run the BTSync daemon :

Terminal
# adduser --quiet --system --group --disabled-password btsync

Home directory /home/btsync will be created for the new user btsync.

You should use this directory as the root of all synchronised directories.

3. Create Configuration File

Before starting the service, you must create a /etc/btsync.conf default configuration file that will be used at every startup.

/etc/btsync.conf
{
  "device_name" : "server",
  "listening_port" : 0,
  "storage_path" : "/home/btsync",
  "pid_file" : "/home/btsync/btsync.pid",
  "check_for_updates" : false,
  "use_upnp" : true,
  "download_limit" : 0,
  "upload_limit" : 0,
  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "admin",
    "password" : "yourpassword"
  }
}

You can directly download this default configuration file :

{displayconsole class=debian}# wget -O /etc/btsync.conf https://raw.githubusercontent.com/NicolasBernaerts/debian-scripts/master/btsync/btsync.conf{/jb_blackbox}

All BTSync data files and process ID will be stored under /home/btsync.

Web UI listening address is preconfigured on 0.0.0.0 to listen on all the network interfaces available. It means that you must change yourpassword with a proper one.

All the preferences defined in this file will be applied at every boot. So if you change them from the Web Interface, your default ones will be re-applied at next btsync server start. So, your server preferences should be setup from this btsync.conf file to survive reboot.

4. Adjust Listening Port for 3G mobile phones

By default, your BTSync instance listens on a random UDP port. If you are planning to synchronize some mobile clients thru a 3G network, it is highly advisable to setup a specific port number.

In fact, many mobile operators are not providing a neutral Internet access. Most of the time they are very restrictive, allowing only some very specific ports. All other ports are blocked. So, the listening port of your BTSync permanent peer will mostly be blocked by your mobile operator. As a result, you won't see the peer if you are connected thru your 3G access.

But, there may be a side path ...

You can setup your BTSync permanent peer to listen on the UDP port defined for an official internet protocol like DNS. As this port is used by a low level protocol, it should not be blocked by your mobile operator. You can get a list of official ports from Wikipedia.

So, by configuring UDP port 53, which is officially used for DNS services, it most probably won't be blocked ! You'll then be able to run your BTSync synchronization even while using a restrictive 3G data network. Using this port has allowed me to see my permanent peer on the french Bouygues Telecom 3G network.

This configuration can be done in /etc/btsync.conf.

"

/etc/btsync.conf
...
listening_port" : 53,
...

5. Declare Service Auto-start

Under Debian, every service should be declared thru a init.d script.

So we need to create a /etc/init.d/btsync script handling the daemon start, stop and restart.

/etc/init.d/btsync
#! /bin/sh
### BEGIN INIT INFO
# Provides: btsync daemon
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: BTSync server daemon
# Description: Daemon script to run a BTSync permanent peer
# Placed in /etc/init.d.
### END INIT INFO
# Author: Nicolas Bernaerts <This email address is being protected from spambots. You need JavaScript enabled to view it.>
# Version:
#  V1.0, 06/09/2013 - Creation
#  V1.1, 09/09/2013 - Use under-priviledged system user

# description variables
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="BTSync server"
NAME="btsync"
USER=$NAME
DAEMON=/usr/local/sbin/$NAME
ROOT=/home/$NAME
PIDFILE=$ROOT/$NAME.pid

# Exit if btsync program is not installed
if [ ! -x "$DAEMON" ] ; then
  echo "Binary $DAEMON does not exist. Aborting"
  exit 0
fi

# Exit if btsync user home directory doesn't exist
if [ ! -d "$ROOT" ]; then
  echo "User $USER does not exist. Aborting"
  exit 0
fi

# Function that starts the daemon/service
# 0 - daemon started
# 1 - daemon already running
# 2 - daemon could not be started
do_start()
{
  # If needed, start the daemon
  if [ -f "$PIDFILE" ]
  then
    echo "$NAME already running"
    RETVAL="1"
  else
    start-stop-daemon --start --quiet --chuid $USER --name $NAME --exec $DAEMON -- --config /etc/btsync.conf
    RETVAL="$?"
    [ "$RETVAL" = "0" ] && echo "$NAME started"
  fi

  return "$RETVAL"
}

# Function that stops the daemon/service
# 0 - daemon stopped
# 1 - daemon already stopped
# 2 - daemon could not be stopped
do_stop()
{
  # Stop the daemon
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  RETVAL="$?"
  [ "$RETVAL" = "0" ] && echo "$NAME stopped"
  [ "$RETVAL" = "1" ] && echo "$NAME was not running"

  # remove pid file
  rm -f $PIDFILE

  return "$RETVAL"
}

# deal with different parameters : start, stop & status
case "$1" in
  # start service
  start)
    do_start
    ;;
  # stop service
  stop)
    do_stop
    ;;
  # restart service
  restart)
    do_stop
    do_start
    ;;
  # unknown command, display help message
  *)
    echo "Usage : $SCRIPTNAME {start|stop|restart}" >&2
    exit 3
    ;;
esac

You can directly download & install this startup script :

Terminal
# wget -O /etc/init.d/btsync https://raw.githubusercontent.com/NicolasBernaerts/debian-scripts/master/btsync/btsync
# chmod +x /etc/init.d/btsync

Last step is to declare the service for automatic startup and shutdown. You also need to start it for the first time.

Terminal
# update-rc.d btsync defaults
# service btsync start

Your BTSync server is now up and running.

You can connect to http://your.server.ip.address:8888/ to start to configure its sharings.

6. Apache as BTSync GUI Reverse Proxy

If your Debian server is not directly accessible from internet and if you want to access it thru the standard http 80 port, you can configure your Apache 2 web server to act as a reverse proxy to forward the request to your BTSync Web GUI.

You just need to declare the following lines in your Apache 2 virtual host :

/etc/apache2/sites-available/yoursite
...
# BitTorrent Sync
ProxyPass /btsync http://127.0.0.1:8888
ProxyPassReverse /btsync http://127.0.0.1:8888
ProxyHTMLURLMap http://127.0.0.1:8888 /btsync
Redirect permanent /gui /btsync/gui
...

After you've restarted your Apache 2 service, you will be able to access your BTSync Web GUI from http://your.site.name/btsync/.

As your GUI will be accessible from the net, make sure you've setup a login / password in the /etc/btsync.conf configuration file.

7. Sync your files

Now that your permanent peer is operational, you can create different shared directories and peers.

Every shared directory should be accessible as read & write by btsync user.

So everytime you want to setup a synchronised directory, you should first create it on the server :

Terminal
# mkdir /home/btsync/myshareddirectory
# chown btsync:btsync /home/btsync/myshareddirectory

You can now connect to the BTSync GUI and declare the share.

As it is the first time you declare this synchronised share, you should generate the shared key.

This key will then be used by all the devices that need to synchronise with this directory.

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus