How to Use BitTorrent Sync to Backup Linux Server

This tutorial explains how to install and use BitTorrent Sync to automatically backup Linux server. BitTorrent Sync allows you to sync folders between machines using BitTorrent protocol. You can sync folders between your computer and smartphone directly. There’s no need to upload content to third-party servers. Contents transmitted by BitTorrent Sync are protected with 256bit AES Encryption.

Because it uses BitTorrent protocol, there must be at least two machines online in order to sync folders. You probably won’t keep your personal computer online all the time. But server is another story. Servers are running almost all the time and connected to the Internet. So using BitTorrent Sync to backup your Linux server is a very good idea.

Sync Scenarios

  1. You have two Linux servers that are running and connected to the Internet 24 x 7,  then you can use BitTorrent Sync to automatically sync folders between them.
  2. You have just one Linux server. You can sync folders between your server and your home computer. When your home computer starts, it will automatically fetch new contents from your Linux server.
  3. You have a Raspberry Pi. You can keep your RPi up and running 24 hours a day and sync folders between your Linux server and RPi. After all, a Raspberry Pi consumes very little power and it does not cost you too much money.

So let’s get started.

Backup Linux Server with A Master Server and a Slave server Setup

If you have two servers then you can configure one as master and another as slave. That means, you update your website content on master server and the slave server can only read the syncing folder on master server but it can not add new content to the syncing folder. The slave server is used only for backup purpose.

Install BitTorrent Sync on Master Server

You can use the following method to install BitTorrent on any Linux distribution.

Log into your Linux server via SSH and download the Linux version of BitTorrent Sync.

64-bit:

wget https://download-cdn.getsync.com/stable/linux-x64/BitTorrent-Sync_x64.tar.gz

32-bit

wget https://download-cdn.getsync.com/stable/linux-i386/BitTorrent-Sync_i386.tar.gz

Extract the tar file.

tar xvf BitTorrent-Sync_*.tar.gz

Now a new executable file named btsync will be extracted to the current working directory and it’s ready to run. You can start BitTorrent with the following command:

./btsync

By default the WebUI is only accessible on the computer running Sync (localhost:8888). In order to have the WebUI accessible across the LAN or Internet, start Sync this way:

 ./btsync --webui.listen 0.0.0.0:8888

Now you can access the WebUI by typing the following in your browser address bar. Replace your-server-ip with your actual server IP.

your-server-ip:8888

When you first access the WebUI, you will be asked to create an account. But we are using plain HTTP protocol right now and it’s unsafe. So before we create a username and password, we need to install a SSL certificate on the Linux Server. Read the following article to get a free SSL certificate from Let’s Encrypt.

An Overview of HTTPS Encryption and Let’s Encryption on Nginx Deployment

I will use sync.mydomain.com as the domain name to access the WebUI. So I applied a SSL certificate for sync.mydomain.com and you need to apply SSL certificate for sync.yourdomain.com. Replace yourdomain.com with your actuall domain name.

After you got a SSL certificate, come back here.

Setup Nginx Reverse Proxy

Now we will setup Nginx as a reverse proxy for the BitTorrent Sync service. In this setup, btsync only need to listen on the localhost (127.0.0.1).

Create a Nginx server block file.

sudo nano /etc/nginx/conf.d/sync.yourdomain.com.conf

Paste the following text into the file.

server {
   listen 80;
   server_name sync.yourdomain.com;
   return 301 https://sync.yourdomain.com$request_uri;
}
server {
   listen 443 ssl;
   server_name sync.yourdomain.com;

   ssl_protocols TLSv1.1 TLSv1.2;
   ssl_certificate /etc/letsencrypt/live/sync.yourdomain.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/sync.yourdomain.com/privkey.pem;
   access_log /var/log/nginx/sync.yourdomain.com.log;
   location / {
   proxy_pass http://127.0.0.1:8888;
   }
}

save and close the file. This configuration will redirect all plain HTTP request to HTTPS and the server will only accept TLSv1.1 and TLSv1.2 encryption. When you type sync.yourdomain.com in the browser address bar, Nginx will pass the request to 127.0.0.1:8888.

Reload Nginx

sudo service nginx reload       or      sudo systemctl reload nginx

Now access Sync WebUI by going to sync.yourdomain.com. Create a username and password to secure the WebUI.

Create a username and password to secure BitTorrent Sync WebUI

After you created an account and signed into WebUI. It’s time to add a folder on your master server for synchronization. But in order to add a folder on your server for syncing, the user running btsync process must have write permission to the folder which will be synced.

The user running btsync process is the user who invokes the btsync command.

Let’s say you want to sync the web root directory of your server such as /usr/share/nginx/ and this directory is owned by user www-data and group www-data. It’s permission is set as 755 so only www-data user have write permission.

drwxr-xr-x 7 www-data www-data 4096 Mar 9 21:58 nginx

In order to let user www-data and the user running btsync both have write permission, we can use setfacl command to grant write permissions to the user running btsync.

sudo setfacl -R -m "u:<username>:rwx" /usr/share/nginx

In this way, both www-data and the user have write permission to the web root directory. Now you can add the web root directory for syncing. btsync will generate a sharing link, secret key and QR code for this syncing folder.

add the web root directory for syncing

Setting Up the Slave Server

The process for the slave server is almost the same with master server. You install the btsync program, get another free SSL certificate from Let’s Encrypt and setup Nginx reverse proxy. Then create an account to secure the WebUI.

The only difference is that after you signed into the WebUI of the slave server, you need to click the arrow at the upper-left corner and select Enter a key or link from the drop-down menu.

BitTorrent Sync backup Linux Server

Then, go to the master server WebUI, click the share button, then copy the read only key and paste the read only key in the slave server WebUI.

BitTorrent Sync Secrect key

After that you need to select a directory on your slave sever to receive the files from master server. There are two keys. One is for read & write access, the other is for read only access. We copy the ready only key because we don’t want to let the slave server add new content to the syncing folder.

Now the two servers begin the syncing process.

Backup Linux Server with Your Home Computer

If you are using Linux as your home computer’s OS, then follow the above method to install BitTorrent Sync. After it’s installed, you can start btsync and begin syncing right away, you don’t have to setup Nginx reverse proxy and install SSL certificate on your home computer.

Backup Linux Server with Raspberry Pi

BitTorrent Sync has an ARM version, so you have to download the ARM installer for Raspberry Pi and then extract it.

wget https://download-cdn.getsync.com/stable/linux-arm/BitTorrent-Sync_arm.tar.gz

If your Raspberry Pi is headless, then in order to have the WebUI accessible across the LAN or Internet, start Sync this way:

./btsync --webui.listen 0.0.0.0:8888

Again, you don’t need to install SSL certificate on Raspberry Pi or setup Nginx reverse proxy.

Autostart on System Boot

To let BitTorrent Sync automatically start on system boot, add a cron job as a normal user.

crontab -e

add the following line at the end.

@reboot /path/to/btsync --webui.listen 0.0.0.0:8888

Replace /path/to/btsync with the actual path. If btsync only need to listen on localhost(127.0.0.1) then remove the –webui.listen 0.0.0.0:8888 part.

So you see it’s easy to backup Linux server and Happy syncing!

Rate this tutorial
[Total: 2 Average: 4.5]

2 Responses to “How to Use BitTorrent Sync to Backup Linux Server

  • really good guide.
    2 questions. do I follow same steps if I have a fedora server?
    and can I skip the Nginx Reverse Proxy thingy? if not, can you tell me why its necessary?

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here