Sync hacks: How to easily sync config files between servers

sync-hacks-2

In Sync Hacks, we spotlight cool uses of Sync from the creative minds of our users. Sync is our free, unlimited, and secure file-syncing application. If you have an interesting use or how-to, shoot us an email at sync[at]bittorrent.com. Can’t wait to hear what you guys cook up.

In this week’s edition of Sync Hacks, Marty Strong shows us how to keep config files synced between multiple servers using BitTorrent Sync. It allows him to create user accounts for his VPNs on a single server. Marty also hosts his friends’ pages and this allows his friends to update their sites easily.

We asked him why he chose Sync. His response: I needed an easy way to keep files synced without relying on a third-party Cloud storage service. What he likes about Sync? It’s free, easy to setup & use, fast, distributed and secure. “I could go on,” he says. But we don’t let him.

Read on to see his full post originally posted on his personal blog here.

Installing and configuring BitTorrent Sync to sync config files between servers

In this tutorial you will need:

1. At least two servers

2. BitTorrent Sync

For this tutorial I will be using two 64bit Ubuntu VPSs, mileage may vary slightly depending on your platform of choice.

Step One:

Fire up your two servers (if they aren’t already running), once ready install the package you wish to sync config files for.

Step Two:

SSH to one of your servers and create a .btsync directory and change to it.

mkdir ~/.btsync && cd ~/.btsync

Download the BitTorrent Sync package for your platform from here: http://labs.bittorrent.com/experiments/sync.html (click ‘Download’), for me that would be Linux x64.

wget http://btsync.s3-website-us-east-1.amazonaws.com/btsync_x64.tar.gz

Un-tar the package to the newly created directory

tar -xvf btsync_x64.tar.gz

Create a BT Sync config file and open it with Nano

nano btsync.conf

Add the following to the config file making sure to set your own username and password at the relevant section of the file, it may also be useful to name the machine e.g. ”device_name”: “Server 1″

{
  "device_name": "My Sync Device",
  "listening_port" : 0,                       // 0 - randomize port

/* storage_path dir contains auxilliary app files
   if no storage_path field: .sync dir created in the directory
   where binary is located.
   otherwise user-defined directory will be used
*/
  "storage_path" : "/home/user/.sync",

// uncomment next line if you want to set location of pid file
// "pid_file" : "/var/run/btsync/btsync.pid",

  "check_for_updates" : true,
  "use_upnp" : true,                              // use UPnP for port mapping

/* limits in kB/s
   0 - no limit
*/
  "download_limit" : 0,
  "upload_limit" : 0,

/* remove "listen" field to disable WebUI
   remove "login" and "password" fields to disable credentials check
*/
  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "admin",
    "password" : "password"
  }

/* !!! if you set shared folders in config file WebUI will be DISABLED !!!
   shared directories specified in config file
   override the folders previously added from WebUI.
*/
/*
  ,
  "shared_folders" :
  [
    {
//  use --generate-secret in command line to create new secret
      "secret" : "MY_SECRET_1",                   // * required field
      "dir" : "/home/user/bittorrent/sync_test", // * required field

//  use relay server when direct connection fails
      "use_relay_server" : true,
      "use_tracker" : true,
      "use_dht" : false,
      "search_lan" : true,
//  enable sync trash to store files deleted on remote devices
      "use_sync_trash" : true,
//  specify hosts to attempt connection without additional search
      "known_hosts" :
      [
        "192.168.1.2:44444"
      ]
    }
  ]
*/

// Advanced preferences can be added to config file.
// Info is available in BitTorrent Sync User Guide.

}

Create a startup script and open it with Nano

nano /etc/init.d/btsync

Add the following to the startup script, this has been adapted from Jack Minardi’s from here: http://jack.minardi.org/raspberry_pi/replace-dropbox-with-bittorrent-sync-and-a-raspberry-pi/, make sure you replace USERACCOUNT with the account you will be running the script from

#! /bin/sh
# /etc/init.d/btsync
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
    /USERACCOUNT/.btsync/btsync --config /USERACCOUNT/.btsync/btsync.conf
    ;;
stop)
    killall btsync
    ;;
*)
    echo "Usage: /etc/init.d/btsync {start|stop}"
    exit 1
    ;;
esac

exit 0

Set the relevant permissions and add the init script

chmod 755 /etc/init.d/btsync
update-rc.d btsync defaults

Start BT Sync

service btsync start

You’ve now installed BT Sync and should be able to access it by browsing to your server’s IP address on port 8888 e.g. http://1.2.3.4:8888

Step Three:

Repeat Step Two on your other server(s)

Step Four:

Connect to BT Sync on one of your servers in your browser and click ‘Add Folder’

Click on the ‘Generate’ button for BT Sync to generate a secret and paste this secret to a text editor

Add the path to the box or click on the folders until you reach your desired conf folder

Click ‘Add’, this will index the directory and display it’s status to you on screen.

Step Five:

Connect to BT Sync on your other server(s) in your browser and click the ‘Add Folder’ button as you did before

Do NOT click ‘Generate’ this time but instead paste in the secret you generated on your first server

Select the folder that the config files reside in (as you did in step four)

Click ‘Add’, this will index the directory like it did before, but this time because you have another BT Sync instance with the same folder/secret combination they will start syncing with each other, you should be able to see the status on the screen.

Marty Strong (@martystronguk) is a casual blogger currently working at CloudFlare in London (www.cloudflare.com). He’s a big Formula 1 fan and loves travelling the world. You can visit his personal blog here: www.mjcs.co.uk