syncthing for windows, linux and bsd

Recently lost another hard disk on one of the Windows machines and decided it was time to implement a automated backup solution. What I settled on was a combination of Syncthing and AWS S3, brokered through the house FreeBSD file server.

Setting up Syncthing itself wasn’t too difficult. The executable as an archived binary for Windows and Linux. I used the port on FreeBSD. Configuration is through a web interface on first launch. Each platform had a slightly different automated start up solution. For FreeBSD a simple init script, for Linux a systemd script and on Windows a neat (but sparsely documented) little program called Syncthing Tray for Windows.

For the FreeBSD and Linux boxes I set up the web interfaces so they could be reached remotely, complete with a username and password. All that took was entering the system’s host name in place of the loopback adapter IP address. On all 3 platforms I also reset the web listening port to 8083 and turned on TLS encryption. I also unchecked the option to automatically launch the web browser on all of the Windows machines.

In order to sync folders from one machine to another the device (host) names were set to their DNS FDQN’s and each shared folder was given a unique ID that matched that of the corresponding folder on the other host. So, for example, “alpha-phil-documents” on the alpha.example.com Windows machine was mapped to “D:\Users\phil\Documents” on that machine, corresponding to /data/backup/alpha/phil/Documents on the FreeBSD file server. On each machine being backed up these folders were also marked as the “master” to prevent any changes on the backup host from corrupting the data. For hosts that were not on the file server’s subnet I made sure the server’s IP address was included in its device configuration there.

Syncthing Tray was configured to point to the Syncthing binary (in my case “D:\apps\syncthing\syncthing.exe”), and a symlink to it put in the user’s Startup folder.

Here are my init scripts for FreeBSD and Fedora 21 Linux.

First for FreeBSD (from the port):


#!/bin/sh

# $FreeBSD: head/net/syncthing/files/syncthing.in 373700 2014-12-01 03:42:08Z swills $
#
# PROVIDE: syncthing
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# syncthing_enable (bool):      Set to NO by default.
#                               Set it to YES to enable syncthing.
# syncthing_user (user):        Set user to run syncthing.
#                               Default is "syncthing".
# syncthing_group (group):      Set group to run syncthing.
#                               Default is "syncthing".
# syncthing_dir (dir):          Set dir to run syncthing in.
#                               Default is "/var/tmp/syncthing".

. /etc/rc.subr

name=syncthing
rcvar=syncthing_enable

load_rc_config $name

: ${syncthing_enable:="NO"}
# : ${syncthing_user:="syncthing"}
: ${syncthing_user:="root"}
# : ${syncthing_group:="syncthing"}
: ${syncthing_group:="wheel"}
: ${syncthing_dir:="/var/db/syncthing"}

pidfile=/var/run/syncthing.pid
procname="/usr/local/bin/syncthing"
command="/usr/sbin/daemon"
command_args="-f -p ${pidfile} ${procname} -home=${syncthing_dir} -no-browser ${syncthing_args}"

start_precmd=syncthing_startprecmd

syncthing_startprecmd()
{
        if [ ! -e ${pidfile} ]; then
                install -o ${syncthing_user} -g ${syncthing_group} /dev/null ${pidfile};
        fi

        if [ ! -d ${syncthing_dir} ]; then
                install -d -o ${syncthing_user} -g ${syncthing_group} ${syncthing_dir}
        fi

}

run_rc_command "$1"

In addition to this script the following line was added to /etc/rc.conf:

syncthing_enable="YES"

Next for Fedora 21:

[Unit]
Description=Syncthing for %i

[Service]
Type=simple
User=%i
ExecStart=/usr/local/bin/syncthing
WorkingDirectory=%h

[Install]
WantedBy=multi-user.target

Once the service is run for the first time using “systemctl start syncthing@phil” (giving the user who owns the folders to be backed up), a “systemctl enable syncthing@phil” will ensure it starts after reboot.

Once on the file server the synched files are mirrored to a secondary drive and then every couple of days sent over to AWS S3 using the s3cmd diff command.

This entry was posted in System Administration, Systems Analysis on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).