Thursday, February 26, 2015

How to install Syncthing on your Raspberry Pi

First of all, what is Syncthing? As the official website says:

Syncthing replaces proprietary sync and cloud services with something open, trustworthy and decentralized. Your data is your data alone and you deserve to choose where it is stored, if it is shared with some third party and how it's transmitted over the Internet.

In just a few words: it's an open source alternative to BTSync (formerly known as BittorrentSync)!
Since we like open source solutions, why don't we install it?

HOW TO INSTALL:

1) Let's check the latest release of syncthing on the official website:

https://github.com/syncthing/syncthing/releases


2) Now, connect via ssh to your Rapsberry Pi and download the latest armv5 version of syncthing:

For example:

wget https://github.com/syncthing/syncthing/releases/download/vx.yy.zz/syncthing-linux-armv5-vx.yy.zz.tar.gz

(IMPORTANT NOTE: x.yy.zz stands for the latest version number that you want to download, therefore if the latest version is, say, 0.10.23, the command will be:

wget https://github.com/syncthing/syncthing/releases/download/v0.10.23/syncthing-linux-armv5-v0.10.23.tar.gz


3) Untar it:

tar xvzf syncthing-linux-armv5-vx.yy.zz.tar.gz 

and rename the untarred folder:

mv syncthing-linux-armv5-vx.yy.zz syncthing


4) Now move to the syncthing directory:

cd syncthing

and start syncthing:

./syncthing

We are going to need to wait for the RSA key to be generated (have patience, it needs time!!):

pi@raspi ~/syncthing $ ./syncthing
13:07:22 INFO: Generating RSA certificate and key...
13:11:32 OK: Created RSA certificate file
13:11:32 OK: Created RSA key file
[FVCFD] 13:11:34 INFO: syncthing v0.10.22 (go1.2.2 linux-arm) jb@jborg-mbp 2015-02-26 15:48:25 UTC
[FVCFD] 13:11:34 INFO: My ID: HDUR8EHCIL3SJCHPA8UGMXIWKH8FHRTNCX6HBCOLOQ8FHSET7BV
[FVCFD] 13:11:34 INFO: No config file; starting with empty defaults
[FVCFD] 13:11:34 INFO: Edit /home/pi/.config/syncthing/config.xml to taste or use the GUI
[FVCFD] 13:11:34 INFO: Starting web GUI on http://127.0.0.1:8080/


5) Take note of the ID of your machine (in the example above it's the line that says: "[FVCFD] 13:11:34 INFO: My ID: HDUR8EHCIL3SJCHPA8UGMXIWKH8FHRTNCX6HBCOLOQ8FHSET7BV")


6) Since we are running a headless Raspberry Pi, we now need to enable the web GUI to be accessable from other machines:

nano /home/<user>/.config/syncthing/config.xml

To do this, just edit the line that says: <address>127.0.0.1:8080</address>
to: <address>0.0.0.0:8080</address>


7) Now we can connect to the Syncthing GUI from any computer in the network, just inserting the ip:port of the RasPi in any browser:

http://IP-of-RasPi:8080


8) Here we will be able to add other machines and the folders that need to be synced.

That's it!

Well, not entirely. If you want to make syncthing start at boot, just do as follows:

9) Let's create an init.d script for syncthing:

sudo nano /etc/init.d/syncthing

10) Copy the following lines and paste them inside it. Please don't forget to edit the two highlighted lines as per inline instructions)

===== BEGIN /etc/init.d/syncthing (do not copy this line) =====

#!/bin/sh
### BEGIN INIT INFO
# Provides: syncthing
# 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 syncthing.
# Description: Starts the syncthing daemon for all registered users.
### END INIT INFO

# Replace with users you want to run syncthing clients for
# syncthing_USERS="<your name here>"
syncthing_USERS="pi"  #Replace with your user
DAEMON=/home/user/syncthing/syncthing   #Replace with the path to syncthing

startd() {
  for stuser in $syncthing_USERS; do
    HOMEDIR=$(getent passwd $stuser | awk -F: '{print $6}')
    if [ -f $config ]; then
      echo "Starting syncthing for $stuser"
      start-stop-daemon -b -o -c $stuser -S -u $stuser -x $DAEMON
    else
      echo "Couldn't start syncthing for $stuser (no $config found)"
    fi
  done
}

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

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

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

exit 0

===== END /etc/init.d/syncthing (do not copy this line) =====

11) save and exit nano ('Ctrl-X' and then 'Y' to save)


12) Let's make the script executable:

sudo chmod +x /etc/init.d/syncthing


13) Let's update the init.d sequence:

sudo update-rc.d syncthing defaults


14) Let's start the daemon (just the first time, it will autostart from now on):

/etc/init.d/syncthing start

Now it's really done! :)



Credits:

3 comments: