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/ backup@192.168.6.200::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 ./ backup@192.168.6.200::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在遍歷查找對比文件時,速度很快),因此效率很高。