1. 程式人生 > 實用技巧 >CentOS8.2 配置部署Rsync+sersync和rsync+inotify檔案實時同步

CentOS8.2 配置部署Rsync+sersync和rsync+inotify檔案實時同步

目錄

CentOS8.2 配置rsync+sersync檔案實時同步

Rsyncd 伺服器(備份端) 資料來源端測試rsync同步
主機名 rsyncserver.lianglab.cn sersyncserver.lianglab.cn
IP資訊 192.168.6.200 192.168.6.65

第一步:Rsyncd 伺服器(備份端)配置

192.168.6.200----rsyncd Rsync伺服器(備份端)
hostnamectl set-hostname rsyncserver.lianglab.cn
檢視rsync是否安裝 
[root@centos8 ~]# rpm -qa | grep rsync
rsync-3.1.3-7.el8.x86_64
[root@centos8 ~]# 

【1】安裝rsync

[root@rsyncserver ~]# dnf -y install xinetd rsync
Last metadata expiration check: 1:22:21 ago on Mon 21 Dec 2020 09:09:48 PM CST.
Package rsync-3.1.3-7.el8.x86_64 is already installed.
Dependencies resolved.
====================================================================================================================
 Package                 Architecture            Version                           Repository                  Size
====================================================================================================================
Installing:
 xinetd                  x86_64                  2:2.3.15-24.el8                   AppStream                  135 k

Transaction Summary
====================================================================================================================
Install  1 Package

Total download size: 135 k
Installed size: 379 k
Downloading Packages:
xinetd-2.3.15-24.el8.x86_64.rpm                                                     7.5 MB/s | 135 kB     00:00    
--------------------------------------------------------------------------------------------------------------------
Total                                                                               6.9 MB/s | 135 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                            1/1 
  Installing       : xinetd-2:2.3.15-24.el8.x86_64                                                              1/1 
  Running scriptlet: xinetd-2:2.3.15-24.el8.x86_64                                                              1/1 
  Verifying        : xinetd-2:2.3.15-24.el8.x86_64                                                              1/1 
Installed products updated.

Installed:
  xinetd-2:2.3.15-24.el8.x86_64                                                                                     

Complete!
[root@rsyncserver ~]# 

[root@rsyncserver ~]# rsync --daemon
[root@rsyncserver ~]# netstat -antup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      51170/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      51170/rsync         
[root@rsyncserver ~]# 

【2】建立rsync配置檔案

[root@rsyncserver ~]# cat /etc/rsyncd.conf 

#Rsync server
uid = root                         #執行程序的身份
gid = root                         #執行程序的組
use chroot = yes                   #是否鎖定家目錄
max connections = 100              #最大連線數
timeout = 600                      #超時時間
log file = /var/log/rsyncd.log     #日誌檔案
ignore errors                      #忽略錯誤
read only = false                  #設定服務端檔案讀寫許可權
list = false                       #不顯示服務端資源列表
hosts allow = 192.168.6.0/24       #*代表所有
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/rsync.password

[data]
comment = data
path = /data/www


【3】啟動rsync和xinetd服務

[root@rsyncserver ~]# systemctl start xinetd 
[root@rsyncserver ~]# systemctl enable xinetd
[root@rsyncserver ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@rsyncserver ~]# netstat -antup | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      51170/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      51170/rsync         
[root@rsyncserver ~]# ps aux | grep rsync
root       51170  0.0  0.0  11508   192 ?        Ss   22:32   0:00 rsync --daemon
root       51322  0.0  0.0  12108  1048 pts/1    S+   22:34   0:00 grep --color=auto rsync
[root@rsyncserver ~]# 

【4】配置rsync開機自啟動

方法一:
echo "rsync --daemon --config=/etc/rsyncd.conf"  >> /etc/rc.d/rc.local

方法二:

【5】建立同步目錄

[root@rsyncserver ~]# mkdir -p /data/www

【6】建立密碼檔案

[root@rsyncserver ~]# vi  /etc/rsync.password
[root@rsyncserver ~]# 
[root@rsyncserver ~]# cat /etc/rsync.password
backup:lianglab123
[root@rsyncserver ~]# chmod 600 /etc/rsync.password

第二步:資料來源端測試rsync同步

【1】新建一個檔案儲存好密碼,然後在rsync命令中使用–password-file指定此檔案即可

[root@sersyncserver ~]# hostnamectl set-hostname sersyncserver.lianglab.cn

[root@sersyncserver ~]# echo "lianglab123" > /etc/rsync.password

[root@sersyncserver ~]# chmod 600 /etc/rsync.password 

【2】建立測試檔案

[root@sersyncserver ~]# mkdir -p /data/www
[root@sersyncserver ~]# cp /etc/passwd /data/www/
[root@sersyncserver www]# md5sum /data/www/passwd 
b42f457347877832fc6da97e6cc3481e  /data/www/passwd
[root@sersyncserver ~]# rsync -avzP  /data/www/ [email protected]::data  --password-file=/etc/rsync.password
sending incremental file list
./

sent 74 bytes  received 27 bytes  67.33 bytes/sec
total size is 2,923  speedup is 28.94
[root@sersyncserver ~]# 

【3】登陸到rsyncserver 檢視檔案是否同步

[root@rsyncserver www]# cd /data/www/
[root@rsyncserver www]# ls
passwd
[root@rsyncserver www]# pwd
/data/www
[root@rsyncserver www]# md5sum passwd 
b42f457347877832fc6da97e6cc3481e  passwd
[root@rsyncserver www]# 

第三步:資料來源端開始部署sersync服務

[1] 在google code下載sersync軟體包,裡面有配置檔案與可執行檔案
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@sersyncserver tmp]# wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sersyncserver tmp]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@sersyncserver tmp]# mv GNU-Linux-x86 sersync
[root@sersyncserver tmp]# mv /tmp/sersync /opt
[root@sersyncserver opt]# tree /opt/sersync/
/opt/sersync/
├── confxml.xml  #sersync軟體配置檔案
├── confxml.xml.2020-12-21
└── sersync2    #sersync軟體命令

0 directories, 3 files
[root@sersyncserver opt]# 

[2] 配置sersync

[root@sersyncserver opt]# cp sersync/confxml.xml sersync/confxml.xml.$(date +%F)
[root@sersyncserver opt]# cd sersync/
[root@sersyncserver sersync]# ls
confxml.xml  confxml.xml.2020-12-21  sersync2
[root@sersyncserver sersync]# 

[3]修改sersync配置檔案24-28行

修改前的內容   
    23	    <sersync>
    24		<localpath watch="/opt/tongbu">
    25		    <remote ip="127.0.0.1" name="tongbu1"/>
    26		    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    27		    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    28		</localpath>
    29		<rsync>
    30		    <commonParams params="-artuz"/>
    31		    <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
    32		    <userDefinedPort start="false" port="874"/><!-- port=874 -->
    33		    <timeout start="false" time="100"/><!-- timeout=100 -->
    34		    <ssh start="false"/>

[4].修改31–34行,認證部分【rsync密碼認證】

修改後的內容
    23	    <sersync>
    24		<localpath watch="/data/www">                           #本地同步目錄
    25		    <remote ip="192.168.6.200" name="data"/>            #指定備份伺服器模組資訊
    26		    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    27		    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    28		</localpath>
    29		<rsync>
    30		    <commonParams params="-artuz"/>
    31		    <auth start="true" users="backup" passwordfile="/etc/rsync.password"/>    #開啟使用者認證,並定義認證使用者和認證密碼檔案
    32		    <userDefinedPort start="false" port="874"/><!-- port=874 --> #備份伺服器的埠
    33		    <timeout start="false" time="100"/><!-- timeout=100 -->  #定義超時時間
    34		    <ssh start="false"/>
    35		</rsync>

[5].開啟sersync守護程序同步資料

[root@sersyncserver sersync]# /opt/sersync/sersync2 -d -r -o /opt/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:  /opt/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	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/www && rsync -artuz -R --delete ./ [email protected]::data --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data/www
[root@sersyncserver sersync]# 


CentOS8.2 配置rsync+inotify檔案實時同步


CentOS8.2 配置rsync+inotify實現檔案實時同步需求描述:線上有一套財務系統在伺服器A,已經完成前期的開發,
伺服器B用於推送管理層報表到管理層釘釘群。目前沒有做到自動化,每次報表生產後(圖片格式)伺服器A,
每次需要管理員登入的伺服器A把檔案傳送動伺服器B指定目錄,伺服器B才能推送管理層報表到管理層釘釘群

環境:

192.168.6.198 服務端(用於備份資料,資料提供給領導檢視)
192.168.6.199 客戶端(產生新的檔案,新檔案同步到服務端)

Rsyncd 伺服器 客戶端
主機名 nginx01.lianglab.cn nginx02.lianglab.cn
IP資訊 192.168.6.198 192.168.6.199

1、安裝以下安裝包lrzsz是xshell上傳下載的安裝包,如果通過ftp上傳軟體到機器可以忽略。

dnf install -y lrzsz gcc wget gcc-c++ vim

2、關閉firewall防火牆,開機禁止啟動。線上機器預設關閉,不需要配置。

第一步:開始配置服務端


【1】安裝rsync

dnf install -y rsync

【2】更改配置檔案
vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode
#工作中指定使用者(可以不指定為0)
uid = 0
gid = 0
#相當於黑洞.出錯定位
use chroot = no
##有多少個客戶端同時傳檔案
max connections =200
##超時時間
timeout = 300
##程序號檔案
pid ifle = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
##日誌檔案
log file = /var/log/rsyncd.log
##模組開始(這個模組就是待會兒對端寫指令碼的裡面一個引數的名稱)
[msone]
##需要同步的目錄(準確的說是同步過來後放到哪裡的目錄路徑)
path = /data/www
##表示出現錯誤忽略錯誤
ignore errors
##表示網路許可權可寫(本地控制真正可寫)(親測這裡寫false報錯)
read only = no
##這裡設定IP或讓不讓同步
list = false
#允許的ip或者ip段
hosts allow = 192.168.6.199
##拒絕,以下表示都不拒絕
#hosts deny = 0.0.0.0/32
##認證使用者
auth users = rsyncli
##使用者名稱和密碼存放檔案
secrets file = /etc/rsync.secrets

【3】建立領導要檢視檔案目錄,需要www這個目錄其它使用者可以寫的許可權

mkdir -p /data/www(這個是測試目錄,你的系統上面應該已經有這個目錄了,根據你生產需要修改這個目錄)
chmod 765 /data/www

【4】新增同步需要用的使用者名稱和密碼

#useradd -s /sbin/nologin rsyncli
#passwd rsyncli
密碼設定liang123

【5】建立認證檔案

vi /etc/rsync.secrets
檔案裡面新增如下內容(格式為使用者名稱:密碼):
rsyncli:liang123

【6】為了安全起見給這個檔案設定600許可權。

[root@nginx01 ~]# ll /etc/rsync.secrets
-rw-r--r--. 1 root root 17 Aug 10 11:54 /etc/rsync.secrets
[root@nginx01 ~]# chmod 600 /etc/rsync.secrets
[root@nginx01 ~]# ll /etc/rsync.secrets
-rw-------. 1 root root 17 Aug 10 11:54 /etc/rsync.secrets
[root@nginx01 ~]#

【7】啟動服務

[root@nginx01 ~]# systemctl start rsyncd

[root@nginx01 ~]# netstat -anplt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 13127/rsync
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 876/sshd
tcp 0 0 192.168.6.198:29550 192.168.6.199:9100 ESTABLISHED 29780/./prometheus
tcp 0 48 192.168.6.198:22 10.57.237.13:53860 ESTABLISHED 12322/sshd: tdops [
tcp 0 0 192.168.6.198:22 10.57.237.13:53862 ESTABLISHED 12326/sshd: tdops [
tcp 0 0 127.0.0.1:57758 127.0.0.1:9090 ESTABLISHED 29780/./prometheus
tcp6 0 0 :::9090 :::* LISTEN 29780/./prometheus
tcp6 0 0 :::873 :::* LISTEN 13127/rsync
tcp6 0 0 :::22 :::* LISTEN 876/sshd
tcp6 0 0 ::1:9090 ::1:4852 ESTABLISHED 29780/./prometheus
tcp6 0 0 127.0.0.1:9090 127.0.0.1:57758 ESTABLISHED 29780/./prometheus
tcp6 0 0 ::1:4852 ::1:9090 ESTABLISHED 29780/./prometheus
[root@nginx01 ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2020-08-10 11:56:00 CST; 20s ago
Main PID: 13127 (rsync)
CGroup: /system.slice/rsyncd.service
└─13127 /usr/bin/rsync --daemon --no-detach

Aug 10 11:56:00 nginx01 systemd[1]: Started fast remote file copy program daemon.
Aug 10 11:56:00 nginx01 rsyncd[13127]: Unknown Parameter encountered: "pid ifle"
Aug 10 11:56:00 nginx01 rsyncd[13127]: IGNORING unknown parameter "pid ifle"
Aug 10 11:56:00 nginx01 rsyncd[13127]: rsyncd version 3.1.2 starting, listening on port 873
[root@nginx01 ~]#

第二步:開始配置客戶端


【1】檢視當前系統是否支援inotify

[root@nginx02 tdops]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Aug 10 13:42 max_queued_events
-rw-r--r--. 1 root root 0 Aug 10 13:42 max_user_instances
-rw-r--r--. 1 root root 0 Aug 10 13:42 max_user_watches
[root@nginx02 tdops]#

【2】安裝rsync和inotify(rsync只安裝不啟動不配置)

[root@nginx02 tdops]# dnf install -y rsync

【3】inotify是一種強大的、細粒度的、非同步的檔案系統事件控制機制。
linux核心從2.6.13起,加入了inotify支援,通過inotify可以監控檔案系統中新增、刪除、修改、移動等各種事件,
利用這個核心介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools正是實施監控的軟體。

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/include/
make && make install

【4】最重要的是要寫這個指令碼,讓它自動去檢測我們制定的目錄下檔案發生的變化,
然後再執行rsync的命令把它同步到我們的伺服器端去。

touch inotify.sh
chmod 755 inotify.sh
vi inotify.sh

#!/bin/bash
#chkconfig: 345 88 14
host=192.168.6.198 #server的ip(備份伺服器)
src=/data/www/ #所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=msone #自定義的模組名,需要與client端定義的一致
password=/etc/rsync.password #密碼檔案
user=rsyncli #使用者名稱(一個實際存在的賬號)
inotify=/usr/local/include/ #inotify的安裝目錄

${inotify}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/var/log/rsync.log 2>&1
done

【5】建立/etc/rsync.password檔案(整個檔案需要600的許可權和服務端rsync同步使用者的密碼,這裡是伺服器端rsyncli使用者的密碼)

vi /etc/rsync.password
liang123
chmod 600 /etc/rsync.password

【6】設定核心引數提高資料同步效能。

[root@nginx02 opt]# sysctl -w fs.inotify.max_queued_events="200000000"
fs.inotify.max_queued_events = 200000000
[root@nginx02 opt]# sysctl -w fs.inotify.max_user_watches="2000000000"
fs.inotify.max_user_watches = 2000000000
[root@nginx02 opt]# sysctl -w fs.inotify.max_user_instances="65535"
fs.inotify.max_user_instances = 65535

【7】啟動服務指令碼

chmod +x /opt/inotify.sh
[root@nginx02 opt]# nohup /opt/inotify.sh &> /dev/null & #放到後臺執行
[1] 16011
[1]+ Done nohup /opt/inotify.sh &>/dev/null
[root@nginx02 opt]#


Rsync+inotify-tools與rsync+sersync架構的區別


1、rsync+inotify-tools
a、inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並沒有把具體是哪個檔案或者哪個目錄發生了變化記錄下來;
b、rsync在同步的時候,並不知道具體是哪個檔案或目錄發生了變化,每次都是對整個目錄進行同步,當資料量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查詢對比檔案),因此效率很低

2、rsync+sersync
a、sersync可以記錄被監聽目錄中發生變化的(增,刪,改)具體某個檔案或目錄的名字;
b、rsync在同步時,只同步發生變化的檔案或目錄(每次發生變化的資料相對整個同步目錄資料來說很小,rsync在遍歷查詢對比檔案時,速度很快),因此效率很高。