資料同步——rsync遠端同步

Geroge_Ming發表於2020-11-02

rsync同步簡介

關於rsync

■ 一款增量備份工具

Remote Sync,遠端同步
支援本地複製,或者與其他SSH、rsync主機同步
官方網站: http://rsync.samba.org

配置rsync備份源

配置rsync源伺服器

■ rsync同步源

指備份操作的遠端伺服器,也稱備份源

配置rsync源

■ 基本思路

建立rsyncd.conf配置檔案、獨立的帳號檔案
啟用rsync的 --daemon模式

■ 應用示例

使用者backuper,允許下行同步
操作目錄為 /var/www/html(安裝HTTP)

■ 配置檔案rsyncd.conf

需手動建立,語法類似於Samba配置
認證配置auth users、secrets file,不加則為匿名

■ rsync帳號檔案

採用 “使用者名稱:密碼” 的記錄格式,每行一個使用者記錄
獨立的賬號資料,不依賴於系統賬號

■ 啟用rsync服務

通過 --daemin獨自提供服務

使用rsync備份工具

■ rsync命令的用法

rsync [選項] 原始位置 目標位置

■ 常用選項

-a:歸檔模式,遞迴併保留物件屬性
-v:顯示同步過程的詳細資訊
-z:在傳輸檔案時進行壓縮
-H保留硬連結檔案
-A:保留ACL屬性資訊
–delete:刪除目標位置而原始位置沒有的檔案
–checksum:根據物件的校驗和來決定是否跳過檔案

■ 配置源的兩種表示方法

格式1:使用者名稱@主機地址::共享模組名
格式2:rsync://使用者名稱@主機地址/共享模組名


格式一:
[root@localhost etc]# rsync backuper@20.0.0.3::wwwroot /opt/

格式二:
[root@localhost etc]#  rsync -avz rsync://backuper@20.0.0.3::wwwroot /opt/

rsync+inotify結合使用

rsync遠端同步實驗操作

要先關閉防火牆和核心防護
[root@localhost ~]# systemctl stop  firewalld  ###關閉防火牆 
[root@localhost ~]# vim /etc/selinux/config    ###關閉核心防護
SELINUX=disabled
[root@localhost ~]# iptables -F    ###清空防火牆規則
[root@localhost ~]# setenforce 0   ###臨時關閉核心防護

####配置rsync源伺服器####

uid = nobody
gid = nobody
use chroot = yes  ###禁錮家目錄
address = 20.0.0.3   ###監聽地址
port 873                      ###埠號
log file = /var/log/rsyncd.log   ###指定日誌檔案
pid file = /var/run/rsyncd.pid   ###開啟pid
hosts allow = 20.0.0.0/24          ###執行哪些網段可以訪問(白名單)


[wwwroot]                     ###共享模組名(自定義的)
path = /var/www/html    ###共享站點目錄
comment = www.zk.cn  ###描述性資訊,可以隨便寫
read only = no                 ###只讀模式
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  ###裡面這些格式不進行壓縮(因為格式本來就是壓縮格式)
auth users = backuper      ###建立一個身份驗證的賬戶
secrets file = /etc/reyncd_users.db   ###配置了身份驗證賬戶,那要有密碼,去這裡配置密碼

[root@localhost ~]#vim  /etc/reyncd_users.db   ###新增密碼
backuper:abc123

[root@localhost ~]#  chmod 600 /etc/reyncd_users.db   ###給讀的許可權

[root@localhost ~]# yum -y install httpd  ###安裝一下apache,因為需要/var/www/html檔案

[root@localhost ~]# reync --daemon   ###啟動服務
[root@localhost ~]# rsync -antp |grep 873    ###過濾一下埠號看看起來了沒

發起端配置

發起端:
格式一:
[root@localhost etc]# rsync backuper@20.0.0.3::wwwroot /opt/   ###進行同步
格式二:
[root@localhost etc]#  rsync -avz rsync://backuper@20.0.0.3::wwwroot /opt/    ###進行同步

免互動:
[root@localhost ]#cd /etc/  
[root@localhost etc]# touch server.pass  ###建立存密碼檔案
[root@localhost etc]#  vim server.pass     ###往裡面寫密碼
abc123
[root@localhost etc]#  chmod 600 /etc/server.pass  ###給讀的許可權

[root@localhost etc]# rsync -avz --delete --password-file=/etc/server.pass   backuper@20.0.0.3::wwwroot /opt/  ###面互動式同步

發起端可以做計劃任務定期同步

[root@localhost ]# crontab -e  ###可以用計劃任務定期同步

rsync+inotify實驗操作

發起端配置

[root@localhost ~]# vim /etc/sysctl.conf  ###調整inotify核心引數,在最後新增這三段
fs.inotify.max_queued_events = 16384     ###監控事件佇列大小
fs.inotify.max_user_instances = 1024        ###最多監控例項數
fs.inotify.max_user_watches = 1048576    ###每個例項最多監控檔案數,監控數應大於監控目標的總檔案數

[root@localhost ~]# sysctl -p               ###重新整理生效一下
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

將inotify-tools-3.14.tar工具上傳上來
[root@localhost abc]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/   ###將工具解壓到/opt目錄下

[root@localhost inotify-tools-3.14]# yum -y install gcc gcc-c++ make  ###安裝一下gcc編譯器

[root@localhost ~]# cd /opt/inotify-tools-3.14/                       ###到這個目錄下
[root@localhost inotify-tools-3.14]# ./configure                      ###執行
[root@localhost inotify-tools-3.14]# make && make install   ###編譯安裝

[root@localhost ~]# yum -y install httpd   ###安裝一下apache
[root@localhost ]# chmod 777 /var/www/html/    ###目錄給個777許可權

inotifywait  -mrq  -e  modify,create,move,delete  /var/www/html     ### -mrq:持續性監控,-e:操作,modify:修改,create:建立,move:移動,delete:刪除  ,監控本地的/var/www/html

敲了上面的監控命令就會持續監控不能操作,需要在開一個發起端操作字元頁面

進入到另開的發起端操作字元介面

[root@localhost ~]# cd /var/www/html/  ###進入到html目錄
[root@localhost html]# touch abc    ###建立一個abc

------然後剛剛敲監控那邊就會出現操作提示-----
/var/www/html/ CREATE abc

沒問題的話,我們就回剛剛監控的發起端,ctrl+c或ctrl+z結束掉監控

[root@localhost ~]# cd /opt/   
[root@localhost opt]# vim inotify.sh  
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@20.0.0.3::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if  [ $(pgrep rsync | wc -l) -le 0 ] ; then
         $RSYNC_CMD
    fi
done

[root@localhost opt]# chmod +x inotify.sh   ###給個執行許可權


[root@localhost ]# ls -l /var/www/  ###用這條命令檢視兩邊都檢視一下html許可權給足了沒

在源端在調一個資料
前面配置的時候已經是no了,就不用操作這幾步了,直接跳到下面的發起端啟動指令碼就好

[root@localhost ~]# vim /etc/rsyncd.conf  
read only = no   ###需要把wwwroot模組的只讀模式改成no

[root@localhost ~]# netstat -ntap | grep rsync  ###雖然改了配置,但是還沒重啟,檢視一下程式
tcp        0      0 20.0.0.3:873            0.0.0.0:*               LISTEN      20491/rsync  
 
[root@localhost ~]# kill -9 20491                        ###殺死程式
[root@localhost ~]# netstat -ntap | grep rsync   ###在檢視一下應該就沒有了

[root@localhost ~]# cd /var/run/                ###去這個目錄下
[root@localhost run]# rm -rf rsyncd.pid     ###刪掉這個pid
[root@localhost run]# rsync --daemon      ###啟動一下就行

[root@localhost run]# netstat -ntap | grep rsync   ###在看一下就又有了
tcp        0      0 20.0.0.3:873            0.0.0.0:*               LISTEN      70903/rsync  

---------------------------發起端啟動指令碼----------------------

[root@localhost opt]# ./inotify.sh 

--------------需要在開一個發起端操作字元頁面------------------

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is test" > test.txt

---------------------監控那邊的發起端就會出現提示----------------------

[root@localhost opt]# ./inotify.sh  ###這個報錯不影響同步
rsync: chgrp "/html" (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp "/html/.test.txt.at3wRj" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

--------------------源端檢視一下是否同步過去了------------------------

[root@localhost run]# cd /var/www/html/
[root@localhost html]# cd html
[root@localhost html]# ls
test.txt

rsync的同步操作和rsync+inotify結合使用操作完成

Rsync服務常見問題彙總講解

客戶端的錯誤現象:No route to host

 rsync服務端開啟的iptables防火牆

   [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

   rsync: failed to connect to 172.16.1.41: No route to host (113)

   rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]

異常問題解決:

   關閉rsync服務端的防火牆服務(iptables)

   [root@backup mnt]# /etc/init.d/iptables stop

   iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

   iptables: Flushing firewall rules:                         [  OK  ]

   iptables: Unloading modules:                               [  OK  ]

   [root@backup mnt]# /etc/init.d/iptables status

   iptables: Firewall is not running.

==============================================================================================

ERROR: The remote path must start with a module name not a /
rsync客戶端執行rsync命令錯誤:

客戶端的錯誤現象:

   [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::/backup

   ERROR: The remote path must start with a module name not a /

   rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

異常問題解決:

   rsync命令語法理解錯誤,::/backup是錯誤的語法,應該為::backup(rsync模組)

==============================================================================================

@ERROR: auth failed on module backup
@ERROR: auth failed on module oldboy

客戶端的錯誤現象:

   [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

異常問題解決:

   1. 密碼真的輸入錯誤,使用者名稱真的錯誤

   2. secrets file = /etc/rsync.password指定的密碼檔案和實際密碼檔名稱不一致

   3. /etc/rsync.password檔案許可權不是600

   4. rsync_backup:123456密碼配置檔案後面注意不要有空格

   5. rsync客戶端密碼檔案中只輸入密碼資訊即可,不要輸入虛擬認證使用者名稱稱

==============================================================================================

@ERROR: Unknown module ‘backup’

4. Unknown module 'backup'  

[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

@ERROR: Unknown module 'backup'

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

異常問題解決:

   1、 /etc/rsyncd.conf配置檔案模組名稱書寫錯誤

   2、配置檔案中網段限制不對

==============================================================================================

Permission denied

[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

sending incremental file list

hosts

rsync: mkstemp ".hosts.5z3AOA" (in backup) failed: Permission denied (13)

 

sent 196 bytes  received 27 bytes  63.71 bytes/sec

total size is 349  speedup is 1.57

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]  

異常問題解決:

   1. 共享目錄的屬主和屬組不正確,不是rsync

   2. 共享目錄的許可權不正確,不是755

==============================================================================================

chdir failed

[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

@ERROR: chdir failed

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

異常問題解決:

    1. 備份儲存目錄沒有建立

    2. 建立的備份儲存目錄和配置檔案定義不一致

    [root@backup backup]# /etc/init.d/xinetd restart

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Stopping xinetd:                                           [  OK  ]

Starting xinetd: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
                                                                     [  OK  ]

   說明:如果沒有備份儲存目錄,xinetd服務都不能正確啟動

==============================================================================================

invalid uid rsync

[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

@ERROR: invalid uid rsync

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
異常問題解決:

rsync服務對應rsync虛擬使用者不存在了

==============================================================================================

客戶端已經配置了密碼檔案,但免祕鑰登入方式,依舊需要輸入密碼
password file must not be other-accessible

[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

password file must not be other-accessible

continuing without password file

Password:

sending incremental file list

sent 26 bytes  received 8 bytes  5.23 bytes/sec

total size is 349  speedup is 10.26
異常問題解決:

rsync客戶端的祕鑰檔案也必須是600許可權

==============================================================================================

rsync客戶端連線慢問題
錯誤日誌輸出

2017/03/08 20:14:43 [3422] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors

2017/03/08 20:14:43 [3422] name lookup failed for 172.16.1.31: Name or service not known

2017/03/08 20:14:43 [3422] connect from UNKNOWN (172.16.1.31)

2017/03/08 20:14:43 [3422] rsync to backup/ from rsync_backup@unknown (172.16.1.31)

2017/03/08 20:14:43 [3422] receiving file list

2017/03/08 20:14:43 [3422] sent 76 bytes  received 83 bytes  total size 349

正確日誌輸出

2017/03/08 20:16:45 [3443] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors

2017/03/08 20:16:45 [3443] connect from nfs02 (172.16.1.31)

2017/03/08 20:16:45 [3443] rsync to backup/ from rsync_backup@nfs02 (172.16.1.31)

2017/03/08 20:16:45 [3443] receiving file list

2017/03/08 20:16:45 [3443] sent 76 bytes  received 83 bytes  total size 349
異常問題解決:

檢視日誌進行分析

==============================================================================================

rsync服務沒有正確啟動Connection refused (111)

 [root@oldboy-muban ~]#  rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

rsync: failed to connect to 172.16.1.41: Connection refused (111)

rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]

解決 rsync服務沒開啟

[root@oldboy-muban ~]# rsync --daemon

[root@oldboy-muban ~]# ss -lntup |grep rsync

tcp    LISTEN     0      5                     :::873                  :::*      users:(("rsync",1434,5))

tcp    LISTEN     0      5                      *:873                   *:*      users:(("rsync",1434,4))

[root@oldboy-muban ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

sending incremental file list

hosts

 sent 196 bytes  received 27 bytes  49.56 bytes/sec

total size is 349  speedup is 1.57

==============================================================================================

11 port 22: Connection refused
環境:本地伺服器叢集內部傳輸利用遠端ssh 報錯

利用(telnet 172.16.1.31 22) 排查服務監聽狀態後採取的解決方法

[root@oldboy-muban ~]# rsync /etc/hosts 172.16.1.31:/tmp

ssh: connect to host 172.16.1.31 port 22: Connection refused

rsync: connection unexpectedly closed (0 bytes received so far) [sender]

rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

排錯思路:

[root@oldboy-muban ~]# ping 172.16.1.31

PING 172.16.1.31 (172.16.1.31) 56(84) bytes of data.

64 bytes from 172.16.1.31: icmp_seq=1 ttl=64 time=0.628 ms

64 bytes from 172.16.1.31: icmp_seq=2 ttl=64 time=0.393 ms

64 bytes from 172.16.1.31: icmp_seq=3 ttl=64 time=1.06 ms

64 bytes from 172.16.1.31: icmp_seq=4 ttl=64 time=0.745 ms

 

[root@oldboy-muban ~]# traceroute 172.16.1.31

traceroute to 172.16.1.31 (172.16.1.31), 30 hops max, 60 byte packets

 1  nfs01 (172.16.1.31)  0.597 ms  0.189 ms  0.965 ms

/etc/init.d/iptables status

iptables: Firewall is not running.

[root@backup ~]#

[root@backup ~]# netstat -lntup|grep 22

  p        0      0 10.0.0.31:22                0.0.0.0:*                   LISTEN      1187/sshd   

故障原因:無法連線

telnet 172.16.1.31 22

解決方法:

[root@oldboy-backup-41]# vim /etc/ssh/sshd_config

#Port 22

#AddressFamily any

#ListenAddress 10.0.0.31 改為 0.0.0.0

#ListenAddress ::

總結:內網傳輸通過SSH pro 22 表明22埠連結不上

==============================================================================================

12 --passwd-file=/etc/rsync.passwd: unknown option 沒有正確輸入password檔名
報錯:–passwd-file=/etc/rsync.passwd: unknown option

錯誤案例

本地rsync.password 檔案要保持一致缺少字母都會報錯

echo "123456">>/etc/rsync.passwd

[root@nfs01 ~]# chmod 600 /etc/rsync.passwd

[root@nfs01 ~]# ll /etc/rsync.passwd

-rw------- 1 root root 7 Mar  9 13:47 /etc/rsync.passwd

[root@nfs01 ~]# rsync  -az -P /root/ rsync_backup@172.16.1.41::backup --passwd-file=/etc/rsync.passwd

rsync: --passwd-file=/etc/rsync.passwd: unknown option

rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

正確做法:

[root@nfs01 ~]# echo "123456">>/etc/rsync.password

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

[root@nfs01 ~]# ll /etc/rsync.password

-rw------- 1 root root 7 Mar  9 13:49 /etc/rsync.password

rsync  -az -P /server/files/secure-20161219  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

sending incremental file list

secure-20161219

    51053780 100%   14.31MB/s    0:00:03 (xfer#1, to-check=0/1)

rsync: mkstemp ".secure-20161219.lcnuWA" (in backup) failed: Permission denied (13)

 

sent 2210982 bytes  received 27 bytes  491335.33 bytes/sec

total size is 51053780  speedup is 23.09

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

 

[root@backup ~]# ls /backup/

100.log          cc.txt       optimize-init_sys.sh

anaconda-ks.cfg

      1)可能是服務沒有開啟

      2)iptables SELinux

      3)本次遇見sshd傳輸受限 限制了傳輸的ip(安全)

==============================================================================================

Rsync服務端排錯思路

檢視rsync服務配置檔案路徑是否正確 /etc/rsyncd.conf

檢視配置檔案例的host allow,host deny,允許的ip網段是否是允許客戶端訪問的ip網段

檢視配置檔案中path引數裡的路徑是否存在,許可權是否正確(正常應為配置檔案中的UUID引數對應的屬主和組)

檢視rsync服務是否啟動,埠是否存在 ps -ef  netstat -lntup

檢視iptables防火牆和SELinux是否開啟允許rsync服務通過,也可以關閉

檢視服務端rsync配置檔案裡的密碼許可權是否為600 密碼檔案格式是否正確,正確格式(使用者名稱:密碼)檔案路徑和配置檔案裡的secrect files 引數對應

如果是推送資料,要檢視,配置rsyncd.conf 檔案中使用者是否對模組下目錄有可讀的許可權

==============================================================================================

客戶端排錯思路

檢視客戶端rsync配置的密碼檔案是否為600的許可權,密碼檔案格式是否正確,注意:僅需要有密碼,並且和服務端的密碼一致

用telnet連結rsync伺服器ip地址873埠,檢視服務是否啟動(可測試服務端防火牆是否阻擋telnet10.0.0.100 873)

客戶端執行命令是 rsync -avzP rsync_backup@10.0.0.100::backup/test/test/ --password-file=/etc/rsync.password

此命令要記清楚尤其10.0.0.100::backup/test/處的雙引號及隨後的backup為模組名稱

相關文章