rsync服務

RSYNC服務

概念介紹

備份服務器是架構中最重要的服務器

rsync軟件介紹:

Rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具
全量:將全部數據,進行傳輸覆蓋
增量:只傳輸差異部分的數據

  • 實現增量複製的原理:
    Rsync通過其獨特的“quick check”算法,實現增量傳輸數據

  • 官方增量傳輸算法說明:
    Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks
    for files that have changed in size or in last-modified time. Any changes in the other preserved
    attributes (as requested by options) are made on the destination file directly when the quick check
    indicates that the file’s data does not need to be updated.
    在同步備份數據時,默認情況下,Rsync通過其獨特的“quickcheck”算法,它僅同步大小或者最後修改時間發生變化的文件或目錄,當然也可根據權限,屬主等屬性的變化同步,但需要指定相應的參數,甚至可以實現只同步一個文件裏有變化的內容部分,所以,可以實現快速的同步備份數據。
  • rsync軟件功能:
    1. 類似於cp命令---(本地備份傳輸數據)
    2. 類似於scp命令---(遠程備份傳輸數據)
    3. 類似於rm命令--- (實現無差異同步備份)
    4. 類似於ls命令 --- (本地文件信息查看)
      rsync命令屬於1 v 4的命令

rsync == cp命令
[root@backup ~]#
[root@backup ~]# cp -a /etc/hosts /tmp/
[root@backup ~]# ls /tmp/hosts
/tmp/hosts
[root@backup ~]# rm -f /tmp/hosts
[root@backup ~]# ls /tmp/hosts
ls: cannot access /tmp/hosts: No such file or directory
[root@backup ~]# rsync /etc/hosts /tmp/
[root@backup ~]# ls /tmp/hosts
/tmp/hosts
rsync == scp命令
[root@backup ~]# scp -rp /etc/hosts 10.0.0.31:/tmp/
The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.
RSA key fingerprint is 4d:00:46:ff:a3:4d:1d:2e:5b:f0:72:28:ec:54:29:86.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.31' (RSA) to the list of known hosts.
[email protected]'s password:
hosts 100% 371 0.4KB/s 00:00
[root@backup ~]# rsync -rp /etc/hosts 10.0.0.31:/tmp/
[email protected]'s password:
rsync == rm命令
[root@backup tmp]# mkdir /oldboy01
[root@backup tmp]# cp -a /tmp/ /oldboy01/
[root@backup tmp]# ll /oldboy01/
total 16
-rw-r--r-- 1 root root 371 Oct 11 15:12 hosts
-rw-r--r-- 1 root root 4165 Oct 10 12:24 optimize-init_sys.sh
-rw-r--r-- 1 root root 2184 Aug 5 2015 sysctl.conf
[root@backup tmp]# rm -rf /oldboy01/

[root@backup tmp]# ll /oldboy01/
total 0
[root@backup tmp]# mkdir /null
[root@backup tmp]# cp -a /tmp/* /oldboy01/
[root@backup tmp]# rsync --delete /null/ /oldboy01/
rsync: --delete does not work without -r or -d.
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
[root@backup tmp]# rsync -r --delete /null/ /oldboy01/
[root@backup tmp]# ll /oldboy01/
total 0
rsync == ls -l命令
[root@backup tmp]# # rsync == ls
[root@backup tmp]# ls -l /etc/hosts
-rw-r--r--. 2 root root 371 Oct 10 15:45 /etc/hosts
[root@backup tmp]# rsync /etc/hosts
-rw-r--r-- 371 2017/10/10 15:45:09 hosts

  • Rsync的特性總結(7個特性信息說明):
    1. 支持拷貝普通文件與特殊文件如鏈接文件,設備等。
    2. 可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。
      #tar zcvf backup_1.tar.gz /opt/data -exclude=oldboy
      說明:在打包/opt/data時就排除了oldboy命名的目錄和文件。
    3. 可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變-p。
    4. 可實現增量同步,既只同步發生變化的數據,因此數據傳輸效率很高(tar -N)。

      將備份/home 目錄自 2008-01-29 以來修改過的文件
      tar -N 2008-01-29 -zcvf /backups/inc-backup$(date +%F).tar.gz /home
      將備份 /home目錄昨天以來修改過的文件
      tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup
      $(date +%F).tar.gz /home
      添加文件到已經打包的文件
      tar -rf all.tar *.gif
      說明:這條命令是將所有.gif的文件增加到all.tar的包裏面去。-r是表示增加文件的意思。

    5. 可以使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸文件(rsync本身不對數據加密)
    6. 可以通過socket(進程方式)傳輸文件和數據(服務端和客戶端)*****。重點掌握
    7. 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。

Rsync的企業工作場景說明

  1. 兩臺服務器之間數據同步(定時任務cron+rsync) 同步網站內部人員數據信息(定時任務最小週期爲1分鐘)
  2. 兩臺服務器之間數據同步(實時任務inotify/sersync/lrsyncd+rsync)同步網站用戶人員數據信息

操作部分

rsync軟件工作方式

SYNOPSIS
Local: rsync [OPTION...] SRC... [DEST]
本地數據同步方式
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
遠程數據同步方式:
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
守護進程方式數據同步:

本地數據同步方式(類似於cp)

Local: rsync [OPTION...] SRC... [DEST]
rsync -- 數據同步命令
[OPTION...] -- rsync命令參數信息
SRC -- 要同不得數據信息(文件或目錄)
[DEST] -- 將數據傳輸到什麼位置
實例演示命令:
rsync /etc/hosts /tmp/

遠程數據同步方式(類似scp)--又稱爲隧道傳輸

Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
說明:需要進行交互傳輸數據。如果想實現免交互傳輸數據,需要藉助ssh+key方式實現

pull:
[USER@] : 以什麼用戶身份傳輸數據信息
HOST: 遠程主機信息(IP地址信息 主機名稱信息)
SRC: 遠端要恏過來的數據信息
[dest] 恏到本地什麼位置


push:
SRC: 本地要懟過去的數據信息
DEST 懟到遠端什麼位置

  • 實踐操作:pull
    rsync 10.0.0.31:/tmp/oldboyedu.txt /tmp/
    實踐操作:push
    [root@backup tmp]# rsync -r /tmp 10.0.0.31:/tmp/
    [email protected]'s password:
    [root@backup tmp]# rsync -r /tmp/ 10.0.0.31:/tmp/
    [email protected]'s password:
    說明:/tmp -- 表示將tmp目錄下面數據內容及目錄本身都進行傳輸 /tmp/ -- 表示只傳輸tmp目錄下面的內容信息
    [root@backup tmp]# rsync -vzrtopgP -e 'ssh -p 22' [email protected]:/tmp/ /tmp/
    [email protected]'s password:
    receiving incremental file list
    sent 12 bytes received 154 bytes 36.89 bytes/sec
    total size is 6720 speedup is 40.48
    [root@backup tmp]# scp -rp [email protected]:/tmp/ /tmp/
    [email protected]'s password:
    sysctl.conf 100% 2184 2.1KB/s 00:00
    optimize-init_sys.sh 100% 4165 4.1KB/s 00:00
    hosts 100% 371 0.4KB/s 00:00
    oldboyedu.txt

守護進程方式數據同步:(免交互傳輸同步傳輸數據)

Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

規劃:

  1. backup服務器作爲rsync服務端
  2. 以rsync客戶端服務器作爲參照服務器,將數據推到rsync服務端

配置rsync守護進程方式(需要有服務端與客戶端)

  • 第一部分:配置rsync服務端(將服務端配置到backup服務器上)

    • 第一個里程碑:軟件是否存在
      [root@backup tmp]# rpm -qa|grep rsync
      rsync-3.0.6-12.el6.x86_64
    • 第二個里程碑:進行軟件服務配置
      [root@backup tmp]# ll /etc/rsyncd.conf
      ls: cannot access /etc/rsyncd.conf: No such file or directory
      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
      [backup]
      comment = "backup dir by oldboy"
      path = /backup
      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
      EOF

    • 第三個里程碑:創建rsync服務管理用戶
      useradd -s /sbin/nologin -M rsync
    • 第四個里程碑:創建數據備份存儲目錄
      mkdir /backup
      chown -R rsync.rsync /backup/
    • 第五個里程碑:創建認證用戶密碼文件
      echo "rsync_backup:oldboy123" >/etc/rsync.password
      chmod 600 /etc/rsync.password
    • 第六個里程碑:啓動rsync服務
      rsync --daemon
      [root@backup /]# ps -ef|grep rsync
      root 2623 1964 0 17:27 pts/0 00:00:00 vim /etc/rsyncd.conf
      root 2652 1 0 17:37 ? 00:00:00 rsync --daemon
      root 2654 2574 0 17:37 pts/1 00:00:00 grep --color=auto rsync
      [root@backup /]# netstat -lntup|grep rsync
      tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 2652/rsync
      tcp 0 0 :::873 :::
      LISTEN 2652/rsync
      至此:服務端配置完成
  • 第二部分:配置rsync客戶端(架構中其他服務器稱爲rsync客戶端)
    • 第一個里程碑:軟件是否存在
      [root@backup tmp]# rpm -qa|grep rsync
      rsync-3.0.6-12.el6.x86_64
    • 第二個里程碑:建立認證文件
      echo "oldboy123" >/etc/rsync.password
      chmod 600 /etc/rsync.password
    • 第三個里程碑:測試傳輸
      Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
      交互式:rsync -avz /etc/hosts [email protected]::backup
      非交互式:rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password

常見問題:
問題01:
[root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup
Password:
sending incremental file list hosts
rsync: mkstemp ".hosts.U5OCyR" (in backup) failed: Permission denied (13)
sent 200 bytes received 27 bytes 13.76 bytes/sec
total size is 371 speedup is 1.63
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
說明:備份目錄權限設置不正確

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