sersync 介紹
sersync類似于inotify,同樣用于監控,但它克服了inotify的缺點.
inotify最大的不足是會產生重復事件,或者同一個目錄下多個檔案的操作會產生多個事件,例如,當監
控目錄中有5個檔案時,洗掉目錄時會產生6個監控事件,從而導致重復呼叫rsync命令,另外比如:vim
檔案時,inotify會監控到臨時檔案的事件,但這些事件相對于rsync來說是不應該被監控的
sersync 優點:
sersync是使用c++撰寫,而且對linux系統檔案系統產生的臨時檔案和重復的檔案操作進行過濾,
所以在結合rsync同步的時候,節省了運行時耗和網路資源,因此更快,
sersync配置很簡單,其中提供了靜態編譯好的二進制檔案和xml組態檔,直接使用即可
sersync使用多執行緒進行同步,尤其在同步較大檔案時,能夠保證多個服務器實時保持同步狀態
sersync有出錯處理機制,通過失敗佇列對出錯的檔案重新同步,如果仍舊失敗,則按設定時長對
同步失敗的檔案重新同步
sersync不僅可以實作實時同步,另外還自帶crontab功能,只需在xml組態檔中開啟,即也可以
按要求隔一段時間整體同步一次,而無需再額外配置crontab功能
sersync 可以二次開發
sersync專案地址: https://code.google.com/archive/p/sersync/
sersync下載地址:https://code.google.com/archive/p/sersync/downloads
案例:基于rsync daemon 實作 sersync
#在資料服務器上下載sersync,并拷貝至相應的目錄,設定PATH變數
[root@sz-kx-centos8 ~]# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
# 解壓
[root@sz-kx-centos8 ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sz-kx-centos8 ~]# cp -a GNU-Linux-x86 /usr/local/sersync
[root@sz-kx-centos8 ~]# echo 'PATH=/usr/local/sersync:$PATH' > /etc/profile.d/sersync.sh
[root@sz-kx-centos8 ~]# source /etc/profile.d/sersync.sh
[root@sz-kx-centos8 ~]# ll /usr/local/sersync/
total 1772
-rwxr-xr-x 1 root root 2214 Oct 26 2011 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 sersync2
#確認安裝rsync客戶端工具
[root@sz-kx-centos8 ~]# rpm -q rsync &> /dev/null || dnf -y install rsync
#備份sersync組態檔
[root@sz-kx-centos8 ~]# cp /usr/local/sersync/confxml.xml{,.bak}
#修改sersync組態檔
[20:47:13 root@sz-kx-centos8 ~]# cat /usr/local/sersync/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="true"/> #修改此行為true,檔案屬性變化后也會同步
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www"> #修改此行,需要同步的源目錄或檔案,建議同步目錄
<remote ip="備份服務器IP" name="backup"/> #修改此行,指定備份服務器地址和rsync daemon的模塊名,如果下面開啟了ssh start,此時name為遠程shell方式運行時的目標目錄
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/> #修
改此行為true,指定備份服務器的rsync配置的用戶和密碼檔案
<userDefinedPort start="false" port="874"/><!-- 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>
#創建連接rsynd服務器的用戶密碼檔案,并必須修改權限
[root@sz-kx-centos8 ~]# echo centos > /etc/rsync.pas
[root@sz-kx-centos8 ~]# chmod 600 /etc/rsync.pas
#查看幫助
[root@sz-kx-centos8 ~]# sersync2 -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@sz-kx-centos8 ~]# sersync2 -dro /usr/local/sersync/confxml.xml
#如果同步失敗,可以手動執行下面命令,觀察程序
[root@sz-kx-centos8 ~]# cd /data/www && rsync -artuz -R --delete ./ rsyncuser@backup-server::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /data/www
#sersync支持多實體,也即監控多個目錄時,只需分別配置不同組態檔,然后使用sersync2指定對應配置
檔案運行
[root@sz-kx-centos8 ~]# sersync2 -rd -o /etc/sersync.d/nginx.xml
基于遠程shell 實作 sersync
#不需要配置rsync daemon,只需要配置基于key驗證的ssh即可
[root@sz-kx-centos8 ~]# ssh-keygen
[root@sz-kx-centos8 ~]# ssh-copy-id backup-server
#下載sersync,并拷貝至相應的目錄,設定PATH變數同5.5.2
#修改sersync組態檔
[root@sz-kx-centos8 ~]# cat /usr/local/sersync/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="true"/> #修改此行為true
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www"> #修改此行,指定源資料目錄
<remote ip="備份服務器IP" name="/data/backup"/> #修改此行指定備份服務器地址和備份目標目錄
<!--<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/rsync.pas"/> #必須修改 此行,不啟用認證start=false
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="true"/> #修改此行為true,使用遠程shell方式的rsync連接方式,無需在目標
主機上配置啟動rsync daemon服務
#####################################以下行不需要修改
####################################
</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>
#將中間的行可以洗掉
</head>
[root@sz-kx-centos8 ~]# sersync2 -dro /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
案例 基于LAMP搭建web服務資料同步

# 安裝mysql做主從復制
[root@centos8 ~]# yum install mysql-server -y
[root@centos8 ~]# yum install mysql-server -y
開機啟動mysql
[root@centos8 ~]# systemctl enable --now mysqld
[root@centos8 ~]# systemctl enable --now mysqld
配置主從
#主配置
[root@centos8 ~]# vim /etc/my.cnf
[mysqld]
server-id=28
log-bin=/data/mysql/mysql-bin
#創建目錄并授權
[root@centos8 ~]# mkdir /data/mysql/ -p
[root@centos8 ~]# chown -R mysql.mysql /data/mysql
# 重新啟動mysql服務
[root@centos8 ~]# systemctl restart mysqld
#從配置
[mysqld]
server_id=38
log-bin=/data/mysql/mysql-bin
read_only
#創建目錄并授權
[root@centos8 ~]# mkdir /data/mysql/ -p
[root@centos8 ~]# chown -R mysql.mysql /data/mysql/
# 重新啟動mysql服務
[root@centos8 ~]# systemctl restart mysqld
主進入資料庫創建庫并創建用戶還有授權
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> use db1;
Database changed
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000002 | 156 | No |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
# 創建主從同步用戶
mysql> create user repluser@'172.31.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to repluser@'172.31.0.%';
Query OK, 0 rows affected (0.00 sec)
從配置
使用幫助
mysql> help change master to;
mysql> CHANGE MASTER TO
MASTER_HOST='172.31.0.28',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=156;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.31.0.28
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1064
Relay_Log_File: centos8-relay-bin.000002
Relay_Log_Pos: 1232
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# 創建WordPress庫
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
# 創建WordPress用戶
mysql> create user wordpress@'172.31.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
# 授權
mysql> grant all on wordpress.* to wordpress@'172.31.0.%';
Query OK, 0 rows affected (0.00 sec)
創建目錄NFS做資料同步
[root@sz-kx-centos8 ~]# mkdir /data/wordpress -p
[root@sz-kx-centos8 ~]# chmod 777 /data/wordpress/
[root@sz-kx-centos8 ~]# vim /etc/exports
/data/wordpress 172.31.0.0/16(rw)
# 安裝NFS
[root@centos8 ~]# yum install -y nfs-utils
[root@centos8 ~]# yum install -y nfs-utils
# 開機啟動
[root@sz-kx-centos8 ~]# systemctl enable --now nfs-server.service
[root@sz-kx-centos8 ~]# systemctl enable --now nfs-server.service
# 重讀配置
[root@sz-kx-centos8 ~]# exportfs -r
# 查看本機所有NFS共享
[root@sz-kx-centos8 ~]# exportfs -v
/data/wordpress
172.31.0.0/16(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
##安裝web
[root@CentOS8 ~]# yum install httpd php-fpm php-json php-mysqlnd nfs-utils -y
[root@CentOS8 ~]# yum install httpd php-fpm php-json php-mysqlnd nfs-utils -y
# 開機啟動
[root@localhost ~]# systemctl enable --now httpd php-fpm
[root@localhost ~]# systemctl enable --now httpd php-fpm
# 掛載
[root@localhost ~]# vim /etc/fstab
172.31.0.18:/data/wordpress/wp-content/uploads /var/www/html/wp-content/uploads nfs _netdev 0 0
[root@localhost ~]# vim /etc/fstab
172.31.0.18:/data/wordpress/wp-content/uploads /var/www/html/wp-content/uploads nfs _netdev 0 0
# 創建目錄
[root@localhost ~]# mkdir /var/www/html/wp-content/uploads -p
[root@localhost ~]# mkdir /var/www/html/wp-content/uploads -p
[root@localhost ~]# mount -a
[root@localhost ~]# mount -a
# 查看能不能看遠程主機的共享
[root@localhost ~]# showmount -e 172.31.0.18
Export list for 172.31.0.18:
/data/wordpress 172.31.0.0/16
# 授權改所屬用戶(在掛載的nfs主機改)
[root@sz-kx-centos8 /data/wordpress]# chown -R apache.apache /data/wordpress/*
資料同步(需要手動執行同步資料,很是不方便,所以使用以下的方法解決手動執行同步資料問題)
# master機安裝軟體包
[root@sz-kx-centos8 ~]# yum -y install inotify-tools
# back機安裝
[root@localhost ~]# yum install -y rsync rsync-daemon
# back機操作
[root@localhost ~]# rsync --daemon
[root@localhost ~]# ss -tanlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:873 0.0.0.0:*
# back機操作
[root@localhost ~]# vim /etc/rsyncd.conf
[backup]
path = /data/wordpress/
read only = no
# 測驗master機操作
[root@sz-kx-centos8 ~]# rsync rsync://172.31.0.58
backup
# 在master機器手動執行同步
[root@sz-kx-centos8 ~]# echo "centos" > /etc/rsync.pas
[root@sz-kx-centos8 ~]# chmod 600 /etc/rsync.pas
先把資料手工推送到back機器
[root@sz-kx-centos8 ~]# rsync /data/wordpress/ rsync://172.31.0.58::backup
[root@sz-kx-centos8 ~]# rsync -avz --delete --password-file=/etc/rsync.pas /data/wordpress rsyncuser@172.31.0.18::backup
推薦使用這個方式同步資料
sersync 實作實時資料同步
[root@sz-kx-centos8 ~]# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
改組態檔按照上面的改即可
# 以后臺方式執行同步
[root@sz-kx-centos8 ~]# sersync2 -dro /usr/local/sersync/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/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 rsyncuser
passwordfile is /etc/rsync.pas
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/wordpress && rsync -artuz -R --delete ./ rsyncuser@172.31.0.58::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /data/wordpress
# 如果同步失敗,可以手動執行下面命令,觀察程序
[root@sz-kx-centos8 ~]# cd /data/wordpress && rsync -artuz -R --delete ./ rsyncuser@172.31.0.58::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
瀏覽器同步資料(也可以改成域名決議ip訪問)


遇到報錯
[root@sz-kx-centos8 ~]# rsync rsync://172.31.0.58
rsync: failed to connect to 172.31.0.58 (172.31.0.58): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(127) [Receiver=3.1.3]
解決方法:使用[root@sz-kx-centos8 ~]# telnet 172.31.0.58 873
看通不通,如果不通可能是防火墻沒有關閉,關閉即可重新同步
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/286239.html
標籤:其他
