歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux下兩台服務器文件實時同步方案設計和實現

linux下兩台服務器文件實時同步方案設計和實現

日期:2017/3/1 13:53:33   编辑:關於Linux
linux下兩台服務器文件實時同步方案設計和實現 inotify-toolsrsync同步linux實時同步inotify linux下兩台服務器文件實時同步方案設計和實現 假設有如下需求: 假設兩個服務器: 192.168.0.1 源服務器 有目錄 /opt/test/ 192.168.0.2 目標服務器 有目錄 /opt/bak/test/ 實現的目的就是保持這兩個服務器某個文件目錄保持實時同步 實現方式: 通過rsync+inotify-tools結合來實現 需要安裝軟件: 1. rsync 同步軟件 在 源服務器 和 目標服務器 都需要安裝 源服務器: 是rsync客戶端,不需要配置 目標服務器: 是rsync服務器端,需要配置/etc/rsyncd.conf裡的內容 安裝後需要新建配置文件:/etc/rsyncd.conf 配置文件在: /etc/ 文件內容如下: uid = root gid = root use chroot = no max connections = 10 strict modes = yes pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock log file= =/var/run/rsyncd.log [www] path= /opt/bak/test comment= analyse read only = false hosts allow = * 2. inotify-tools 工具 該工具為文件實時監控工具,需要linux操作系統內核支持,內核支持需要至少版本為2.6.13 檢查操作系統是否支持,執行如下: uname -r 查看版本 返回: 2.6.32-220.4.1.el6.x86_64 則表示版本2.6.32 大於2.6.13,則支持。 執行: ll /proc/sys/fs/inotify total 0 -rw-r--r-- 1 root root 0 Oct 18 12:18 max_queued_events -rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_instances -rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_watches 有三項輸出,則表示默認支持inotify,可以安裝inotify-tools工具. 如果不支持,需要采用新版本的linux操作系統 版本達到要求,就可以安裝了。 安裝inotify-tools後會在相關安裝目錄下生成如下兩個文件: ll /usr/local/bin/ total 88 -rwxr-xr-x 1 root root 44327 Oct 10 15:32 inotifywait -rwxr-xr-x 1 root root 41417 Oct 10 15:32 inotifywatch 則表示安裝成功。 注意: 在 源服務器上需要安裝,目標服務器上不需要安裝inotify。 3. 相關腳本: 在源服務器上新建腳本: inotify_bak.sh #!/bin/bash src=/opt/test/ /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file do /usr/bin/rsync -arzuq $src 192.168.0.2::www/ echo " ${file} was rsynced" >>/opt/soft/log/rsync.log 2>&1 done 注意: 這裡的 www 是在目標服務器/etc/rsyncd.conf裡配置的模塊名稱:[www] 賦予執行權限: chmod +x inotify_bak.sh 然後執行: inotify_bak.sh & 放入後台執行 4. 關於啟動 目標服務器:先啟動rsync後台服務: /usr/bin/rsync --daemon 來源服務器: 執行 inotify_bak.sh & 5. 測試: 在來源服務器目錄中新建目錄和文件,inotify_bak.sh腳本會檢測到,然後同步到目標服務器的相關目錄下 可以查看日志文件: /opt/soft/log/rsync.log 命令如下:觀察實時同步的情況。 tail -f /opt/soft/log/rsync.log
Copyright © Linux教程網 All Rights Reserved