rsync+inotify實現實時數據同步

本例中是通過inotify監控客戶端的目錄文件是否發生變化,如發生變化實時的同步到服務端

主機

client 17X.19.23.24
server 1X.237.150.72

監控client端目錄

/export/jenkins_data
/export/yangfan

client端

  • 安裝文件監控工具inotify-tools
    [root@A01-R08-I23-24 Shell]# yum install inotify-tools
  • 安裝rsync
    [root@A01-R08-I23-24 Shell]# yum install rsync
  • 配置rsync客戶端密碼 (此密碼是rsync互相通信的密碼,與系統登錄密碼不是一回事)
    [root@A01-R08-I23-24 Shell]# cat /etc/rsync.password
    2egseZjPc7jJxwa#
  • 配置密碼文件權限
    [root@A01-R08-I23-24 Shell]# chmod 600 /etc/rsync.password
  • 配置監控腳本 (本例中是監控兩個目錄,所以寫了兩個監控腳本,之前嘗試一個腳本中監控兩個目錄會有問題)
    [root@A01-R08-I23-24 Shell]# cat inotify-an.sh
    #!/bin/bash
    /usr/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M’ --format ‘%T %w%f’ -e modify,delete,create,attrib /export/yangfan | while read file
    do
    /usr/bin/rsync -vzrtopg --delete --progress /export/yangfan/ [email protected]::ansible-data --password-file=/etc/rsync.password
    echo “KaTeX parse error: Expected 'EOF', got '&' at position 46: …og/rsync.log 2>&̲1 done [root@…{files} was rsynced” >> /var/log/rsync.log 2>&1
    done
  • 啓動監控腳本
    [root@A01-R08-I23-24 Shell]# nohup sh inotify-an.sh &
    [root@A01-R08-I23-24 Shell]# nohup sh inotify-jen.sh &

server端

  • 安裝rsync
    [root@A01-R15-I150-72-4000255 ~]# yum install rsync
  • 修改rsync配置文件
    [root@A01-R15-I150-72-4000255 ~]# cat /etc/rsyncd.conf
    uid = 0
    gid = 0
    use chroot = no
    max connections = 0
    timeout = 300
    #####################
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    motd file=/etc/rsyncd.motd
    #####################
    [ansible-data]
    path = /export/yangfan/
    read only = false
    list = false
    secrets file = /etc/rsync.password
    #hosts allow = 0.0.0.0/24
    #hosts deny = 0.0.0.0/32
    auth users = root
    #######################
    [jenkins-data]
    path = /export/jenkins_data/
    read only = false
    list = false
    #hosts allow = 0.0.0.0/24
    #hosts deny = 0.0.0.0/32
    auth users = root
    secrets file = /etc/rsync.password
  • 配置密碼文件 (服務端密碼文件裏格式必須是 用戶名:密碼)
    [root@A01-R15-I150-72-4000255 ~]# cat /etc/rsync.password
    root:2egseZjPc7jJxwa#
  • 配置文件權限
    [root@A01-R15-I150-72-4000255 ~]# chmod 600 /etc/rsyncd.conf
    [root@A01-R15-I150-72-4000255 ~]# chmod 600 /etc/rsync.password
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章