天天看點

rsync+inotify實時同步一、rsync簡介二、rsync+inotify實作遠端同步

rsync檔案實時同步

  • 一、rsync簡介
    • rsync
    • rsync特性
  • 二、rsync+inotify實作遠端同步
    • 1、環境準備
      • ①兩台主機
      • ②建立同步的目錄
    • 2、主機免密登入
      • ①配置免密登入
      • ②驗證免密登入
    • 3、rsync服務安裝介紹
      • ①安裝rsync服務
      • ②啟動服務
      • ③同步測試
    • 4、inotify監控程式安裝介紹
      • ①inotify監控安裝
      • ②監控測試
    • 5、編寫實時同步腳本并測試
      • ①編寫實時同步腳本
      • ② 測試腳本

一、rsync簡介

rsync

rsync(remote synchronize)是linux下一款開源的資料鏡像備份工具。可以使本地的不同分區、不同目錄之間快速實作資料同步,也可實作遠端主機之間的資料快速同步,達到資料備份的效果。同步的檔案可以保持原來檔案的權限、時間、軟硬連結等附加資訊。

rsync特性

①鏡像儲存整個目錄樹和檔案系統

②保持原來檔案的權限、時間、軟硬連結等等

③無需特殊權限即可安裝

④快速:第一次同步時rsync會複制全部内容,但在下一次隻傳輸修改過的檔案。rsync在傳輸的過程可以實時的壓縮和解壓縮操作,是以可以使用更少的帶寬。

⑤安全:可以使用scp、ssh等方式來傳輸檔案,當然也可以通過直接的scket連接配接。

⑥支援匿名傳輸,以友善進行網站鏡像

二、rsync+inotify實作遠端同步

1、環境準備

背景:通過rsync+inotify 同步/test71/目錄到/test72/ 達到快速備份的效果

①兩台主機

主機:192.168.105.71

192.168.105.72

②建立同步的目錄

192.168.105.71上:

192.168.105.72上:

2、主機免密登入

①配置免密登入

192.168.105.71上:

[[email protected] ~]# ssh-keygen
[[email protected] ~]# ssh-copy-id [email protected]
           

②驗證免密登入

3、rsync服務安裝介紹

rsync常用的幾個參數(詳情請man rsync):

-a, –archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgoD。

-v, –verbose 詳細模式輸出。

-z, –compress 對備份的檔案在傳輸時進行壓縮處理。

– --delete 删除目标檔案夾内多餘的文檔。

-q, –quiet 精簡輸出模式。

-r, –recursive 對子目錄以遞歸模式處理。

-R, –relative 使用相對路徑資訊。

-h, –help 顯示幫助資訊。

例如:

rsync --delete -avz /test71/ [email protected]:/test72/

①安裝rsync服務

②啟動服務

[[email protected] ~]# systemctl restart rsyncd
[[email protected] ~]# systemctl enable rsyncd
           

③同步測試

71主機上面:

[[email protected] ~]# cd /test71/
[[email protected] test71]# touch haha.txt
[[email protected] test71]# rsync -avPz --delete --progress /test71/ 192.168.105.72:/test72/
           

觀察72主機目錄變化:

[[email protected] ~]# cd /test72/ ;ls -rlht 
總用量 0
-rw-r--r--. 1 root root 0 11月 21 13:45 haha.txt
           

4、inotify監控程式安裝介紹

inotify

Inotify可用于檢測單個檔案,也可以檢測整個目錄。當檢測的對象是一個目錄的時候,目錄本身和目錄裡的内容都會成為檢測的對象

–用法參數詳解

-e:事件

-d:背景運作

-m:始終保持事件監聽狀态

-q:列印很少的資訊,僅僅列印監控事件的資訊 安靜狀态

-r :遞歸查詢目錄

-timefmt:指定時間輸出的格式

-excluder:排除檔案或者目錄的時候不區分大小寫

①inotify監控安裝

下載下傳inotify-tools-3.13.tar.gz包并上傳到192.168.105.71上:

解包

編譯安裝

[[email protected] ~]# cd /inotify-tools-3.13/
[[email protected] inotify-tools-3.13]# ./configure
[[email protected] inotify-tools-3.13]# make && make install
           

②監控測試

輸入測試程序後,另起一個終端,cd /test71/ ;touch haha1.txt,效果如下:

[[email protected] test71]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /test71/
21/11/19 13:52 /test71/haha1.txt
21/11/19 13:52 /test71/haha1.txt
           

5、編寫實時同步腳本并測試

①編寫實時同步腳本

[[email protected] ~]# vi rsync.sh
#!/bin/bash
host=192.168.105.72
src1=/test71/
src2=/test72/
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src1 | while read file
do
rsync -avPz --delete --progress $src1 $host:$src2
echo "${file} was rsynced" >>/root/rsync.log
done
           

② 測試腳本

使用nohup執行腳本并放入背景,防止終端斷開腳本中段

在71機子上建立檔案

在72機子上檢視效果如下

[[email protected] ~]# ls -rlht /test72/
總用量 0
-rw-r--r--. 1 root root 0 11月 21 13:45 haha.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha1.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha9.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha8.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha7.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha6.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha5.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha4.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha3.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha2.txt
-rw-r--r--. 1 root root 0 11月 23 01:49 haha10.txt
           

繼續閱讀