參考文檔:https://vastxiao.github.io/article/2017/09/02/lsync-201709021425/css
官網:https://axkibe.github.io/lsyncd/node
服務器角色 | 操做系統類型 | IP地址 | 目錄 | 目標 |
源服務器 | Centos7.4 | 11.11.11.2 | /share | 實時同步到目標服務器 |
目標服務器 | Centos7.4 | 11.11.11.3 | /opt/share |
yum -y install lua lua-devel rsync
一、配置源服務器免密碼ssh登陸目標服務器git
若是要經過ssh的方式,從源服務器同步到目標服務器的話,須要在源服務器上配置ssh免密碼登陸,若是是同一臺服務器的本地目錄同步到另外一個目錄的話,不須要配置ssh免密碼登陸
二、安裝依賴包github
yum -y install lua lua-devel rsyn
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
路徑 說明 /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/ 默認的日誌目錄
這裏ssh端口改成22022web
實現結果:對源目錄下的文件進行增刪改都會同步到目標服務器,但須要注意的是在啓動服務後會刪除目標服務指定的目錄下的文件(相似於鏡像文件)shell
[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
參數詳解:centos
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端口,能夠不寫。
[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 ~]#
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 } }
腳本每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
六、其餘設置tomcat
##### 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:bash
當須要同步的數據量上百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