(一)環境準備:RHEL6.6
服務器IP:
rsync_server(數據源):192.168.26.130
rsync_client(客戶端):192.168.26.131vim
同步目錄:
rsync_server:/app/rsync_server
rsync_client:/app/rsync_clientbash
(二)安裝及配置rsync
client客戶端配置
1.安裝rsync
# yum install rsync xinetd -y
# cp /etc/xinetd.d/rsync{,.bak}---進行rsync文件的備份
# vim /etc/xinetd.d/rsync---編輯文件
------------------------
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
------------------------
# /etc/init.d/xinetd start---將服務啓動
2.配置rsync
# vim /etc/rsyncd.conf---本身建立一個文件,官方沒有給模板
------------------------
logfile = /var/log/rsyncd.log #日誌文件位置,啓動rsync後自動產生這個文件,無需提早建立
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lockfile = /var/run/rsyncd.lock #支持max connections參數的鎖文件
secretsfile = /etc/rsync.pass #用戶認證配置文件,裏面保存用戶名稱和密碼,後面會建立這個文件
motdfile = /etc/rsyncd.Motd #rsync啓動時歡迎信息頁面文件位置(文件內容自定義)
[app_rsync_client] #自定義名稱
path = /app/rsync_client/ #rsync服務端數據目錄路徑
comment = app_rsync_client #模塊名稱與[app_rsync_client]自定義名稱相同
uid = root #設置rsync運行權限爲root
gid = root #設置rsync運行權限爲root
port 873
use chroot = no #默認爲true,修改成no,增長對目錄文件軟鏈接的備份
read only = no #設置rsync服務端文件爲讀寫權限
list = no #不顯示rsync服務端資源列表
mac connections = 200
timeout = 600
auth user = rsync #執行數據同步的用戶名,能夠設置多個,用英文狀態下逗號隔開
host allow = 192.168.26.131 #容許進行數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開
host deny = 192.168.26.45,192.168.26.46 #禁止數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開,先容許後拒絕
------------------------
3.配置rsync同步的帳戶密碼
# vim /etc/rsync.pass
------------------------
rsync:123456
------------------------
4.啓動rsync
# chmod 600 /etc/rsyncd.conf
# chmod 600 /etc/rsync.pass
# /etc/init.d/xinetd restart---賦權啓動rsync
# service iptables stop---關閉客戶端的防火牆
# setenforce 0---將客戶端的SELinux設置爲Permissive服務器
server服務端配置
1.安裝rsync
# yum install rsync xinetd -y
# cp /etc/xinetd.d/rsync{,.bak}
# vim /etc/xinetd.d/rsync---編輯文件
------------------------
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
------------------------
2.配置rsync同步的帳戶密碼
# vim /etc/passwd.txt
------------------------
123456
------------------------
# chmod 600 /etc/passwd.txt
3.測試手動同步
# mkdir -pv /app/rsync_server/&&touch /app/rsync_server/test.txt---在服務器端建立/app/rsync_server目錄,並在目錄中建立test.txt文件
# service iptables stop---關閉服務器端的防火牆
# setenforce 0---將服務器端的SELinux設置爲Permissive
# rsync -avH --port=873 --progress --delete /app/rsync_server/ rsync@192.168.26.131::app_rsync_client --password-file=/etc/passwd.txt
rsync目標端rsync服務端配置的用戶名,app_rsync_client目標端rsync服務端配置的模塊名稱app
client客戶端驗證
1.驗證:
# ls /app/rsync_client---此時在客戶端發現已經產生了從服務器端傳輸過來的test.txt文件了
2.其餘
# rsync --daemon
這個也能夠經過編輯# vim /etc/rc.d/rc.local,將rsync加入實現開機自動啓動socket
(三)安裝inotify-tools實現實時觸發rsync進行同步
server服務端配置
1.下載安裝inotify-tools,記住須要使用epel6的源:
# yum install inotify-tools -y
# rpm -qa inotify-tools---查看其程序是否安裝成功
# rpm -ql inotify-tools---查看程序包含的文件
2.配置inotify-tools:
# sysctl -a | egrep -i "max_queued_events|max_user_watches|max_user_instances"---查詢顯示inotify相關的系統參數
# vim /etc/sysctl.conf---編輯sysctl.conf文件
------------------------
fs.inotify.max_queued_events = 99999999 #inotify隊列最大長度,若是值過小,會出現"** Event Queue Overflow **"錯誤,致使監控文件不許確
fs.inotify.max_user_watches = 99999999 #要同步的文件包含多少目錄,能夠用:find /app/rsync_server/ -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/app/rsync_server/爲同步文件目錄)
fs.inotify.max_user_instances = 65535 #每一個用戶建立inotify實例最大值
------------------------
# sysctl -p---參數當即生效
3.建立實時同步腳本:
# mkdir -p /usr/local/inotify/---建立inotify目錄
# vim /usr/local/inotify/rsync.sh---建立一個rsync.sh的腳本文件
------------------------
#!/bin/bash
src_dir="/app/rsync_server/" #源服務器同步目錄
dst_dir="app_rsync_client" #目標服務器rsync同步目錄模塊名稱
exclude_dir="/usr/local/inotify/exclude.list" #不須要同步的目錄,若是有多個,每一行寫一個目錄,使用相對於同步模塊的路徑。例如:不須要同步/app/rsync_server/"目錄下的a目錄和b目錄下面的b1目錄
rsync_user="rsync" #目標服務器rsync同步用戶名
rsync_passwd="/etc/passwd.txt" #目標服務器rsync同步用戶的密碼在源服務器的存放路徑
dst_ip="192.168.26.131" #目標服務器ip,多個ip用空格分開
rsync_command(){
rsync -avH --port=873 --progress --delete --exclude-from=$exclude_dir $src_dir $rsync_user@$ip::$dst_dir --password-file=$rsync_passwd
}
for ip in $dst_ip;do
rsync_command
done
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $src_dir \
| while read file;do
for ip in $dst_ip;do
rsync_command
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
done
done
------------------------
4.賦權並添加開機啓動
# chmod +x /usr/local/inotify/rsync.sh---添加可執行權限
# touch /usr/local/inotify/exclude.list---建立腳本中定義的exclude.list文件
# vim /etc/rc.d/rc.local
------------------------
nohup /bin/sh /usr/local/inotify/rsync.sh &
------------------------
5.測試
重啓192.168.26.130服務器端後
# cd /app/rsync_server/
# touch test{2..9}ide
client客戶端驗證
此時咱們在/app/rsync_client目錄下即可以看到服務器端實時生成的文件測試