rsync+ Notify配置解析及步驟詳解

rsync步驟配置

Rsync介紹

什麼是rsync

rsync是一款開源,快速,多功能的可實現增量的本地或遠程的數據鏡像同步備份的優秀工具。適用於多個平臺。從軟件名稱可以看出來是遠程同步的意思(remote sync)。可使本地主機不同分區或目錄之間及本地和遠程兩臺主機之間的數據快速同步鏡像,遠程備份等功能。

在同步備份時,默認情況下,rsync通過其獨特的“quick check”算法,僅同步大小或者最後修改時間發生變化的文件或目錄(也可根據權限,屬主等變化同步,需要制定參數)。甚至是隻同步一個文件裏變化的內容部分,所以可以實現快速的同步數據的功能。

提示:傳統的cp,scp工具拷貝每次均爲完整拷貝,而rsync除了完整拷貝,還具備增量拷貝的功能,因此從此性能及效率上更勝一籌。

Rsync的特性如下:

1)支持拷貝特殊文件如鏈接,設備等

2)可以有排除指定文件或目錄同步的功能,相當於打包命令tar

3)可以保持原來文件或目錄的權限,時間,軟硬鏈接等所有屬性均不改變。

4)可實現增量同步,即只同步發生變化的數據,因此數據傳輸效率更高

5)可以使用rcp,rsh,ssh等方式來配合傳輸文件,也可以通過直接的socker鏈接

6)支持匿名的或認證的進程模式傳輸,方便進行數據備份及鏡像。

rsync命令格式常見的三種:

1.rsync [OPTION]... SRC DEST
2.rsync [OPTION]... SRC [USER@]HOST:DEST rsync
例:rsync -avz --delete /SRC -e ssh [email protected]:/DEST
需要修改端口時:
rsync -avz --delete /SRC -e "ssh -p2222" [email protected]:/DEST
3.[OPTION]... [USER@]HOST:SRC DEST

rsync的參數說明

-v :詳細輸出
-z :傳輸時進行壓縮以提高傳輸效率。
-a :歸檔模式,表示以遞歸的方式傳輸文件,並保持文件的屬性
--exclude :排除不需要同步傳輸的文件或者目錄
--delete: 讓目標目錄和源目錄的數據一致

Inotify介紹:

Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。
在上面章節中,我們講到,rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。

環境說明

服務類型 IP地址 應用 操作系統
源服務器 192.168.24.248 rsync inotify-tools 腳本 centos7/redhat7
目標服務器 192.168.24.129 rsync centos7/redhat7

需求

*部署rsync+inotify-tools把源服務器上的etc目錄同步到目標服務器上的/linfan/目錄下

在目標服務器上做以下配置

1.關閉防火牆與SELINUX

[root@linfan ~]# systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@linfan ~]# getenforce 0

2.安裝rsync服務端軟件

[root@linfan ~]# yum -y install rsync

3.設置rsyncd.conf配置文件

[root@linfan ~]# vim /etc/rsyncd.conf  //增加以下內容
log file = /var/log/rsyncd.log //日誌文件位置,啓動rsync後自動產生,無需提前創建
pidfile = /var/run/rsyncd.pid //pid文件存放位置
lock file = /var/run/rsync.lock //支持max connections參數的鎖文件
secrets file = /etc/rsync.pass //用戶認證配置文件,裏面存放用戶名稱和密碼,必須手動創建這個文件

 [etc_from_client]  //自定義同步名稱
  path = /linfan/ //rsync服務端存放路徑,客戶端的數據將同步到此目錄
  comment = sync etc from client
  uid = root  //設置rsync運行權限爲root
  gid = root   //設置rsync運行權限爲root
  port = 873    //默認端口爲873
  ignore errors  //表示出現錯誤忽視錯誤
  use chroot = no  //默認爲true ,修改爲no,增加對目錄軟鏈接的備份
  read only = no //設置rsync服務端爲讀寫權限
  list = no  //不顯示rsync服務端資源列表
  max connections = 200 //最大連接數
  timeout = 600  //設置超時時間
  auth users = admin  //執行數據同步的用戶名,可以設置多個,用英文逗號隔開
  hosts allow = 192.168.24.248 //允許進行數據同步的IP地址,可以設置多個,用英文逗號隔開
  hosts deny = 192.168.24.188  ////禁止進行數據同步的IP地址,可以設置多個,用英文逗號隔開

4.創建存放路徑目錄

[root@linfan ~]# mkdir /linfan

5.創建用戶認證文件

[root@linfan ~]# echo 'admin:518' > /etc/rsync.pass
[root@linfan ~]# cat /etc/rsync.pass
admin:518 

6.設置文件權限

[root@linfan ~]# chmod 600 /etc/rsync* 
[root@linfan ~]# ll /etc/rsync*
-rw-------. 1 root root 880 Aug 13 14:54 /etc/rsyncd.conf
-rw-------. 1 root root  10 Aug 13 14:55 /etc/rsync.pass

7.啓動rsync服務並設置開機自啓動

[root@linfan ~]# systemctl start rsyncd
oot@linfan ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

在源服務器上做以下部署:

1.關閉防火牆與SELINUX

[root@linfan ~]#  systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]#  sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@linfan ~]# setenforce 0 

2.配置yum源

[root@linfan ~]# cd /etc/yum.repos.d/ 
[root@linfan yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Bas e-163.repo 
[root@linfan yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@linfan yum.repos.d]#  sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@linfan ~]# yum -y install epel-release //安裝過程略
[root@linfan ~]# yum -y update --skip-broken //升級過程略
安裝rsync服務端軟件,只需要安裝,不要啓動,不需要配置
[root@linfan ~]# yum -y install rsync

3.創建認證密碼文件

[root@linfan ~]# echo '518' > /etc/rsync.pass
[root@linfan ~]# cat /etc/rsync.pass
518

4.設置文件權限,只設置文件所有者具有讀取、寫入的權限

[root@linfan ~]# chmod 600 /etc/rsync.pass
[root@linfan ~]# ll /etc/rsync.pass
-rw-------. 1 root root 0 Aug 13 15:46 /etc/rsync.pass

5.在源服務器上創建測試目錄,然後在源服務器上運行以下命令

[root@linfan ~]# mkdir -pv /root/etc/test
rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
./
test/

sent 69 bytes  received 22 bytes  182.00 bytes/sec
total size is 0  speedup is 0.00

6.運行完成後在目標服務器上查看,在/linfan/目錄下有test目錄,說明數據同步成功

[root@linfan ~]# ls /linfan
test

7.安裝inotify-tools工具,實時觸發rsync同步

//檢查服務器內核是否支持inotify
[root@linfan ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_queued_events
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_user_instances
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_user_watches
//如果有這三個max開頭的文件則表示服務器內核支持inotify

//安裝inotify-tools
[root@linfan ~]# yum -y install make gcc gcc-c++ inotify-tools   

8.寫同步腳本

[root@linfan ~]# mkdir /scripts
[root@linfan ~]# touch /scripts/inotify.sh
[root@linfan ~]# chmod 755 /scripts/inotify.sh
[root@linfan ~]# ll
total 8
drwxr-xr-x. 3 root root   18 Aug 13 15:48 ad
-rw-------. 1 root root 1309 Jul 11 15:50 anaconda-ks.cfg
-rw-r--r--. 1 root root   71 Jul 18 17:16 doudou.repo
drwxr-xr-x. 3 root root   18 Aug 13 15:48 etc
[root@linfan ~]# vim /scripts/inotify.sh 
 host=192.168.24.129 //目標服務器的ip(備份服務器)
src=/etc //在源服務器上所要監控的備份目標
des=etc_from_client //自定義的模塊名,需要與目標服務器上的定義名稱同步
password=/etc/rsync.pass //執行數據同步的密碼文件
user=admin  //執行數據同步的名
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ; do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//檢查腳本

[root@linfan ~]# bash -x /scripts/inotify.sh

//啓動腳本

[root@linfan ~]#  nohup bash /scripts/inotify.sh &
[root@linfan ~]# ps -ef|grep inotify

//在源服務器上生成一個新的文件

[root@linfan ~]# mkdir /etc/httpd24/
[root@linfan ~]# echo 'hello world' > /etc/httpd24/test

//查看inotify生成的日誌

[root@linfan ~]# tail /tmp/rsync.log
20180810 14:59 /etc/httpd24/testCREATE was rsynced
20180810 14:59 /etc/httpd24/testMODIFY was rsynced
//從日誌上可以看到,生成了一個test文件,並且添加內容到其中

9.設置腳本開機自動啓動

[root@linfan ~]# chmod +x /etc/rc.d/rc.local
[root@linfan ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Apr 11 15:36 /etc/rc.d/rc.local
[root@linfan ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@linfan ~]# tail  /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

10.到目標服務器上查看是否把新生成的文件自動傳上去了

[root@linfan linfan]# pwd
/linfan
[root@linfan linfan]# ls
etc  test
[root@linfan ~]# cd /etc/http24/
[root@linfan http24]# cat test
hello world
//在源服務器上的內容已經同步目錄上了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章