在Linux上安装配置BitTorrent Sync


背景介绍:
目前我们线上的前端服务器数量比较多,超过200多台,每次发布新应用的时候,都是将软件包放在一台专门的Push服务器上,再由所有的前端服务器通过rsync自动同步。但随着前端服务器的数量越来越多,Push服务器的带宽已经成为了瓶颈。
而BitTorrent Sync这种P2P方式的同步则是一种解决方案。同时它的跨平台支持也非常好,无论是Windows,Linux,Mac OS,甚至手机端都有相应的客户端。虽然目前尚未开源,但可以免费使用,还是很不错的。

下面,就是我们在线上的纯Linux测试环境中的安装与配置步骤:
Servers:
idc2-server1,idc2-server2,idc2-server3

1. 下载BitTorrent Sync,在所有服务器上:
$ sudo wget http://download-lb.utorrent.com/endpoint/btsync/os/linux-x64/track/stable -O /tmp/btsync_x64.tar.gz
$ sudo mkdir /opt/btsync
$ cd /opt/btsync
$ sudo tar xzf /tmp/btsync_x64.tar.gz

2. 创建服务管理脚本,在所有服务器上:
$ sudo vim /etc/init.d/btsync

 
#!/bin/sh
#
# description: starts and stops the btsync client

CONF=/opt/btsync/btsync.cfg
PROC=/opt/btsync/btsync
PIDFILE=/opt/btsync/btsync.pid

start() {
  PID1=$(pidof btsync)
  if [ -z ${PID1} ]; then
    echo -n "Starting BitTorrent Sync: "
    ${PROC} --config ${CONF}
  else
    echo "BitTorrent Sync is already running at pid:${PID1}"
  fi
  return $?
}  

stop() {
  echo -n "Stopping BitTorrent Sync: "
  PID1=$(pidof btsync)
  if [ ! -z ${PID1} ]; then
    kill -9 ${PID1}
    echo "OK"
  else
    echo "Failed"
  fi
  return $?
}  

status() {
  PID1=$(pidof btsync)
  PID2=$(cat ${PIDFILE}) 
  echo -n "Checking BitTorrent Sync: "
  if [ ! -z ${PID1} ] && [ "${PID1}" -eq "${PID2}" ]; then
    echo "OK"
  else
    echo "Failed"
  fi
  return $?
}  

case "$1" in
  start)
   start
  ;;
  stop)
    stop
  ;;
  restart)
    stop
    sleep 1
    start
  ;;
  status)
    status
  ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 2
esac

$ sudo chmod +x /etc/init.d/btsync

3. 创建用于同步的目录,在所有服务器上:
$ sudo mkdir /opt/btsync_transfer

4. 创建配置文件,在idc2-server1上:
[heydevops@idc2-server1 btsync]$ sudo vim /opt/btsync/btsync.cfg

 
{ 
  "device_name": "idc2-server1",
  "listening_port" : 8889, // 0 - randomize port

  "check_for_updates" : false,
  "use_upnp" : false,

  "storage_path" : "/opt/btsync",
  "pid_file" : "/opt/btsync/btsync.pid",

  "download_limit" : 0, // 0 - no limit
  "upload_limit" : 0, 

  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "admin",
    "password" : "btsync"
  }

  ,
  "folder_rescan_interval" : 60,
  "lan_encrypt_data" : false,
  "lan_use_tcp" : true
}

5. 创建同步所需的密钥,在idc2-server1上:
$ sudo /etc/init.d/btsync start

打开Web UI:http://idc2-server1:8888
用户名: admin
密码: btsync

点击 "Add Folder",
在 "Path" 中输入 "/opt/btsync_transfer"
点击 "Generate" 得到 "Secret" 为 "ALUORWDEWOLV354ZHPHFT4TSQO67JWQAN"
如下图所示:
2

6. 创建配置文件,在idc2-server2和idc2-server3上:
[heydevops@idc2-server2 btsync]$ sudo vim btsync.cfg

 
{ 
  "device_name": "idc2-server2",
  "listening_port" : 8889, // 0 - randomize port

  "check_for_updates" : false,
  "use_upnp" : false,
  
  "storage_path" : "/opt/btsync",
  "pid_file" : "/opt/btsync/btsync.pid",

  "download_limit" : 0, // 0 - no limit
  "upload_limit" : 0, 

  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "admin",
    "password" : "btsync"
  }

  , 
  "shared_folders" :
  [
    {
      "secret" : "ALUORWDEWOLV354ZHPHFT4TSQO67JWQAN", // * required field
      "dir" : "/opt/btsync_transfer", // * required field
      "use_sync_trash" : false,
      "use_relay_server" : false,
      "use_tracker" : false,
      "search_lan" : true,
      "known_hosts" : 
      [
        "idc2-server1:8889"
      ]
    }
  ]

  ,
  "folder_rescan_interval" : 60,
  "lan_encrypt_data" : false, // Encryption is very painful in terms of speed. Disabling it for LAN to increase the speed. 
  "lan_use_tcp" : true
}

[heydevops@idc2-server2 btsync]$ sudo /etc/init.d/btsync start

[heydevops@idc2-server3 btsync]$ sudo vim btsync.cfg

 
{ 
  "device_name": "idc2-server3",
  "listening_port" : 8889, // 0 - randomize port
  
  "check_for_updates" : false,
  "use_upnp" : false,

  "storage_path" : "/opt/btsync",
  "pid_file" : "/opt/btsync/btsync.pid",

  "download_limit" : 0, // 0 - no limit
  "upload_limit" : 0, 

  "webui" :
  {
    "listen" : "0.0.0.0:8888",
    "login" : "admin",
    "password" : "btsync"
  }

  , 
  "shared_folders" :
  [
    {
      "secret" : "ALUORWDEWOLV354ZHPHFT4TSQO67JWQAN", // * required field
      "dir" : "/opt/btsync_transfer", // * required field
      "use_sync_trash" : false
      "use_relay_server" : false,
      "use_tracker" : false,
      "search_lan" : true,
      "known_hosts" : 
      [
        "idc2-server1:8889"
      ]
    }
  ]

  ,
  "folder_rescan_interval" : 60,
  "lan_encrypt_data" : false, // Encryption is very painful in terms of speed. Disabling it for LAN to increase the speed. 
  "lan_use_tcp" : true
}

[heydevops@idc2-server3 btsync]$ sudo /etc/init.d/btsync start

7. 在/opt/btsync_transfer中放置一些文件,就可以查看同步的状态了。
打开Web UI:http://idc2-server1:8888
如下图所示:
btsync_2

,

  1. #1 by 517charles on 2014/01/12 - 20:28

    写的不错,很好,很喜欢,不知道贵公司是干嘛的,前端服务器尤200多台呀

  2. #2 by 销声匿迹linux on 2014/01/14 - 13:20

    第四步的配置文件里面
    "device_name": "idc2-server3",
    应该是
    "device_name": "idc2-server1",
    才对吧?

    另外是否可以简单的说下配置文件里面有些参数的意义,比如说
    use_sync_trash是同步删除的意思么?

    • #3 by mcsrainbow on 2014/01/14 - 17:03

      OK,已修正。
      默认是将删除的文件移动到一个Trash目录,而不是真正的删除,所以我改成了False来禁用这个功能。

  3. #4 by cartier on 2014/03/06 - 14:27

    这个只能做一台服务器到多台服务器的分发吗?能不能做到两台服务器上某个文件夹的双向同步呢?谢谢!

  4. #5 by cartier on 2014/03/06 - 16:25

    您好!博主
    还想问您一下,这个是实时的吗?是不是源服务器上文件一旦建立,下面的被同步的服务器会立即开始复制?谢谢!

  5. #6 by cartier on 2014/03/07 - 15:08

    您好!博主
    我实际按照您的文档做了实验,我起初做了三台服务器,A B C ,A为主服务器,主同步文件夹就设在A上,B C为被同步的服务器,三者之间的同步时正常的,但是我发现当我再将一台服务器D加进去的时候,在D被同步的过程中,我看到只有A的网口有明显的同步数据流量,而B和C都没有发出数据流量帮助A来同步D,这是什么原因,谢谢!请帮忙分析一下,不胜感激!

(will not be published)
*