RSYNC 服務介紹

RSYNC 服務介紹

一、rsync介紹

  • rsync功能
    • 作爲命令,實現本地-遠程文件同步
    • 作爲服務,實現本地-遠程文件同步
  • rsync特點
    • 可以鏡像保存整個目錄樹和文件系統
    • 可以保留原有的權限(permission,mode),owner,group,時間(修改時間,modify time),軟硬鏈接,文件acl,文件屬性(attributes)信息等
    • 傳輸效率高,使用同步算法,只比較變化的
    • 支持匿名傳輸,方便網站鏡像;也可以做驗證,加強安全
  • rsync同類服務
    • sync 同步:刷新文件系統緩存,強制將修改過的數據塊寫入磁盤,並且更新超級塊。
    • async 異步:將數據先放到緩衝區,再週期性(一般是30s)的去同步到磁盤。
    • rsync 遠程同步:remote synchronous

二、rsync語法介紹

2.1 man rsync查看

rsync(1)                                                              rsync(1)

NAME
       rsync ? a fast, versatile, remote (and local) file-copying tool  //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
//shell遠程訪問(命令模式)
       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
//通過後臺程序訪問(作爲服務)
       Usages  with  just  one  SRC arg and no DEST arg will list the source
       files instead of copying.

2.2 rsync相關參數

    -v      詳細模式輸出
    -a      歸檔模式,遞歸的方式傳輸文件,並保持文件的屬性,equals -rlptgoD
    -r      遞歸拷貝目錄
    -l      保留軟鏈接
    -p      保留原有權限
    -t      保留原有時間(修改)
    -g      保留屬組權限
    -o      保留屬主權限
    -D      等於--devices  --specials    表示支持b,c,s,p類型的文件
    -R      保留相對路徑
    -H      保留硬鏈接
    -A      保留ACL策略
    -e      指定要執行的遠程shell命令
    -E      保留可執行權限
    -X      保留擴展屬性信息  a屬性

三、 rsync作爲命令同步數據

3.1 本機同步數據

[root@review1 ~]# mkdir dir1
[root@review1 ~]# mkdir dir2
[root@review1 ~]# touch dir1/file{1..5}
[root@review1 ~]# ls dir1/
file1  file2  file3  file4  file5
[root@review1 ~]# ls dir2
[root@review1 ~]# 
//檢測dir1中有5個文件,dir2中沒有文件

//同步方式一:
[root@review1 ~]# rsync -va /root/dir1 /root/dir2
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5

sent 279 bytes  received 111 bytes  780.00 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# 
[root@review1 ~]# ls dir2
dir1
//同步dir1到dir2中成功

//方式二:
[root@review1 ~]# rsync -va /root/dir1/ /root/dir2
sending incremental file list
./
file1
file2
file3
file4
file5

sent 266 bytes  received 110 bytes  752.00 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls dir2
dir1  file1  file2  file3  file4  file5

總結:
1. 本地數據同步的時候,源目錄後面的“/”會影響同步的結果
2. # rsync -av /dir1/ /dir3 //只同步目錄下面的文件到指定的路徑
3. # rsync -av /dir1 /dir2 //將當前目錄dir1和目錄下的所有文件一起同步

-R:不管加不加”/”,都會將源數據的絕對路徑一起同步

3.2 遠程同步

需求1:將本地(192.168.221.129)/root/dir1 文件同步到遠端(192.168.226.128)/root/dir中:

[root@review1 ~]# rsync -av /root/dir1 [email protected]:/root/dir/       //同步的命令
The authenticity of host '192.168.226.128 (192.168.226.128)' can't be establis
RSA key fingerprint is 24:36:34:69:1f:6e:b7:60:b0:2a:ae:90:46:aa:86:c5.
Are you sure you want to continue connecting (yes/no)? yes              //key授權
Warning: Permanently added '192.168.226.128' (RSA) to the list of known hosts.
[email protected]'s password:                                      //遠端用戶密碼
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5

sent 279 bytes  received 111 bytes  26.90 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]#

//遠端:192.168.226.128
[root@min1 dir]# pwd
/root/dir
[root@min1 dir]# ls
dir1
[root@min1 dir]# 
//同步成功

  • 本地到遠程同步另一種寫法,利用ssh

將本地(192.168.221.129)/root/dir文件同步到遠端(192.168.226.128)/root/中:

[root@review1 dir2]# rsync -ave 'ssh -lroot' /root/dir 192.168.226.128:/root/ 
root@192.168.226.128's password: 
sending incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5

sent 300 bytes  received 115 bytes  26.77 bytes/sec
total size is 0  speedup is 0.00

主機192.168.226.128

[root@min1 ~]# pwd
/root
[root@min1 ~]# ls
anaconda-ks.cfg  install.log         software
dir              install.log.syslog  testdir
[root@min1 ~]# cd dir
[root@min1 dir]# ls
dir1
[root@min1 dir]# 
//同步成功

需求2:將遠程數據/root/dir同步到本地/root中:

[root@review1 ~]# rsync -av root@192.168.226.128:/root/dir /root
root@192.168.226.128's password: 
receiving incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5

sent 114 bytes  received 305 bytes  25.39 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls /root
anaconda-ks.cfg  dir  dir1  dir2  install.log  install.log.syslog
[root@review1 ~]# 
//同步成功!

注1:

rsync 並不是單純的複製文件,它主要功能是進行文件同步!例如:

//主機:192.168.226.128 中testdir目錄下有5個文件
[root@min1 testdir]# pwd
/root/testdir
[root@min1 testdir]# ls
file1  file2  file3  file4  file5
[root@min1 testdir]# 

//本地主機:192.168.221.129 中/root目錄
[root@review1 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
//將遠程testdir同步到本地
[root@review1 ~]# rsync -avR [email protected]:testdir /root/
[email protected]'s password: 
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5

sent 110 bytes  received 287 bytes  27.38 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  testdir
[root@review1 ~]# cd testdir/
[root@review1 testdir]# ls
file1  file2  file3  file4  file5

//遠端刪除file1-file3
[root@min1 testdir]# ls
file1  file2  file3  file4  file5
[root@min1 testdir]# rm file{2..4}
rm: remove regular empty file `file2'? y
rm: remove regular empty file `file3'? y
rm: remove regular empty file `file4'? y
[root@min1 testdir]# ls
file1  file5
//本地同步
[root@review1 testdir]# rsync -avR --delete [email protected]:testdir /root/
[email protected]'s password: 
receiving incremental file list
deleting testdir/file4
deleting testdir/file3
deleting testdir/file2
testdir/

sent 15 bytes  received 75 bytes  3.16 bytes/sec
total size is 0  speedup is 0.00
[root@review1 testdir]# ls
file1  file5
[root@review1 testdir]# 
//加上--delete參數,源文件刪除,本地也刪除

總結:

​ rsync是一個同步命令(服務),它不僅可以用來複制、備份,最大的作用在於同步,即保持兩端一直,所以遠端文件被刪除後,同步後,本地文件也可以刪除,要注意rsync的靈活用法。

注2:

rsync同步參數-R是會同步絕對路徑的。例:

[root@review1 ~]# rsync -avR [email protected]:/root/testdir /root/
root@192.168.226.128's password: 
Permission denied, please try again.
root@192.168.226.128's password: 
receiving incremental file list
root/
root/testdir/
root/testdir/file1
root/testdir/file2
root/testdir/file3
root/testdir/file4
root/testdir/file5

sent 114 bytes  received 315 bytes  15.05 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  root
[root@review1 ~]# cd root/
[root@review1 root]# ls
testdir
//此處同步將192.168.226.128中的root也創建了一個,同步的是絕對路徑,沒有的文件夾自動幫你創建。

[root@review1 root]# rsync -avR [email protected]:testdir /root/
root@192.168.226.128's password: 
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5

sent 110 bytes  received 287 bytes  29.41 bytes/sec
total size is 0  speedup is 0.00
[root@review1 root]# ls
testdir
[root@review1 root]# cd
[root@review1 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  root  testdir
//將root從目錄中去掉就不會再創建root,直接在本地的/root 中直接創建了testdir

總結:

​ rsync -R會同步絕對路徑,沒有則自動創建,所以在寫命令時一定要注意。

四、rsync作爲服務運行

​ rsync作爲服務是託管給xinetd服務管理的,有以下特點:①進程在後臺運行,不受終端影響(關終端不會關閉服務,除非殺死相關進程)②可以用相關參數實現一些功能,比如:日誌記錄,訪問控制,驗證登錄等

4.1 無密碼同步

  • 修改子配置文件/etc/xinetd.d/rsync
  1 # default: off
  2 # description: The rsync server is a good addition to an ftp server,     as it \
  3 #       allows crc checksumming etc.
  4 service rsync
  5 {
  6         disable = no                        //打開rsync服務
  7         socket_type     = stream
  8         wait            = no
  9         user            = root
 10         server          = /usr/bin/rsync
 11         server_args     = --daemon
 12         log_on_failure  += USERID
 13 }

  • 沒有主配置文件,需要自己創建
[root@review1 etc]# pwd
/etc
[root@review1 etc]# ls |grep rsync.conf
[root@review1 etc]# 
  • 創建主配置文件/etc/rsyncd.conf (注意,這裏是rsyncd.conf 不是rsync.conf,配置文件名寫錯會報錯,無法遠程同步。)

    • 全局參數:可以指定默認端口、pid文件、ip地址等

      例如:
      motd file=/etc/rsyncd.welcome 歡迎文件,路徑自定義[可選]

    • 局部參數
      例如:
      [notes] 共享給客戶端看到的名字,名字自定義,命令中寫這個名字
      path=/share/dir/ 實際共享的服務器路徑,名字必須是你要共享的實際路徑

  • 啓動xinetd服務

[root@review1 etc]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@review1 etc]# netstat -ntpl |grep 873
tcp        0      0 :::873                      :::*                        LISTEN      1745/xinetd

//查看遠端服務器192.168.221.128共享文件
[root@review1 etc]# rsync -a 192.168.221.128::
share                   //共享成功(顯示的是標籤名)

//檢查192.168.221.128的共享文件
[root@min2 ~]# cat /etc/rsyncd.conf 
[share]
path=/root/dirtest
[root@min2 ~]# cd dirtest/
[root@min2 dirtest]# ls
123

[root@review1 ~]# rsync -avR  192.168.221.128::share /root/    //
receiving incremental file list

sent 27 bytes  received 67 bytes  188.00 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls
123  anaconda-ks.cfg  dir  install.log  install.log.syslog  testdir

注:若要把本地文件同步到遠程主機上,需要更改主配置文件

[root@min2 dirtest]# vim /etc/rsyncd.conf
  1 [share]
  2 path=/root/dirtest/
  3 read only = false
  4 uid = root
  5 gid = root

[root@min2 ~]# chmod 755 dirtest/
[root@min2 ~]# ll
total 32
-rw-------. 1 root root  1128 May 17 19:56 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 May 23 12:30 dirtest

//要想遠程同步需要注意三點:①rsync權限:共享文件夾必須設爲可讀②共享文件夾權限設爲755③uid和gid設爲root

[root@review1 ~]# rsync -av /root/dir/ [email protected]::share
sending incremental file list
./

sent 25 bytes  received 11 bytes  72.00 bytes/sec
total size is 0  speedup is 0.00
//同步成功

4.2 密碼同步

  • 修改主配置文件
[root@min2 etc]# vim /etc/rsyncd.conf 
  1 [share]
  2 path=/root/dirtest/
  3 read only = false
  4 uid = root
  5 gid = root
  6 auth users = root                   //有權限訪問的用戶,這裏沒有的用戶沒有權限訪問rsync服務
  7 secrets file = /etc/rsyncd.secrets    //指明密碼文件路徑
  • 設置其他相關配置
[root@min2 etc]# vim /etc/rsyncd.secrets  //寫密碼文件中內容:可以訪問的用戶及其密碼
  1 root:123456
[root@min2 etc]# chmod 600 /etc/rsyncd.secrets   //更改密碼文件權限
[root@min2 etc]# ll |grep rsyncd.secrets 
-rw-------.  1 root root     12 May 23 14:36 rsyncd.secrets
[root@min2 etc]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
  • 測試驗證
[root@review1 ~]# rsync -av root@192.168.221.128::share /root/dir/
Password: 
receiving incremental file list
123

sent 74 bytes  received 149 bytes  49.56 bytes/sec
total size is 0  speedup is 0.00
[root@review1 ~]# ls /root/dir/
123                                     //訪問成功

特別注意:

①配置文件中auth users 寫了哪個用戶,哪個用戶就可以訪問,沒寫的就不能訪問,密碼訪問一旦開啓,所有人都要輸入密碼,所以沒有在auth users中指定的用戶是無法訪問的。

②密碼文件的屬主必須是rsync服務的運行者,權限必須是600。例如:root運行rsync –daemon,則secrets file的owner也必須是root;secrets file權限必須是600。

4.3 rsync日誌自定義輸出

  • 日誌定義
[root@review1 ~]# man rsync                //man一下,瞭解rsync日誌語法

--log-file=FILE         override the "log file" setting         //日誌文件路徑定義
--log-file-format=FMT   override the "log format" setting       //日誌格式定義

 --log-file=FILE
              This option causes rsync to log what it is doing to a file.  This is similar to
              the logging that a daemon does, but can be requested for the client side and/or
              the server side of a non-daemon transfer.  If specified  as  a  client  option,
              transfer  logging  will be enabled with a default format of “%i %n%L”.  See the
              --log-file-format option if you wish to override this.

              Here’s a example command that requests the remote side to log what  is  happen-
              ing:

                rsync -av --rsync-path="rsync --log-file=/tmp/rlog" src/ dest/

              This  is very useful if you need to debug why a connection is closing unexpect-
              edly.

       --log-file-format=FORMAT
              This allows you to specify exactly what per-update logging is put into the file
              specified  by  the  --log-file  option  (which  must also be specified for this
              option to have any effect).  If you specify an empty string, updated files will
              not  be  mentioned  in the log file.  For a list of the possible escape charac-
              ters, see thelog format” setting in the rsyncd.conf manpage.

              The default FORMAT used if --log-file is specified and this option  is  not  is
              ’%i %n%L’.

  • 配置文件寫法
[root@review1 ~]# vim /etc/rsyncd.conf

  1 log file=/var/log/rsyncd.log   
  2 
  3 [notes]
  4 path=/root/testdir

注意:

​ 配置文件是全局變量,必須寫在標籤外面!

  • 測試
[root@review1 log]# cd /var/log/
[root@review1 log]# ls
anaconda.ifcfg.log    boot.log    messages
anaconda.log          btmp        ntpstats
anaconda.program.log  cron        secure
anaconda.storage.log  dmesg       spooler
anaconda.syslog       dmesg.old   tallylog
anaconda.xlog         dracut.log  wtmp
anaconda.yum.log      lastlog     yum.log
audit                 maillog
//rsync.log 不存在

[root@mysql ~]# rsync [email protected]::
notes           
//遠端訪問一下
[root@review1 log]# ls
anaconda.ifcfg.log    boot.log    messages
anaconda.log          btmp        ntpstats
anaconda.program.log  cron        rsyncd.log
anaconda.storage.log  dmesg       secure
anaconda.syslog       dmesg.old   spooler
anaconda.xlog         dracut.log  tallylog
anaconda.yum.log      lastlog     wtmp
audit                 maillog     yum.log

//再次查看,rsync.log文件已經生成。
[root@review1 log]# vim rsyncd.log 
 1 018/05/24 16:46:14 [7459] name lookup failed for 192.168.221.130: Name     or service not known
  2 2018/05/24 16:46:14 [7459] connect from UNKNOWN (192.168.221.130)
  3 2018/05/24 16:46:14 [7459] module-list request from UNKNOWN (192.168.2    21.130)
//日誌生成成功!

五、rsync+inotify架構實現數據實時同步

5.1 安裝inotify工具

  • 解壓軟件到指定文件夾
[root@review1 software]# ls
inotify-tools-3.13.tar.gz
[root@review1 software]# tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
  • 配置並安裝
[root@review1 software]# cd /usr/src/inotify-tools-3.13/
[root@review1 inotify-tools-3.13]# ls
aclocal.m4    config.h.in   COPYING     libinotifytools  man      src
AUTHORS       config.sub    depcomp     ltmain.sh        missing
ChangeLog     configure     INSTALL     Makefile.am      NEWS
config.guess  configure.ac  install-sh  Makefile.in      README
[root@review1 inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify

//編譯、安裝
[root@review1 inotify-tools-3.13]# make && make install
  • 檢查安裝情況
[root@review1 inotify-tools-3.13]# cd /usr/local/inotify/
[root@review1 inotify]# ls
bin  include  lib  share
[root@review1 inotify]# cd bin/
[root@review1 bin]# ls
inotifywait  inotifywatch
[root@review1 bin]# cd ../include/
[root@review1 include]# ls
inotifytools
[root@review1 include]# cd ../lib/
[root@review1 lib]# ls
libinotifytools.a   libinotifytools.so    libinotifytools.so.0.4.1
libinotifytools.la  libinotifytools.so.0
[root@review1 lib]# cd ../share/
[root@review1 share]# ls
doc  man

5.2 腳本監測

  • 編寫本地同步腳本
  1 #!/bin/bash
  2 /usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,    attrib,move /dir |while read events  
  3         do
  4                 rsync -a --delete /dir/  /dir1/ ;  //同步dir到dir1
  5                 echo "`date +%F\ %T`出現事件$events" >> /var/log    /rsync.log 2>&1;
  6         done
  7 
  8 # chmod +x 1.sh

注:①/usr/local/inotify/bin/inotifywait inotify監測模塊命令所在路徑

​ ②/dir 監測目錄

​ ③/dir1 備份目錄

​ ④這裏的rsync也可以遠程同步到其他主機,和rsync的遠程同步寫法一樣。

  • 測試
[root@review1 bin]# cd /dir
[root@review1 dir]# ls
file1  file2  file3
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
[root@review1 dir1]# 
[root@review1 dir1]# ls
[root@review1 dir1]# cd /dir
[root@review1 dir]# mkdir test
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
file1  file2  file3  test
//同步成功

//日誌
tail -f /var/log/rsync.log
2018-05-25 09:19:23出現事件/dir/ CREATE,ISDIR test

5.3 錯誤總結

  • inotify監測目錄問題
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,    attrib,move /root/dir |while read events
//注意這裏的監測目錄是/root/dir

//報錯
[root@review1 bin]# ./inotify.sh 
Couldn't watch /root/dir: File name too long

結論:

​ 這裏的File name too long是因爲/root/dir/目錄內部遞歸了太多的目錄,目錄太多,inotify就無法監測了!

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