使用BitTorrent-Sync實現雙機文件雙向同步


BitTorrent-Sync是一款基於P2P的分布式文件同步工具,簡稱btsync,非開源軟件但免費使用。本文使用btsync實現兩台服務器上的軟件雙向同步。

安裝

直接從官網下載相應的安裝包,為了在Linux服務器上安裝,使用的是Linux x64版本BitTorrent-Sync_x64.tar.gz

然后解壓到指定的文件夾,即可執行程序。


sudo mkdir -p /usr/local/btsync
sudo chown shenfeng /usr/local/btsync
tar -zxvf BitTorrent-Sync_x64.tar.gz -C /usr/local/btsync/

進入解壓后的目錄,可以看到3個文件,其中btsync為可執行文件。


$ ls
btsync  LICENSE.TXT  README

執行--help可以查看幫助信息


 ./btsync --help

BitTorrent Sync 2.3 (239)
Usage:
	  btsync [ options ... ]
Options:
	--help                    Print this message
	--config 
  
  
  
          
            Use a configuration file --storage 
           
             Storage path for identity and license --identity 
            
              Creates user identity --license 
             
               Apply owner license --nodaemon Do not daemonize --dump-sample-config Print a sample configuration file --log 
              
                Set log file --webui.listen 
               
                 : 
                
                  Set the webui listening interface --generate-secret Generate a read/write key --get-ro-secret 
                 
                   Get the read-only key associated to a read/write key 
                  
                 
                
               
              
             
            
          

配置

編寫啟動腳本。


$ sudo vi /etc/init.d/btsync
#!/bin/sh
#
# description: starts and stops the btsync client

CONF=/usr/local/btsync/btsync.cfg
PROC=/usr/local/btsync/btsync
PIDFILE=/usr/local/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

編寫啟動配置文件


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

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

  "storage_path" : "/usr/local/btsync",
  "pid_file" : "/usr/local/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" : 1,
  "lan_encrypt_data" : false,
}

啟動程序,通過web頁面進行登錄和后續配置。


$ /etc/init.d/btsync start
Starting BitTorrent Sync: By using this application, you agree to our Privacy Policy, Terms of Use and End User License Agreement.
https://www.getsync.com/legal/privacy
https://www.getsync.com/legal/terms-of-use
https://www.getsync.com/legal/eula

Webui is listening on 0.0.0.0:8888
BitTorrent Sync forked to background. pid = 15002

在瀏覽器頁面登錄控制頁面,即服務器IP+8888端口。

使用配置文件中的賬號密碼進行登錄。

login

在啟動一台主機上,點擊左上角的Add Folder按鈕,增加需要同步的目錄。
add folder

確認打開目錄后,會生成該目錄的Key,因為需要雙向同步,那么選擇READ+WRITE的Key。
copy_key

登錄第二台服務器,登錄后,點擊Add Folder的第三個選項。填入上面復制的Key之后,選擇本地用於同步的目錄就可以進行同步了。
enter_key

Sync_result

參考文檔



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM