中小型網站搭建-數據實時的複製-inotify/sersyncphp
inotify是一種強大的,細粒度的、異步的文件系統事件監控機制(軟件),linux內核從2.6.13起,加入inotify支持,經過inotify能夠監控文件系統中添加、刪除、修改、移動等各類事件。linux
1. backup部署rsync服務端 /nfsbackup目錄shell
# mkdir -p /nfsbackup/express
# mkdir -p /backup/vim
# useradd -s /sbin/nologin -M rsync緩存
# chown -R rsync.rsync /backupbash
# chown -R rsync.rsync /nfsbackup多線程
# ll -d /backup/ /nfsbackup/併發
drwxr-xr-x 2 rsync rsync 4096 Jun 5 22:21 /backup/dom
drwxr-xr-x 2 rsync rsync 4096 Jun 5 22:18 /nfsbackup/
# echo 'rsync_backup:123456' >/etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:123456
# chmod 600 /etc/rsync.password
# ll /etc/rsync.password
-rw------- 1 root root 20 Jun 5 22:30 /etc/rsync.password
# vim /etc/rsyncd.conf
######rsync_config_______________start
#created by hkping 15:01 2018-5-27
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
path = /backup/
[nfsbackup]
path = /nfsbackup/
# rsync --daemon
# echo 'rsync --daemon' >> /etc/rc.local
# ps -ef|grep rsync
root 1481 1 0 22:15 ? 00:00:00 rsync --daemon
root 1484 1374 0 22:17 pts/0 00:00:00 grep rsync
2. 客戶端nfs01配置,並測試nfs01推送數據rsync
# echo '123456' > /etc/rsync.password
# chmod 600 /etc/rsync.password
# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
sending incremental file list
sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 310 speedup is 9.12
3. 安裝epel源,nfs01配置inotify,安裝inotify-tools
# yum install epel-release -y 通常以前優化已經有了epel源(可不作)
關鍵參數說明
# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_queued_events 可容納的事件數量
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_user_instances 可運行的進程數
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_user_watches 可監視文件數量
# yum install inotify-tools -y
# rpm -ql inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch
一共安裝了2個工具(命令),即inotifywait和inotifywatch
inotifywait(經常使用) 在被監控的文件或目錄上等待特定文件系統事件(open、close,delete等)發生,執行後處於阻塞狀態,適合在shell腳本中使用。
inotifywatch 收集被監視的文件系統使用度統計數據,指文件系統事件發生的次數統計。
-r 遞歸查詢目錄
-q 打印不多的信息,僅僅打印監控事件的信息
-m 始終保持事件監聽狀態
--timefmt 指定時間輸出的格式
--format 打印使用指定的輸出相似格式字符串
-e 指定要監視的事件列表
4. 測試監控,複製nfs01窗口測試
# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e create 建立
# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e delete 刪除
# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e close_write 文件內容是否修改
# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e moved_to 重命名
5. 在nfs01,寫監控腳本,運行監控腳本
# mkdir -p /server/scripts
# vim jiankong.sh
#!/bin/bash
#desc: watch /data dir && rsync to backup
inotifywait -mrq /data/ --format "%w%f" -e create,modify,close_write,moved_to|while read line
do
rsync -az /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
done
# sh /server/scripts/jiankong.sh
# /bin/sh /server/scripts/jiankong.sh & 把監控腳本放在後臺運行
# ps -ef|grep jiankong
root 2088 1736 0 22:14 pts/1 00:00:00 /bin/sh /server/scripts/jiankong.sh
root 2090 2088 0 22:14 pts/1 00:00:00 /bin/sh /server/scripts/jiankong.sh
6. 同步測試文件
# touch /data/oldboy{01..10}.txt
# ll /nfsbackup/
total 8
-rw-r--r-- 1 rsync rsync 310 Jun 5 20:14 hosts
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy01.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy02.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy03.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy04.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy05.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy06.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy07.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy08.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy09.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy10.txt
總結:inotify優缺點:
inotify優勢:監控文件系統事件變化,經過同步工具實現實時數據同步。
inotify缺點:
1)併發若是大於200個文件(10-100K),同步就會有延遲。
2)咱們前面寫的腳本,每次都是所有推送一次,但確實是增量的。
3)監控到事件後,調用rsync同步是單進程的,sersync多進程同步。既然有了inoitfy-tools,爲何還要開發sersync?
sersync功能多:(inotify+rsync命令)
1)支持經過配置文件管理
2)真正的守護進程socket
3)能夠對失敗文件定時重傳(定時任務功能)
4)第三方的HTTP接口(例如:更新cdn緩存)
5)默認多線程rsync同步
建議:
(1)當同步的目錄數據量不大時,建議使用rsync+inotify
(2)當同步的目錄數據量很大時(幾百G甚至1T以上)文件不少時,建議使用rsync+sersync
安裝sersync軟件
[root@nfs01 /]# mkdir -p /home/oldboy/tools
[root@nfs01 /]# cd /home/oldboy/tools
[root@nfs01 tools]# rpm -qa lrzsz
lrzsz-0.12.20-27.1.el6.x86_64
[root@nfs01 tools]# rz -E 上傳文件sersync_installdir_64bit.zip
rz waiting to receive.
[root@nfs01 tools]# ll
total 692
-rw-r--r-- 1 root root 708025 Jun 6 22:20 sersync_installdir_64bit.zip
[root@nfs01 tools]# unzip sersync_installdir_64bit.zip
Archive: sersync_installdir_64bit.zip
creating: sersync_installdir_64bit/
creating: sersync_installdir_64bit/sersync/
creating: sersync_installdir_64bit/sersync/bin/
inflating: sersync_installdir_64bit/sersync/bin/sersync
creating: sersync_installdir_64bit/sersync/conf/
inflating: sersync_installdir_64bit/sersync/conf/confxml.xml
creating: sersync_installdir_64bit/sersync/logs/
[root@nfs01 tools]# mv sersync_installdir_64bit/sersync/ /usr/local/
[root@nfs01 tools]# ll /usr/local/sersync/
total 12
drwxr-xr-x 2 root root 4096 Dec 23 2012 bin
drwxr-xr-x 2 root root 4096 Dec 23 2012 conf
drwxr-xr-x 2 root root 4096 Dec 23 2012 logs
[root@nfs01 tools]# ll /usr/local/sersync/bin/sersync
-rw-r--r-- 1 root root 1810128 Oct 26 2011 /usr/local/sersync/bin/sersync
[root@nfs01 tools]# chmod +x /usr/local/sersync/bin/sersync
[root@nfs01 tools]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 188 bytes received 27 bytes 143.33 bytes/sec
total size is 310 speedup is 1.44
[root@nfs01 tools]# ln -s /usr/local/sersync/bin/sersync /usr/local/bin/ 建立軟鏈接,之後能夠直接使用sersync命令
[root@nfs01 tools]# sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
參數-d:啓用守護進程模式
參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
c參數-n: 指定開啓守護線程的數量,默認爲10個
參數-o:指定配置文件,默認使用confxml.xml文件
參數-m:單獨啓用其餘模塊,使用 -m refreshCDN 開啓刷新CDN模塊
參數-m:單獨啓用其餘模塊,使用 -m socket 開啓socket模塊
參數-m:單獨啓用其餘模塊,使用 -m http 開啓http模塊
不加-m參數,則默認執行同步程序
[root@nfs01 conf]# cd /usr/local/sersync/conf
[root@nfs01 conf]# cp confxml.xml confxml.xml.ori
[root@nfs01 conf]# vim confxml.xml
:set nu 顯示行號
[root@nfs01 conf]# diff confxml.xml confxml.xml.ori 比較修改後和修改前配置文件的不一樣
24,25c24,25 要改配置文件的行號
< <localpath watch="/data">
< <remote ip="172.16.1.41" name="nfsbackup"/>
---
> <localpath watch="/opt/tongbu">
> <remote ip="127.0.0.1" name="tongbu1"/>
30,31c30,31 要改配置文件的行號
< <commonParams params="-artuz --delete"/>
< <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
---
> <commonParams params="-artuz"/>
> <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
36c36
< <failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once-->
---
> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
[root@nfs01 conf]# cat 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="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="nfsbackup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz --delete"/>
<auth start="true" users="rsynz_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/var/log/rsync_fail.log" 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>
[root@nfs01 conf]# sersync -dro /usr/local/sersync/conf/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsynz_backup
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz --delete -R --delete ./ rsynz_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data 表示成功運行
測試nfs01 /data目錄下增刪文件,是否同步到backup /nfsbackup目錄