近期公司要上線新項目,後臺框架選型我選擇當前較爲流行的laravel,運行環境使用lnmp。php
以前我這邊項目tp32+apache,開發工具使用phpstorm。mysql
新建/編輯文件經過phpstorm配置自動上傳到測試服務器web根目錄(固然服務器根目錄只是本身使用的,http訪問也是用本身的域名)。linux
用laravel要用到composer,win10的cmd下面用起來不方便,就本地用virtual box + vagrant搭建基於centos7.1的lnmp開發環境,vagrant掛載本地代碼目錄。laravel
測試環境的redis,mysql等不能直接在本地訪問。git
搜索資料,瞭解到centos的vim下編輯文件保存時可實時將該文件同步到測試環境。github
如下內容截取自部分搜索結果,稍有改動:web
1、爲何要用Rsync+sersync架構?redis
一、sersync是基於Inotify開發的,相似於Inotify-tools的工具sql
二、sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字,而後使用rsync同步的時候,只同步發生變化的這個文件或者這個目錄。express
2、Rsync+Inotify-tools與Rsync+sersync這兩種架構有什麼區別?
一、Rsync+Inotify-tools
(1):Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增長、刪除、修改),並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;
(2):rsync在同步的時候,並不知道具體是哪一個文件或者哪一個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以,效率很低。
二、Rsync+sersync
(1):sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;
(2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來講是很小的,rsync在遍歷查找比對文件時,速度很快),所以,效率很高。
小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件不少時,建議使用Rsync+sersync。
說明:
vagrant搭建的源服務器(CentOS Linux release 7.5.1804 (Core)): (Sersync+web)
可鏈接redis和mysql的目標服務器(測試環境): (Rsync+web)
目的:把源服務器上/opt/www/yingguzhitou_cms目錄實時同步到目標服務器的/aliadata/user/carl/yingguzhitou_cms目錄
具體操做:
第一部分:在目標服務器安裝Rsync服務端
一、關閉SELINUX
#vi /etc/selinux/config #編輯防火牆配置文件
#SELINUX=enforcing#註釋掉
#SELINUXTYPE=targeted#註釋掉
SELINUX=disabled#增長
:wq! #保存,退出
setenforce 0 #當即生效
二、開啓防火牆tcp 873端口(Rsync默認端口)
#vi /etc/sysconfig/iptables #編輯防火牆配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -ptcp --dport 873 -j ACCEPT
:wq! #保存退出
#systemctl restart firewalld.service #最後重啓防火牆使配置生效
三、安裝Rsync服務端軟件
#yum install rsync xinetd #安裝
# vi /etc/rc.d/rc.local #設置開機啓動
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
:wq! #保存退出
# chmod +x/etc/rc.d/rc.local #不然重啓不執行
# systemctl start xinetd #啓動(CentOS中是以xinetd來管理Rsync服務的)
四、建立rsyncd.conf配置文件
#vi /etc/rsyncd.conf #建立配置文件,添加如下代碼
log file =/var/log/rsyncd.log #日誌文件位置,啓動rsync後自動產生這個文件,無需提早建立
pidfile =/var/run/rsyncd.pid #pid文件的存放位置
lock file =/var/run/rsync.lock #支持max connections參數的鎖文件
secretsfile = /etc/rsync.pass #用戶認證配置文件,裏面保存用戶名稱和密碼,後面會建立這個文件
motd file =/etc/rsyncd.Motd #rsync啓動時歡迎信息頁面文件位置(文件內容自定義)
[Sync] #自定義名稱
path = /aliadata/user/carl/yingguzhitou_cms/#rsync服務端數據目錄路徑
comment = 這是註釋#模塊名稱與[md]自定義名稱相同
uid = root #設置rsync運行權限爲root
gid = root #設置rsync運行權限爲root
port=873 #默認端口
use chroot= no #默認爲true,修改成no,增長對目錄文件軟鏈接的備份
read only =no #設置rsync服務端文件爲讀寫權限
list = no #不顯示rsync服務端資源列表
maxconnections = 200 #最大鏈接數
timeout =600 #設置超時時間
auth users= Sync #執行數據同步的用戶名,能夠設置多個,用英文狀態下逗號隔開
hosts allow= 源服務器ip #容許進行數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開
hosts deny= 192.168.21.xxx #禁止數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開
:wq! #保存,退出(貼進配置文件將中文去掉,不然可能形成沒法識別模塊)
五、建立用戶認證文件
#vim /etc/rsync.pass #配置文件,添加如下內容,添加容許傳輸用戶和密碼
fRs6dkR2yQsT #格式,用戶名:密碼(我這裏只保存密碼,vagrant上傳使用root操做)能夠設置多個,每行一個用戶名:密碼
:wq! #保存退出
六、設置文件權限
#chmod 600 /etc/rsyncd.conf #設置文件全部者讀取、寫入權限
#chmod 600 /etc/rsync.pass #設置文件全部者讀取、寫入權限
七、啓動rsync
# systemctl start xinetd #啓動
# systemctl stop xinetd #中止
# systemctl restart xinetd #從新啓動
第二部分:在源服務器上操做
1、安裝Rsync客戶端
一、關閉SELINUX
#vim /etc/selinux/config #編輯防火牆配置文件
#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增長
:wq! #保存退出
#setenforce 0 #當即生效
二、開啓防火牆tcp 873端口(Rsync默認端口,作爲客戶端的Rsync能夠不用開啓873端口)
#vim /etc/sysconfig/iptables #編輯防火牆配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -ptcp --dport 873 -j ACCEPT
:wq! #保存退出
# systemctl restartfirewalld.service #最後重啓防火牆使配置生效
三、安裝Rsync客戶端端軟件
#whereis rsync #查看系統是否已安裝rsync,出現下面的提示,說明已經安裝
rsync:/usr/bin/rsync /usr/share/man/man1/rsync.1.gz
#yum install xinetd #只安裝xinetd便可,CentOS中是以xinetd來管理rsync服務的
#yum install rsync xinetd #若是默認沒有rsync,運行此命令進行安裝rsync和xinetd
# vi /etc/rc.local #設置開機啓動
/usr/bin/rsync --daemon
#vi /etc/reyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
motd file = /etc/rsyncd.Motd
[yingguzhitou_cms]
comment = 這是註釋
uid = root
gid = root
port=873
:wq退出
# chmod + x /etc/rc.d/rc.local #不然重啓不執行
#systemctl start xinetd #啓動(CentOS中是以xinetd來管理rsync服務的)
四、建立認證密碼文件
#vim /etc/passwd.txt #編輯文件,添加如下內容,該密碼應與目標服務器中的/etc/rsync.pass中的密碼一致,
fRs6dkR2yQsT #密碼
:wq! #保存退出
#chmod 600 /etc/passwd.txt #設置文件權限,只設置文件全部者具備讀取、寫入權限便可
五、測試源服務器到目標服務器之間的數據同步
#mkdir -p /opt/www/yingguzhitou_cms/ceshi #在源服務器上建立測試文件夾,而後在源服務器運行下面1行命令
rsync -avH --port=873 --progress --delete /opt/www/yingguzhitou_cms/ root@目標服務器IP::Sync --password-file=/etc/passwd.txt
運行完成後,分別在目標服務器上查看,在/alidata/user/carl/yingguzhitou_cms/目錄下有ceshi文件夾,說明數據同步成功,命令中目錄能夠隨意目錄,傳輸到目標服務器目錄時文件(或目錄)均放到/etc/rsyncd.conf配置的服務器目錄路徑,若是源目錄改變了,那麼傳輸時兩個目錄將進行目錄匹配,會有增刪動做,所以須要注意。
第三部分:安裝sersync工具,實時觸發rsync進行同步
一、查看服務器內核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目錄,出現下面的內容,說明服務器內核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
備註:Linux下支持inotify的內核最小爲2.6.13,能夠輸入命令:#uname -a查看內核
CentOS 7默認已經支持inotify
二、修改inotify默認參數(inotify默認內核參數值過小)
查看系統默認參數值:
sysctl -a | grep max_queued_events
結果是:fs.inotify.max_queued_events= 16384
sysctl -a | grep max_user_watches
結果是:fs.inotify.max_user_watches= 8192
sysctl -a | grep max_user_instances
結果是:fs.inotify.max_user_instances= 128
修改參數:
#sysctl -wfs.inotify.max_queued_events="99999999"
#sysctl -w fs.inotify.max_user_watches="99999999"
#sysctl -wfs.inotify.max_user_instances="65535"
#vi /etc/sysctl.conf #添加如下代碼
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出
參數說明:
max_queued_events:
inotify隊列最大長度,若是值過小,會出現"** Event QueueOverflow **"錯誤,致使監控文件不許確
max_user_watches:
要同步的文件包含多少目錄
max_user_instances:
每一個用戶建立inotify實例最大值
三、安裝sersync
sersync下載地址:
進入/usr/local/src目錄
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
#cd /usr/local/src
#tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz #解壓
#mv GNU-Linux-x86 /usr/local/sersync #移動目錄到/usr/local/sersync
四、配置sersync
#cd /usr/local/sersync #進入sersync安裝目錄
#cp confxml.xml confxml.xml-bak #備份原文件
#vim confxml.xml #編輯,修改下面的代碼
<?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"> <host hostip="localhost" port="8008"></host> <debug start="false"/> <fileSystem xfs="false"/> <filter start="false"> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/opt/www/yingguzhitou_cms/"> <remote ip="114.55.xxx.xxx" name="yingguzhitou_cms"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="false" users="root" passwordfile="/etc/passwd.txt"/> <userDefinedPort start="false" port="873"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> <!--ç¦æ¢å®æ¶å ¨çåæ¥ --> <crontab start="false" schedule="600"><!--600mins--> <crontabfilter start="false"> <exclude expression="*.php"></exclude> <exclude expression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync> <plugin name="command"> <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> <filter start="false"> <include expression="(.*)\.php"/> <include expression="(.*)\.sh"/> </filter> </plugin> <plugin name="socket"> <localpath watch="/opt/tongbu"> <deshost ip="192.168.138.20" port="8009"/> </localpath> </plugin> <plugin name="refreshCDN"> <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> <sendurl base="http://pic.xoyo.com/cms"/> <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> </localpath> </plugin> </head>
:wq! #保存退出
參數說明:
localpath watch="/opt/www/yingguzhitou_cms/":#源服務器同步目錄
114.55.xxx.xxx:#目標服務器IP地址
name="yingguzhitou_cms": #目標服務器rsync同步目錄模塊名稱
users="root": #目標服務器rsync同步用戶名
passwordfile="/etc/passwd.pass": #目標服務器rsync同步用戶的密碼在源服務器的存放路徑
remote ip="114.55.xxx.xxx": #目標服務器ip,每行一個
failLogpath="/tmp/rsync_fail_log.sh" #腳本運行失敗日誌記錄
start="true" #設置爲true,每隔600分鐘執行一次全盤同步
五、設置sersync監控開機自動執行
#vim /etc/rc.d/rc.local #編輯,在最後添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #設置開機自動運行腳本
:wq! #保存退出
# chmod +x /etc/rc.d/rc.local #不然重啓不執行
六、添加腳本監控sersync是否正常運行
#mkdir /home/crontab
#vim /opt/crontab/check_sersync.sh #編輯,添加如下代碼
#!/bin/bash #添加腳本監控sersync是否正常運行 sersync="/usr/local/sersync/sersync2" confxml="/usr/local/sersync/confxml.xml" status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l) if [ $status -eq 0 ]; then $sersync -d -r -o $confxml & else exit 0; fi
:wq! #保存退出
#chmod +x /opt/crontab/check_sersync.sh#添加腳本執行權限
#vim /etc/crontab #編輯,在最後添加下面一行
*/5 * * * * /opt/crontab/check_sersync.sh >/dev/null 2>&1 #每隔5分鐘執行一次腳本
#從新加載服務
#systemctl restart crond.service
六、測試sersync實時觸發rsync同步腳本是否正常運行
在源服務器上建立文件inotify_rsync_ceshi
#mkdir /opt/www/yingguzhitou_cms/inotify_rsync_ceshi
從新啓動源服務器
等系統啓動以後,查看目標服務器的/alidata/user/carl/yingguzhitou_cms下是否有inotify_rsync_ceshi文件夾
若是測試都經過,說明inotify實時觸發rsync同步腳本運行正常。
至此,Linux下Rsync+sersync實現數據實時同步完成。
參考地址:https://www.linuxidc.com/Linux/2017-10/147900.htm