Linux服務器部署Rsync服務

Rsync  簡介;

Rsync  是一款開源的、快速的 多功能的 可以實現全量以及增量的本地或者是遠程的數據同步備份的優秀工具
並且可以不進行改變原有的數據屬性信息,實現數據的備份和遷移的特性 ,Rsync 軟件適用於 Linux/unix/windows  等多種操作系統上 。

Rsync備份服務知識點;

1,Rsync可以實現的備份方式 ;

  • 本地備份
  • 遠程備份
  • 無差異備份

2,Rsync實現方式介紹 ;

  •  全量備份數據
  •  增量備份數據 

 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 全量  - 增量的區別                                                                                                                                                               +
+          完整數據傳送表示爲 全量  ,  傳送新增加的數據表示增量,                                                                                  +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   實現rsync增量同步數據,是運用了其獨特的“quick check”算法
    CentOS5,rsync2.x比對方法,把所有的文件比對一遍,然後進行同步。
    CentOS6,rysnc3.x比對方法,一邊比對差異,一邊對差異的部分進行同步。

3,Rsync 特性總結說明 ;

  •  01:支持多種類型文件拷貝
  •  02:支持文件複製排除功能
  •  03:支持文件複製屬性不變
  •  04:支持文件複製增量同步
  •  05:支持文件複製隧道加密
  •  06:支持守護進程同步數據
  •  07:支持數據同步身份驗證

1. 可以鏡像保存整個目錄樹和文件系統;
2. 可以很容易做到保持原來文件的權限、時間、軟硬鏈接等;
3. 無須特殊權限即可安裝;
4. 優化的流程,文件傳輸效率高;
5. 可以使用rcp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接;
6. 支持匿名傳輸,以方便進行網站鏡像。

 

4,Rsync複製原理說明  ;
      網站內部人員數據備份場景 

        1,定時任務+rsync

    網站外部用戶數據備份場景
           2,實時同步工具+rsync

5,Rsync工作方式,介紹  ;

       5.1  本地方式  、 隧道方式  、守護進程 ;
        5.2  單個主機本地之間的數據傳輸(此時類似於cp命令的功能)。
        5.3  藉助rcp,ssh等通道來傳輸數據(此時類似於scp命令的功能)。
        5.4 以守護進程(socket)的方式傳輸數據(這個是rsync自身的重要的功能)。

 

=======================服務端 * 開始部署===================================

[root@rsync ~]# rpm -qa | grep rsync                  #查看軟件是否安裝
rsync-3.0.6-12.el6.x86_64

[root@rsync ~]# 

[root@rsync ~]# rsync --version                           #查看版本信息 
rsync  version 3.0.6  protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes


rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

 配置rsync軟件的配置文件   

 cat >>/etc/rsyncd.conf<<EOF 
    #rsync_config
    #created by HQ at 2017
    ##rsyncd.conf start##                           
    uid = rsync             

    gid = rsync              
    use chroot = no     

    max connections = 200   
    timeout = 300           
    pid file = /var/run/rsyncd.pid            
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    ignore errors
    read only = false
    list = false
    hosts allow = 172.16.1.0/24
    hosts deny = 0.0.0.0/32
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    [backup]
    comment = "backup dir by oldboy"
    path = /backup
    EOF

########################### 配置文件註釋  begin #####################################
#rsync_config
#created by oldboy 15:00 2016-11-15
##rsyncd.conf start##
uid = rsync                                       # 用戶 遠端的命令使用rsync訪問共享目錄
gid = rsync                                      # 用戶組                        
use chroot = no                              # 安全相關
max connections = 200                # 最大連接數
timeout = 300                                # 超時時間
pid file = /var/run/rsyncd.pid      # 進程對應的進程號文件
lock file = /var/run/rsyncd.lock    # 鎖文件
log file = /var/log/rsyncd.log       # 日誌文件
ignore errors                                    # 忽略錯誤
read only = false                                # 可寫
list = false                                           # 不能列表
hosts allow = 172.16.1.0/24          # 允許連接的服務器
hosts deny = 0.0.0.0/32                  # 後勤組連接的服務器
auth users = rsync_backup             # 虛擬用戶
secrets file = /etc/rsync.password   # 虛擬用戶對應的用戶和密碼文件


[backup]                            # 模塊名稱
path = /backup                      # 服務端提供訪問的目錄
########################### 配置文件註釋 end #####################################

第一步  ; 創建備份存儲的目錄,和創建管理用戶用戶組 

[root@rsync ~]# mkdir -p backup                 #創建目錄
[root@rsync ~]# ls
backup  install.log  install.log.syslog  optimize-init_sys.sh  sysctl.conf
[root@rsync ~]# useradd rsync -s /sbin/nologin -M        #創建rsync備份目錄的管理用戶與用戶組
[root@rsync ~]# chown -R rsync.rsync backup/                #授權
[root@rsync ~]# ll    
總用量 64
-rw-------. 1 root  root   1510 11月 18 2017 anaconda-ks.cfg
drwxr-xr-x. 2 rsync rsync  4096 6月  21 23:05 backup
-rw-r--r--. 1 root  root  30419 11月 18 2017 install.log
-rw-r--r--. 1 root  root   9371 11月 18 2017 install.log.syslog
-rwxr-xr-x. 1 root  root   4065 8月   5 2017 optimize-init_sys.sh
-rwxr-xr-x. 1 root  root   2184 8月   5 2017 sysctl.conf
[root@rsync ~]# 

第二步;創建服務端和客戶端的身份認證文件 

[root@rsync ~]# echo "rsync_backup:oldboy123" >/etc/rsync.password
[root@rsync ~]# chmod 600 /etc/rsync.password   #配置文件權限只允許 root 用戶查看 
[root@rsync ~]# cat /etc/rsync.password
rsync_backup:oldboy123
[root@rsync ~]# 

第三步 ;啓動 rsync 服務 

[root@rsync ~]# rsync --daemon                  #啓動服務
[root@rsync ~]# netstat -lntup |grep rsync         #rsync   端口是 873 端口 
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2425/rsync          
tcp        0      0 :::873                      :::*                        LISTEN      2425/rsync          
[root@rsync ~]# 

CentOS 默認以 xinetd 方式運行 rsync 服務。rsync 的 xinetd 配置文件 在 /etc/xinetd.d/rsync。
要配置以 xinetd 運行的 rsync 服務需要執行如下的命令,也可以rsync --daemon 這樣獨立運行 。
# chkconfig rsync on
# service xinetd restart    

======================= 客戶端配置 begin  ===============================

[root@localhost ~]# rpm -qa | grep rsync           #查看軟件是否安裝,如已安裝則無需在進行安裝 
rsync-3.0.9-18.el7.x86_64
[root@localhost ~]# echo "oldboy123" >/etc/rsync.password        #創建認證
[root@localhost ~]# cat /etc/rsync.password
oldboy123
[root@localhost ~]# 

[root@localhost ~]# rsync --daemon          #啓動服務 
[root@localhost ~]# netstat -lntup |grep rsync       #查看是否監聽端口 
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1397/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      1397/rsync          
[root@localhost ~]# 

[root@rsync ~]# rsync -av /etc/hosts 172.16.1.8:/mnt/
[email protected]'s password: 
sending incremental file list
hosts


[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
hosts
[root@localhost mnt]# 

#######################  rsync備份服務部署擴展功能 begin  ############################
①. rsync服務端多模塊功能配置
   
    配置文件多模塊信息
  

 [root@rsync backup]# cat /etc/rsyncd.conf 
    #rsync_config
    #created by HQ at 2017
    ##rsyncd.conf start##
    
    uid = rsync
    gid = rsync
    use chroot = no
    max connections = 200
    timeout = 300
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    ignore errors
    read only = false
    list = false
    hosts allow = 172.16.1.0/24
    hosts deny = 0.0.0.0/32
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    [backup]
    comment = "backup dir by oldboy"
    path = /backup
    [nfsbackup]                     #新增加的一個模塊 
    comment = "nfsbackup dir by oldboy"
    path = /nfsbackup

 說明:01:修改配置文件信息,實現多模塊功能後,需要重啓rsync服務
          02:多模塊信息配置好後,需要創建多模塊指定備份目錄
    
    # 創建多模塊目錄

    mkdir /nfsbackup
        mkdir /nfsbackup -p
        chown -R rsync.rsync /nfsbackup/
        ll -d /nfsbackup/
    

  # 進行多模塊目錄測試

rsync -avz /etc/rc.local [email protected]::backup --password-file=/etc/rsync.password 
    
rsync -avz /etc/rc.local [email protected]::nfsbackup --password-file=/etc/rsync.password 
    

    # 利用rsync客戶端在備份目錄中創建多級目錄
    ①. 多級目錄的表現格式爲 backup/sa/   如果sa後面沒有接/ 表示的是文件不是目錄
    ②. rsync客戶端創建目錄,只能創建一級,不能創建多級目錄
    
 # 利用rsync客戶端同步數據(非常重要)
    rsync -avz /tmp/ [email protected]::backup/ --password-file=/etc/rsync.password 
    說明:/tmp/ 表示同步tmp目錄下內容,但不包含目錄本身
    rsync -avz /tmp [email protected]::backup/ --password-file=/etc/rsync.password 
    說明:/tmp 表示同步tmp目錄下內容及目錄本身

===============================================================
#rsync異常問題總結:


問題一:
[root@nfs tmp]# rsync -avz /etc/hosts [email protected]::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]
問題原因:
①. 認證的用戶名或者密碼輸入的不正確
②. 認證文件編寫有問題(認證內容信息)
③. 認證文件的權限設置的不正確(權限爲600,文件用戶和用戶組還是root不變)
④. rsync服務端配置文件中指定的認證文件路徑與實際認證文件路徑不符


問題二:
[root@nfs01~]#rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
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服務沒有啓動


問題三:
rsync -avz /etc/sysconfig/network [email protected]::nfsbackup --password-file=/etc/rsync.password 
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
解決原因:
①. rsync服務端沒有模塊指定的目錄,需要進行創建和授權


問題四:
[root@web01 ~]# rsync -avz /etc/rc.local [email protected]::backup/sa/ --password-file=/etc/rsync.password 
sending incremental file list
rsync: mkdir "sa" (in backup) failed: File exists (17)
rsync error: error in file IO (code 11) at main.c(576) [receiver=3.0.6]
rsync: connection unexpectedly closed (5 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

①. 傳輸的文件或目錄與備份服務目錄中文件名稱相同,拒絕傳輸

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