天天看點

rsync遠端同步和配置inotify+rsync遠端同步

一.rsync遠端同步

  1. 服務的啟動停止和安裝

    安裝(預設不需要安裝)

    [[email protected] ~]# yum -y install rsync

    rsync遠端同步和配置inotify+rsync遠端同步

    啟動(rsync預設端口号為873)

    [[email protected] ~]# rsync --daemon

    [[email protected] ~]# netstat -anptu | grep rsync

    rsync遠端同步和配置inotify+rsync遠端同步

    停止

    [[email protected] ~]# killall rsync

    [[email protected] ~]# netstat -anptu | grep rsync

    rsync遠端同步和配置inotify+rsync遠端同步
  2. 備份和配置rsync主配置檔案

    1) 備份主配置檔案

    [[email protected] ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf.bak

    rsync遠端同步和配置inotify+rsync遠端同步

    2) 配置主配置檔案

    [[email protected] ~]# vim /etc/rsyncd.conf 進入主配置檔案中

    7 uid = nobody 管理rsync使用者

    8 gid = nobody 管理rsync組

    9 use chroot = yes 禁锢rsync同步源根目錄

    10 address = 192.168.100.10 監聽IP位址

    11 port = 873 監聽端口号

    12 hosts allow = 192.168.100.0/24 允許100網段通路

    24 [bdqn] 共享子產品名字

    25 path = /bdqn 存儲同步源資料根目錄

    26 omment = test bdqn 同步源說明

    27 read only = yes 設定為隻讀

    28 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 指定檔案類型不再進行壓縮

    29 auth users = bob 驗證賬戶

    30 secrets file = /etc/rsyncd_user.db 設定驗證資料庫

    rsync遠端同步和配置inotify+rsync遠端同步
    rsync遠端同步和配置inotify+rsync遠端同步

    3) 建立同步源根目錄

    [[email protected] ~]# mkdir /bdqn

    rsync遠端同步和配置inotify+rsync遠端同步

    4) 配置驗證賬戶密碼資料庫

    [[email protected] ~]# vim /etc/rsyncd_user.db

    bob:[email protected]

    rsync遠端同步和配置inotify+rsync遠端同步

    5) 配置權限

    [[email protected] ~]# chmod 600 /etc/rsyncd_user.db

    rsync遠端同步和配置inotify+rsync遠端同步

    6) 重新開機服務

    [[email protected] ~]# rsync --daemon

    rsync遠端同步和配置inotify+rsync遠端同步
  3. 使用rsync用戶端同步rsync源資料(使用02測試)

    1) 建立目錄benet和accp和在01上的/bdqn下建立測試檔案

    [[email protected] ~]# mkdir /benet

    [[email protected] ~]# mkdir /accp

    rsync遠端同步和配置inotify+rsync遠端同步

    [[email protected] ~]# cd /bdqn

    [[email protected] bdqn]# touch 1.txt

    [[email protected] bdqn]# touch 2.txt

    [[email protected] bdqn]# touch 2.mp3

    rsync遠端同步和配置inotify+rsync遠端同步

    2) 方式一全部備份

    [[email protected] ~]# rsync -avz [email protected]::bdqn /benet/

    共享子產品為bdqn 同步到/benet下

    rsync遠端同步和配置inotify+rsync遠端同步

    驗證

    [[email protected] ~]# cd /benet

    [[email protected] ~]# ls

    rsync遠端同步和配置inotify+rsync遠端同步

    3) 方式二備份沒有的

    [[email protected] ~]# rsync -avz rsync://[email protected]/bdqn /accp

    rsync遠端同步和配置inotify+rsync遠端同步

    驗證

    [[email protected] ~]# cd /accp

    [[email protected] ~]# ls

    rsync遠端同步和配置inotify+rsync遠端同步
  4. Rsync的格式和常見選項

    1) 格式

    rsync [選項] [同步源] [同步目标]

    2) 常見選項

    -r:遞歸模式同步目錄使用

    -l:複制符号連接配接資料(小寫L)

    -v:顯示同步詳細資訊

    -a:歸檔模式保留檔案的屬性

    -z:傳輸過程中對資料進行壓縮

    -p:保留源檔案的權限标記

    -t:保留源檔案的時間标記

    -g:保留源檔案的屬組标記

    -o:保留源檔案的屬主标記

    -H:儲存硬連接配接檔案

    -A:儲存AVL屬性資訊

    -D:儲存設别檔案以及其他的特殊檔案

    –delete:删除目标位置有而原始位置沒有的檔案

    –checksum:根據校驗和(而不是檔案大小,修改時間)來決定是否跳過檔案配置rsync用戶端定期同步rsync資料

    1) 建立存儲密碼檔案并授權600

    [[email protected]os02 ~]# vim /root/password.pass

    [email protected]

    rsync遠端同步和配置inotify+rsync遠端同步

    2) 建立計劃任務定期同步資料

    [[email protected] ~]# crontab -e 進入計劃任務配置檔案

    */2 * * * * /usr/bin/rsync -az --delete --password-file=/root/password.pass [email protected]::bdqn /accp

    rsync遠端同步和配置inotify+rsync遠端同步

    3) 啟動計劃任務服務并設定開機自啟

    [[email protected] ~]# systemctl start crond

    [[email protected] ~]# systemctl enable crond

    rsync遠端同步和配置inotify+rsync遠端同步

    4) 檢視是否計劃任務備份成功

    在01上添加新的測試檔案

    rsync遠端同步和配置inotify+rsync遠端同步
    驗證2分鐘後是否同步了4.txt
    rsync遠端同步和配置inotify+rsync遠端同步
    crontab -l 檢視配置的計劃任務清單
  5. 配置本地資料同步

    1) 建立一個benet目錄

    rsync遠端同步和配置inotify+rsync遠端同步

    2) 同步bdqn目錄資料及子目錄資料到benet目錄

    [[email protected] ~]# rsync -rl /bdqn/ /benet

    rsync遠端同步和配置inotify+rsync遠端同步

    3) 同步bdqn目錄到benet顯示詳細資訊

    [[email protected] ~]# rsync -rlv /bdqn/ /benet

    rsync遠端同步和配置inotify+rsync遠端同步
    二.配置inotify
  6. 切換CD光牒解壓inotify軟體包

    [[email protected] ~]# mount /dev/cdrom /mnt

    [[email protected] ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src

    rsync遠端同步和配置inotify+rsync遠端同步
  7. 配置inotify服務

    1)進入目錄

    [[email protected] ~]# cd /usr/src/inotify-tools-3.14/

    rsync遠端同步和配置inotify+rsync遠端同步

    2)直接配置

    [[email protected] inotify-tools-3.14]# ./configure

    rsync遠端同步和配置inotify+rsync遠端同步

    3)安裝

    [[email protected] inotify-tools-3.14]# make && make install

    rsync遠端同步和配置inotify+rsync遠端同步

    4)驗證安裝是否成功(支援這倆指令的使用說明安裝成功)

    [[email protected] ~]# inotifywa

    Inotifywait(動态監控系統檔案的變化資訊) inotifywatch(安裝的是服務工具)

    rsync遠端同步和配置inotify+rsync遠端同步

    5)調整核心參數

    [[email protected] ~]# vim /etc/sysctl.conf

    fs.inotify.max_queued_events = 16384 監控隊列

    fs.inotify.max_user_instances = 1024 監控執行個體

    fs.inotify.max_user_watches = 1048576 一次性檔案同步數量大于監控目标總數量

    rsync遠端同步和配置inotify+rsync遠端同步

    6)更新核心參數

    [[email protected] ~]# sysctl -p

    rsync遠端同步和配置inotify+rsync遠端同步
  8. Inotifywait的作用和常見選項和監控事件類型

    1)作用

    監控建立檔案,修改檔案,修改檔案屬性,移動删除資料資訊

    2)常見選項

    -m:持續監控

    -r:遞歸同步目錄資料

    -q:輸出資訊

    -e:指定監控事件

    3)監控事件類型

    create:建立檔案或目錄

    move:移動檔案或目錄

    delete:删除檔案或目錄

    modify:修改檔案目錄資料

    attrib:修改檔案目錄屬性

    4)使用inotifywait開始監控(mv修改 rm -rf 删除 echo修改資料 )

    [[email protected] ~]# inotifywait -mrq -e create,move,delete,modify,attrib /bdqn/

    rsync遠端同步和配置inotify+rsync遠端同步
    在另一個同系統視窗上建立檔案或目錄驗證監控
    rsync遠端同步和配置inotify+rsync遠端同步
    rsync遠端同步和配置inotify+rsync遠端同步
    在第一個視窗檢視是否監控成功
    rsync遠端同步和配置inotify+rsync遠端同步

    5)配置ssh秘鑰對實作無身份驗證scp資料

    擷取rsync伺服器的密鑰對

    [[email protected] ~]# ssh-keygen -t rsa

    rsync遠端同步和配置inotify+rsync遠端同步

    上傳秘鑰(公鑰)到遠端同步目标主機(公加私解)輸入上傳密碼:[email protected]

    [[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]

    rsync遠端同步和配置inotify+rsync遠端同步

    驗證

    [[email protected] ~]# scp /bdqn/1.txt [email protected]:/benet

    rsync遠端同步和配置inotify+rsync遠端同步

    6)編輯資料同步腳本自動同步

    [[email protected] ~]# vim /inotify_rsync.sh

    #!/bin/bash

    INOTIFY_CMD="/usr/local/bin/inotifywait -mrq -e create,move,delete,modify,attrib /bdqn/"

    RSYNC_CMD=“rsync -azH --delete /bdqn/ [email protected]:/benet”

    $INOTIFY_CMD | while read DIRECTORY EVENT FILE 一旦INOTIFY_CMD讀取到資訊執行do後面的指令變量

    do

    $RSYNC_CMD

    done

    rsync遠端同步和配置inotify+rsync遠端同步

    配置執行權限

    [[email protected] ~]# chmod +x /inotify_rsync.sh

    rsync遠端同步和配置inotify+rsync遠端同步

    驗證

    在01建立自動同步到02上

    rsync遠端同步和配置inotify+rsync遠端同步
    rsync遠端同步和配置inotify+rsync遠端同步

    轉到背景運作并設定開機自動運作腳本并給rc.local執行權限

    轉到背景’

    [[email protected] ~]# sh /inotify_rsync.sh &

    rsync遠端同步和配置inotify+rsync遠端同步

    設定開機自動啟動腳本

    [[email protected] bdqn]# vim /etc/rc.d/rc.local

    14 /usr/bin/sh /inotify_rsync.sh &

    rsync遠端同步和配置inotify+rsync遠端同步

    設定權限

    [[email protected] bdqn]# chmod +x /etc/rc.d/rc.local

    rsync遠端同步和配置inotify+rsync遠端同步
    重新開機驗證是否還可以同步資料在01上建立檢視02是否同步
    rsync遠端同步和配置inotify+rsync遠端同步
    rsync遠端同步和配置inotify+rsync遠端同步

繼續閱讀