Linxu服务器文件双向同步 rsync+sersync 详细讲解
文章目錄
- Linxu服務器文件雙向同步 rsync+sersync
- 基礎信息
- 安裝
- rsync
- 安裝
- 配置
- 根據配置文件創建相應的目錄、文件、防火墻規則
- 創建需要同步的目錄
- 創建同步用戶
- 防火墻
- 啟動rsyncd服務
- 測試
- 異常處理
- sersync
- 安裝
- sersync配置
- test21配置
- test22配置
- sersync啟動
- test21啟動
- test22啟動
- 測試
- 新增文件
- 修改內容
- 刪除文件
Linxu服務器文件雙向同步 rsync+sersync
基礎信息
系統:Centos7.8
- 服務器信息
| test21 | 10.8.10.21 |
| test22 | 10.8.10.22 |
- 軟件版本信息
| rsync | |
| sersync |
Rsync安裝包下載地址:https://github.com/WayneD/rsync/releases
sersync下載地址:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
安裝
rsync
安裝
查看是否已經安裝
rpm -qa | grep rsync安裝
yum -y install rsync配置
- 修改test21服務器文件/etc/rsyncd.conf
- 修改test22服務器文件/etc/rsyncd.conf
根據配置文件創建相應的目錄、文件、防火墻規則
創建需要同步的目錄
在兩個服務器上分別創建需要同步的目錄
[root@test21 ~]# mkdir /root/updatedir [root@test22 ~]# mkdir /root/updatedir創建同步用戶
創建虛擬用戶rsync_daemon使用的密碼文件/etc/rsync_update.passwd
該文件的權限必須為400
[root@test21 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd [root@test21 updatedir]# chmod 400 /etc/rsync_update.passwd [root@test22 ~]# echo "rsync_daemon:123456789" > /etc/rsync_update.passwd [root@test22 updatedir]# chmod 400 /etc/rsync_update.passwd防火墻
這里我是關閉了防火墻,如果防火墻開啟,防火墻規則要將873端口加入
啟動rsyncd服務
[root@test21 ~]# systemctl start rsyncd [root@test22 ~]# systemctl start rsyncd- 查看端口
測試
- test21向test22同步
test22服務器查看
[root@test22 updatedir]# ls file1 file2 file3 file4 file5- test22向test21同步
test21服務器查看
[root@test21 updatedir]# ls file1 file10 file2 file3 file4 file5 file6 file7 file8 file9rsync數據同步成功,我們可以設置linxu定時任務crontab每隔一段時間自動雙向同步文件。
異常處理
- 提示權限不足
test22發送到test21可以正常發送,test21發送到test22無法發送
[root@test21 updatedir]# rsync -av /root/updatedir/ rsync_daemon@10.8.10.22::update Password: sending incremental file list rsync: failed to set times on "." (in update): Permission denied (13) ./ file1 file2 file3 file4 file5 rsync: mkstemp ".file1.PRynLL" (in update) failed: Permission denied (13) rsync: mkstemp ".file2.L3Zgd2" (in update) failed: Permission denied (13) rsync: mkstemp ".file3.t4KaFi" (in update) failed: Permission denied (13) rsync: mkstemp ".file4.PZF46y" (in update) failed: Permission denied (13) rsync: mkstemp ".file5.NOJYyP" (in update) failed: Permission denied (13)報錯后查看網上各種資料描述,說是目錄權限改為755等,試了之后都不行
隨后又增加了一臺服務器做測試,發現新增的也不行
[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update Password: sending incremental file list rsync: failed to set times on "." (in update): Permission denied (13) rsync: recv_generator: failed to stat "123" (in update): Permission denied (13) ./sent 59 bytes received 173 bytes 51.56 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]后來在stackoverflow發現有大佬提示說selinux問題導致,我們查看服務器配置
參考文獻:https://stackoverflow.com/questions/11039559/rsync-mkstemp-failed-permission-denied-13
[root@test21 etc]# getenforce Permissive [root@test20 updatedir]# getenforce Enforcing [root@test22 etc]# getenforce Enforcing發現的確21和22,20配置不同
修改22,20配置
[root@test20 updatedir]# setenforce 0 [root@test20 updatedir]# getenforce Permissive[root@test22 etc]# setenforce 0 [root@test22 etc]# getenforce Permissivesetenforce 0為臨時修改,永久修改vim /etc/sysconfig/selinux
SELINUX=enforcing 修改為 SELINUX=disable 或者 SELINUX=permissive再次執行發現可以同步成功
[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update Password: sending incremental file list ./ 123sent 102 bytes received 38 bytes 40.00 bytes/sec total size is 0 speedup is 0.00[root@test22 etc]# rsync -av /root/updatedir/ root@10.8.10.20::update Password: sending incremental file list ./ 123sent 102 bytes received 38 bytes 40.00 bytes/sec total size is 0 speedup is 0.00sersync
上面安裝完成rsync,我們只能通過定時任務觸發文件同步,sersync可以對文件的變更進行實時監聽,并觸發rsync對變更的文件進行實時同步。
安裝
下載:[點擊下載]
將文件放在兩臺服務器中,并解壓
[root@test22 sersync]# ll 總用量 712 -rw-------. 1 root root 727290 11月 10 13:11 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@test22 sersync]# pwd /opt/sersync [root@test22 sersync]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz GNU-Linux-x86/ GNU-Linux-x86/sersync2 GNU-Linux-x86/confxml.xml [root@test22 sersync]# ls GNU-Linux-x86/ confxml.xml sersync2[root@test21 sersync]# ll 總用量 712 -rw-------. 1 root root 727290 11月 10 13:11 sersync2.5.4_64bit_binary_stable_final.tar.gz [root@test21 sersync]# pwd /opt/sersync [root@test21 sersync]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz GNU-Linux-x86/ GNU-Linux-x86/sersync2 GNU-Linux-x86/confxml.xml [root@test21 sersync]# ls GNU-Linux-x86/ confxml.xml sersync2sersync配置
test21配置
內容過多只顯示了修改的位置,其他都是默認的
<remote ip="10.8.10.22" name="update"/>
<auth start="true" users="rsync_daemon" passwordfile="/etc/rsync.pas"/>
- watch 指定了監控的文件目錄
- remote ip 指定遠程rsync服務監聽的ip,
- name 指定了遠程rsync服務開啟的模塊
- start=“true” 說明遠程rsync服務開啟了用戶密碼驗證
- users 指定了遠程rsync服務用戶密碼驗證指定的虛擬用戶
- passwordfile 指定了連接遠程rsync服務虛擬用戶的密碼文件,權限需為400,用戶自己手動創建
創建密碼文件
[root@test21 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas [root@test21 GNU-Linux-x86]# chmod 400 /etc/rsync.pastest22配置
[root@test22 GNU-Linux-x86]# vim confxml.xml ... <sersync><localpath watch="/opt/updatedir"><remote ip="10.8.10.21" name="update"/><!--<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="rsync_daemon" passwordfile="/etc/rsync.pas"/><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> ...創建密碼文件
[root@test22 GNU-Linux-x86]# echo 123456789 > /etc/rsync.pas [root@test22 GNU-Linux-x86]# chmod 400 /etc/rsync.passersync啟動
test21啟動
[root@test21 updatedir]# /opt/sersync/GNU-Linux-x86/sersync2 -d -r -o /opt/sersync/GNU-Linux-x86/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/GNU-Linux-x86/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 rsync_daemon 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 /root/updatedir && rsync -artuz -R --delete ./ rsync_daemon@10.8.10.22::update --password-file=/etc/rsync.pas >/dev/null 2>&1 run the sersync: watch path is: /root/updatedir- -d 以后臺daemon的方式運行
- -r 第一次啟動時,使用rsync將本地文件全部同步至遠程服務器。
- -o 加載配置文件
test22啟動
[root@test22 updatedir]# /opt/sersync/GNU-Linux-x86/sersync2 -d -r -o /opt/sersync/GNU-Linux-x86/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/GNU-Linux-x86/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 rsync_daemon 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 /root/updatedir && rsync -artuz -R --delete ./ rsync_daemon@10.8.10.21::tongbu1 --password-file=/etc/rsync.pas >/dev/null 2>&1 run the sersync: watch path is: /root/updatedir測試
新增文件
在test21上傳一個文件,在test22查看也同時存在
[root@test21 updatedir]# ll 總用量 4 -rw-------. 1 root root 253 11月 10 11:12 1.sh[root@test22 updatedir]# ll 總用量 4 -rw-------. 1 root root 253 11月 10 11:12 1.sh修改內容
修改test22
[root@test22 updatedir]# ll 總用量 4 -rw-------. 1 root root 253 11月 10 11:12 1.sh [root@test22 updatedir]# echo 1 > 1.sh [root@test22 updatedir]# ll 總用量 4 -rw-------. 1 root root 2 11月 10 13:59 1.sh [root@test22 updatedir]# cat 1.sh 1查看test21發現修改的內容同步成功
[root@test21 updatedir]# cat 1.sh 1刪除文件
刪除test21文件
[root@test21 updatedir]# rm 1.sh rm:是否刪除普通文件 "1.sh"?y [root@test21 updatedir]# ll 總用量 0在test22 查看發現文件也同步刪除
[root@test22 updatedir]# ll 總用量 0總結
以上是生活随笔為你收集整理的Linxu服务器文件双向同步 rsync+sersync 详细讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kerberos Hbase Api 操
- 下一篇: MySql索引的原理