項目實戰:rsync+sersync實現數據實時同步

一、組網介紹

本次實驗使用兩臺主機:
qll251 角色:Rsync server + Sersync server
qll252 角色: Rsync client

本次實驗採用CentOS7.7系統;同時禁用防火牆,關閉selinux

[root@qll251 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@qll251 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@qll251 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@qll251 ~]# reboot #重啓使selinux配置生效
[root@qll251 ~]# getenforce
Disabled
###以上便是本次實驗環境的準備工作

二、開始部署

  • 在備份源機器上部署sersync+rsync服務

1) 下載sersync的可執行文件

[root@qll251 ~]# wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

qinlulu
2)解壓sersync可執行文件

[root@qll251 ~]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@qll251 ~]# ls
GNU-Linux-x86  sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@qll251 ~]# mv GNU-Linux-x86/ sersync2.5.4 #文件夾重命名
[root@qll251 ~]# ls
sersync2.5.4  sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@qll251 ~]#

3)修改/root/sersync2.5.4/confxml.xml配置文件

[root@qll251 ~]# cd /root/sersync2.5.4/
[root@qll251 sersync2.5.4]# ls
confxml.xml  sersync2
[root@qll251 sersync2.5.4]# cp confxml.xml confxml.xml.bak #大家養成一個好習慣,修改配置文件,第一件事情是:對它進行備份!
[root@qll251 sersync2.5.4]# vim confxml.xml
#####################下面是配置文件
 23     <sersync>
 24         <localpath watch="/data"> #本地備份源路徑
 25             <remote ip="192.168.1.252" name="web"/>   #對端IP地址;web爲對端模塊名稱
 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="rsync_user" passwordfile="/etc/rsync.mypass"/> #開啓認證模式,對端認證用戶,及指定本地密碼文本
 ###################

下圖黃色箭頭所指的代表本次實驗需要修改的配置
在這裏插入圖片描述
4)創建本地備份源目錄及rsync密碼文本/etc/rsync.mypass,並設置相應權限

[root@qll251 ~]# mkdir /data
[root@qll251 ~]# echo 123123 > /etc/rsync.mypass
[root@qll251 ~]# chmod 600 /etc/rsync.mypass
[root@qll251 ~]#

5)安裝並啓動rsync服務

[root@qll251 ~]# yum -y install rsync xinetd
[root@qll251 ~]# systemctl start rsyncd && systemctl enable rsyncd
[root@qll251 ~]# systemctl start xinetd && systemctl enable xinetd
[root@qll251 ~]# rsync --daemon
  • 在qll252主機上部署rsync服務

1)客戶端安裝rsync服務

[root@qll252 ~]# yum -y install rsync

2)修改/etc/rsyncd.conf配置文件

## vim /etc/rsyncd.conf          //若文件不存在,需要自己創建
uid = root        #運行進程的身份
gid = root       #運行進程的組
address =192.168.1.252   #監聽IP
port =873          #監聽端口
hosts allow =192.168.1.0/24   #允許客戶端的IP地址,可以是網段,或者用*表示所有 
use chroot = yes     #若rsync被黑客入侵,則鎖定家目錄,黑客無法再rsync運行的家目錄之外創建文件
max connections =5        #最大連接數
pid file =/var/run/rsyncd.pid       #進程PID,自動生成
lock file =/var/run/rsync.lock      #指max connectios參數的鎖文件
log file =/var/log/rsyncd.log       #日誌文件位置
motd file =/etc/rsyncd.motd #客戶端登陸之後彈出的消息,需要創建
[web]             #共享模塊名稱,sersync端需要跟它保持一致
path =/mybak   #接收備份源端文件的路徑
comment = used for mysql-data    #描述
read only = false     #設置服務端文件讀寫權限
list = yes            #是否允許查看模塊信息
auth users = rsync_user   #指定備份用戶,sersync端需要跟它保持一致
secrets file =/etc/rsync.passwd     #本地存放用戶的密碼文件,格式是  用戶名:密碼

3)創建待接收備份文件的目錄;創建密碼文件/etc/rsync.passwd,並指定權限600

[root@qll252 ~]# mkdir /mybak
[root@qll252 ~]# echo "rsync_user:123123" > /etc/rsync.passwd  #格式是  用戶名:密碼
[root@qll252 ~]# chmod 600 /etc/rsync.passwd

4)以守護進程方式運行rsync;同時確保開機自動運行該守護進程

[root@qll252 ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@qll252 ~]# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local

三、開啓sersync守護進程進行同步數據

我們先手動同步一次,

[root@qll251 ~]# rsync -avz /data [email protected]::web --password-file=/etc/rsync.mypass

在這裏插入圖片描述
手動同步成功,說明我們部署的環境是正確的!

接下來,我們開啓sersync守護進程,實現兩臺服務器之間的數據實時同步

[root@qll251 ~]# /root/sersync2.5.4/sersync2 -d -r -o /root/sersync2.5.4/confxml.xml

在這裏插入圖片描述
已開啓sersync守護進程,現在我們進行驗證

驗證方式:在服務器端批量創建文件,同時在客戶端watch -n /mybak。以0.1秒的時間間隔進行刷新
驗證結果:數據實時同步,如下圖所示:
在這裏插入圖片描述

我們已經實現了,兩臺主機之間的數據實時同步。接下來我們研究下sersync開機自動監控數據同步

實現sersync開機自動監控數據同步

編輯/etc/rc.local
(最後一行添加追加該命令)

/root/sersync2.5.4/sersync2 -d -r -o /root/sersync2.5.4/confxml.xml 

編輯完畢,reboot,如果一切正常。便可實現數據的實時同步

若重啓主機後,/etc/rc.local內的配置命令並未生效。
這時要確保/etc/rc.local和/etc/rc.d/rc.local都有可執行權限
原因如下:

在這裏插入圖片描述
上圖中,我用黃線圈了兩處:
第一句說明:/etc/rc.local/etc/rc.d/rc.local的軟鏈接
第二句:Please note that you must run ‘chmod +x /etc/rc.d/rc.local’ to ensure

通過以上得到結論:
由於/etc/rc.local是/etc/rc.d/rc.local的軟鏈接,所以必須確保/etc/rc.local 和 /etc/rc.d/rc.local都有可執行權限。

解決辦法:chmod +x /etc/rc.d/rc.local

重啓,問題解決。

關於/etc/rc.local的小技巧,你今天get到了嗎?

更多IT技術,請微信搜索公衆號秦露露或者掃描下方二維碼關注

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章