rsync簡介
rsync有四種應用模式 1、shell本地模式 2、遠程shellmoshi 3、查詢模式 4、服務器模式
1、本地模式用於複製目錄到另一個目錄 rsync -av aa /bb
2、遠程shell模式將本地目錄複製到另外一個系統當中 rsync -av aa 192.168.0.10:bb
3、查詢(列表)模式查看遠程系統中目錄的內容 rsync -a 192.168.0.10:bb
4、服務器模式基於C/S模式 服務器啓用一個後臺守護進程 用於接收或者發送文件
服務器地址
server 192.168.0.101 /web/webdata
web1 192.168.0.103 /web1/webdata
web2 192.168.0.104 /web2/webdata
web3 192.168.0.106 /web3/webdata
1、下載相關軟件包
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
wget https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
在server上配置rsync
touch /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web]
path = /web/webdata/
comment = ixdba file
ignore errors
read only = no
write only = no
hosts allow = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/server.pass
各個參數含義
uid 守護進程具有的用戶ID
gid 守護進程具有的組ID
max connections 最大併發連接量 默認0 表示無限制
strict modes 檢查文件的權限 yes爲檢查口令文件權限 密碼文件的權限必須爲root用戶權限
pid file pid文件路徑
lock file 支持max connections的鎖文件
log file 日誌文件路徑
[webuser] 模塊名稱
path 備份的文件或者目錄路徑
ignore errors 表示忽略I/O錯誤
read only 設置爲no用戶可以上傳文件 YES表示只讀
write only 設置爲no用戶可以下載文件 yes表示不能下載
hosts allow 允許的主機
hosts deny 拒絕連接的主機
list 當客戶請求可使用的模塊列表時,該模塊是否被列出 默認爲true 若要隱藏咋設置爲false
auth users 可以連接該模塊的用戶名
secerts file 用戶:密碼 文件的路徑
創建密碼文件
echo "backup" >> /etc/server.pass
注: 發佈服務器上的密碼文件只設置密碼 節點服務器上的密碼文件需要設置賬戶:密碼。
在server系統上啓動rsync守護進程
/usr/local/bin/rsync --daemon
echo "/usr/local/bin/rsync --daemon" >>/etc/rc.local 將rsync服務添加到自啓動文件中
在web123臺服務器上分別安裝rsync服務
安裝inotify-tools在發佈服務器上(節點服務器無須安裝)
檢測內核是否支持 低於2.6.13則需要重新編譯內核 加入對inotify的支持
uname -r
[root@localhost ~]# ll /proc/sys/fs/inotify
總用量 0
-rw-r--r-- 1 root root 0 2月 27 10:25 max_queued_events
-rw-r--r-- 1 root root 0 2月 27 10:25 max_user_instances
-rw-r--r-- 1 root root 0 2月 27 10:25 max_user_watches
如果有以上三項輸出 就表示系統支持inotify 下面進行安裝
tar -zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure
make
make install
ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 32030 2月 27 10:39 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 33979 2月 27 10:39 /usr/local/bin/inotifywatch
安裝完成後會生成inotifywait和inotifywatch 兩個文件,inotifywait用於等待文件或者文件集上的一個特定時間,可以監控文件和目錄設置 inotifywatch用於收集被監控的文件系統統計數據
在三個節點服務器上配置rsync
web1
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /web1/webdata
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/server.pass
web2
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web2]
path = /web2/webdata
comment = ixdba file
ignore errors
read only = no
write only = no
hosts allow = *
list = false
uid = root
gid = root
auth users = web2user
secrets file = /etc/server.pass
web3
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web3]
path = /web3/webdata
comment = ixdba file
ignore errors
read only = no
write only = no
hosts allow = *
list = false
uid = root
gid = root
auth users = web3user
secrets file = /etc/server.pass
以上配置完三臺節點服務器的rsyncd.conf後 並分別創建密碼文件。後依次啓動rsync守護進程,並將服務加到自啓動文件
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local
配置服務器內容發佈節點
通過編寫shell腳本來實現內容的發佈
#!/bin/bash
host1=192.168.0.103
host2=192.168.0.104
host3=192.168.0.106
src=/web/webdata/
dst1=web1
dst2=web2
dst3=web3
user1=web1user
user2=web2user
user3=web3user
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1
/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$host2::$dst2
/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user3@$host3::$dst3
echo "${files} was rsynced" >> /tmp/rsynclog 2>&1
done
將此腳本放到後臺運行,並加入到自啓動文件
sh /root/rsync.sh &
echo "/root/rsync.sh &">>/etc/rc.local
腳本中相關參數如下
--timefmt 指定時間的輸出格式
--format 指定變化文件的詳細信息
有時候會遇到這樣的情況,發佈服務器的目錄中寫入一個較大的文件,當寫入這個大文件需要一段時間時,inotify會持續不斷的輸出該文件被更新的信息 這樣就會不斷出發rsync執行同步操作,佔用大量系統資源,針對這種情況,最理想的罪罰是等待寫完後在觸發rsync同步,這種情況下,可以修改inotify的監控事件 即-e close_write,delete,create,attrib
測試rsync+inotify實時同步
在server服務器的/web/webdata/目錄中添加或者刪除文件或者目錄,查看節點服務器是否更新。