天天看點

lsync目錄檔案實時同步工具

參考文檔:https://vastxiao.github.io/article/2017/09/02/lsync-201709021425/

官網:https://axkibe.github.io/lsyncd/

1、使用環境

伺服器角色 作業系統類型 IP位址 目錄 目标
源伺服器 Centos7.4 11.11.11.2 /share 實時同步到目标伺服器
目标伺服器 Centos7.4 11.11.11.3 /opt/share

2、安裝

2.1、目标伺服器需要安裝的依賴包

yum -y install lua lua-devel rsync      

2.2、源伺服器配置

1、配置源伺服器免密碼ssh登入目标伺服器

如果要通過ssh的方式,從源伺服器同步到目标伺服器的話,需要在源伺服器上配置ssh免密碼登入,如果是同一台伺服器的本地目錄同步到另一個目錄的話,不需要配置ssh免密碼登入      

su - root #用root使用者進行遠端同步

ssh-keygen -t dsa #一路回車

ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected] #将源伺服器的公鑰發送到目标伺服器,這裡需要輸入目标伺服器的root使用者密碼

ssh [email protected] ip address #檢視結果是否為目标伺服器的ip位址,如果是的話,說明ssh免密鑰登入配置成功

2、安裝依賴包

yum -y install lua lua-devel rsyn      

3、安裝lsync

yum -y install lsyncd       #這條指令隻适用于Centos 6.*版本的系統,7以上的系統會報找不到lsyncd包

#Cnetos 7.*版本如果要裝lsyncd的話,需要先安裝擴充源eple如下,然後在執行yum -y install lsyncd
# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -ivh epel-release-latest-7.noarch.rpm      

3、yum安裝檔案結構

路徑              說明
/etc/lsyncd.conf    主配置檔案
/etc/sysconfig/lsyncd       init環境變量和啟動選項配置檔案
/etc/logrotate.d/lsyncd    日志滾動配置檔案
/usr/share/doc/lsyncd-*/examples/    目錄下有lsyncd.conf配置例子
/etc/init.d/lsyncd         lsyncd的init啟動腳本
/usr/bin/lsyncd     lsyncd指令路徑
/var/run/lsyncd/    可放lsyncd.pid的目錄
/var/log/lsyncd/    預設的日志目錄      

4、檔案配置

4.1、使用遠端同步模式同步資料(正式用)

這裡ssh端口改為22022

實作結果:對源目錄下的檔案進行增删改都會同步到目标伺服器,但需要注意的是在啟動服務後會删除目标服務指定的目錄下的檔案(類似于鏡像檔案)

[root@localhost share]# cat /etc/lsyncd.conf
settings {
    inotifyMode = "CloseWrite",
    statusFile = "/var/log/lsyncd.status",
    statusInterval = 3,
    logfile = "/var/log/lsyncd.log"
    }

sync {
    default.rsyncssh,
    source = "/share",
    host = "11.11.11.3",
    targetdir = "/opt/share/",
    maxDelays = 1,
    delay = 3,
    -- init = fales,
    rsync = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose = true,
        _extra = {"--bwlimit=2000"},
        },
        ssh = {
           port = 22022
        }
    }
[root@localhost share]#      
啟動服務
centos6
# /etc/init.d/lsyncd start
設定開機啟動
# chkconfig lsyncd on
centos7
#systemctl start lsyncd.service
設定開機啟動
systemctl enable lsyncd.service       

參數詳解:

setting配置參數詳解
inotifyMode: 預設是CloseWrite,還可以是Modify或者 CloseWrite or Modify
statusFile: 定義狀态檔案位置
statusInterval:将lsyncd的狀态寫入statusFile檔案的建個時間,預設是10s ,對實時同步要求比較高的話,可以調小,我設定為3s。
logfile:定義日志檔案的位置,同步内容也可以在此檔案看到

sync配置參數詳解
 default.rsyncssh:同步到遠端主機目錄,rsync的ssh模式,需要使用key來認證
 source:源目錄,需要被同步的目錄
 host:遠端備份主機IP
 targetdir:遠端備份目錄
 delay:等待rsync同步延時時間,預設15s,如果對實時同步要求比較高,可以設定相對低點,我設定的為3s
 init: 預設為true,啟動lsyncd服務後,同步源目錄裡邊的所有資料,如果值為false時,隻同步源目錄lsyncd服務啟動後改變的檔案
   rsync選項中參數:
     binary:定義rsync的執行位置
     archive:定義傳輸是否歸檔
     compress:定義遠端創術是否壓縮,
     verbose:定義同步時顯示的詳細資訊,列出同步的檔案
     _extra:限制傳輸速度,為了不影響正常業務運作,可以做此設定,機關kb/s,與rsync相同(計算方法:10MB/s 等于 10 x 1024/8 = 1280kb/s; 2000Kb/s 等于2000 x 8/1024 = 15.6MB/s)
   ssh選項參數
     port:定義遠端備份主機的遠端端口,定義非22端口,如果是22端口,可以不寫。      

4.2、使用本地同步模式同步資料(本地備份)

[root@tomcat2 ~]# cat /etc/lsyncd.conf 
settings {
   --pidfile = "/var/run/lsyncd/lsyncd.pid",
   --nodaemon  = false,
   inotifyMode = "CloseWrite",
   maxProcesses = 8,
   statusFile = "/tmp/lsyncd.status",
   statusInterval = 10,
   logfile = "/var/log/lsyncd/lsyncd.log"
}

sync {
   default.rsync,
   source = "/opt/webapplication",
   target = "/back_up/webapplication",
   delete = "false",
   --exclude = { "logs" },
   delay = 5,
   --init = true,
   rsync    = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
        bwlimit = 2000
    }
}

sync {
   default.rsync,
   source = "/data",
   target = "/back_up/data",
   delete = "false",
   --exclude = { "logs" },
   delay = 5,
   --init = true,
   rsync    = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
        bwlimit = 2000
    }
}
[root@tomcat2 ~]#       

4.3、其他同步方式

settings {
    logfile ="/usr/local/lsyncd-2.1.5/var/lsyncd.log",
    statusFile ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",
    inotifyMode = "CloseWrite",
    maxProcesses = 8,
    }
-- I. 本地目錄同步,direct:cp/rm/mv。 适用:500+萬檔案,變動不大
sync {
    default.direct,
    source    = "/tmp/src",
    target    = "/tmp/dest",
    delay = 1
    maxProcesses = 1
    }
-- II. 本地目錄同步,rsync模式:rsync
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "/tmp/dest1",
    excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        bwlimit   = 2000
        }
    }
-- III. 遠端目錄同步,rsync模式 + rsyncd daemon
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "[email protected]::module1",
    delete="running",
    exclude = { ".*", ".tmp" },
    delay = 30,
    init = false,
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        password_file = "/etc/rsyncd.d/rsync.pwd",
        _extra    = {"--bwlimit=200"}
        }
    }
-- IV. 遠端目錄同步,rsync模式 + ssh shell
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "172.29.88.223:/tmp/dest",
    -- target    = "[email protected]:/remote/dest",
    -- 上面target,注意如果是普通使用者,必須擁有寫權限
    maxDelays = 5,
    delay = 30,
    -- init = true,    程序啟動時是否全部同步一次,預設是true
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        bwlimit   = 2000
        -- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
        -- 如果要指定其它端口,請用上面的rsh
        }
    }
-- V. 遠端目錄同步,rsync模式 + rsyncssh,效果與上面相同
sync {
    default.rsyncssh,
    source    = "/tmp/src2",
    host      = "172.29.88.223",
    targetdir = "/remote/dir",
    excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    -- maxDelays = 5,
    delay = 0,
    -- init = false,
    rsync    = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        _extra = {"--bwlimit=2000"},
        },
    ssh      = {
        port  =  1234
        }
    }      

5、使用腳本監測lsyncd服務并設定定時任務

腳本每25秒監測一次lsyncd服務的程序是否正常,若程序不在了,則自動啟動程序,正常與否都會寫入日志檔案
```bash
cat /opt/lsync_monitor.sh
#!/bin/bash
source /etc/profile

START()
{
lsyncd -log Exec /etc/lsyncd.conf
sleep 5
}

CHECK()
{
num=`ps -ef | grep lsyncd.conf | grep -v grep | wc -l`
if [ $num -eq 0 ];then
  {
  dat=`date`
  echo "$dat lsyncd stoped!!!"
  return 1
  }
elif [ $num -eq 1 ];then
  {
  dat=`date`
  echo "$dat lsyncd running..."
  return 0
  }
fi
}

TOT()
{
CHECK
res=$?

if [ $res -eq 1 ];then
  START
fi
}

TOT
sleep 25
TOT      

6、其他設定

##### 5.2 lsync_monitor.sh産生日志隻保留1個月的記錄
由于lsync_monitor.sh每分鐘寫入6行内容,是以一天下來日志量也是比較大的
```bash
cat /opt/month.sh
#!/bin/bash
source /etc/profile
mv -f /var/log/lsync_monitor.log /var/log/last_month.log
> /var/log/lsync_monitor.log
```
##### 5.3 加入crontab定時任務
```bash
crontab -e
*/5 * * * * /usr/sbin/ntpdate -s 192.168.8.18
#####lsync to 10.66.84.16:/opt/share======
*/1 * * * * /opt/lsync_monitor.sh >> /var/log/lsync_monitor.log
0 0 1 * * /opt/month.sh      

FAQ:

當需要同步的資料量上百G時,程序起來一會後,自動停了。檢視日志:
因為同步的資料量較大,需要lsyncd監測的inode數量超過了自身預設的最大監控數量,是以報錯
```bash
tail -f /var/log/lsyncd/lsyncd.log
遇到:Error: Terminating since out of inotify watches.
Consider increasing /proc/sys/fs/inotify/max_user_watches

解決辦法:
echo 65535000 >  /proc/sys/fs/inotify/max_user_watches
這個值改成多少需要根據源目錄所在分區的的inode多少來定(使用df -i檢視該分區總的inode數量)
sysctl -p
重新開機lsyncd