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 root@11.11.11.3 #將源服務器的公鑰發送到目標服務器,這里需要輸入目標服務器的root用戶密碼
ssh root@11.11.11.3 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    = "syncuser@172.29.88.223::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    = "root@172.29.88.223:/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

 


免責聲明!

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



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