eg:linux
操做系統:CentOS release 6.5vim
服務器IP:rsync_server(數據源) 192.168.23.27bash
rsync_client (目標端)192.168.23.67服務器
同步目錄: rsync_server /home/publicapp
rsync_client /home/public socket
rsync_client (目標端)192.168.23.67ide
一、安裝Rsync服務端工具
[root@zabbixserver ~]# yum -y xinted rsync^C [root@zabbixserver ~]# cat /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = yes flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID } [root@zabbixserver ~]# cat /etc/rsyncd.conf uid = root gid = root max connections=4 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock secrets file= /etc/rsyncd.passwd hosts allow = 192.168.23.0/24 [www] comment = public path= /home/public read only = no exclude= test auth users= work secrets file= /etc/rsync.passwd 這些是個人配置文件:/etc/xinetd.d/rsync,/etc/rsyncd.conf [root@zabbixserver ~]# cat /etc/rsync.passwd work:123abc [root@zabbixserver ~]# ls -l /etc/rsync.passwd -rw------- 1 root root 12 Sep 27 00:17 /etc/rsync.passwd [root@zabbixserver ~]#service xinetd restart 啓動rsync 服務 rsync --daemon --config=/etc/rsyncd.conf
rsync_server(數據源) 192.168.23.27post
安裝Rsync客戶端測試
[root@localhost rsync_server]# whereis rsync #查看系統是否已安裝rsync rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz #說明已經安裝 [root@localhost rsync_server]#
yum install xinetd #已安裝rsync只安裝xinetd便可,CentOS中是以xinetd來管理rsync服務的
yum install rsync xinetd #若是默認沒有rsync,運行此命令進行安裝rsync和xinetd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost rsync_server]
# vim /etc/xinetd.d/rsync
service
rsync
{
disable = no
#修改成no
flags = IPv6
socket_type = stream
wait = no
user = root
server =
/usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
[root@localhost rsync_server]
# /etc/init.d/xinetd restart
[root@localhost rsync_server]
# vim /etc/passwd.txt
123456
[root@localhost rsync_server]
# chmod 600 /etc/passwd.txt
|
測試
在rsync_server的/home/public目錄下建立文件testfile,在rsync_server端運行同步命令同步數據:
sync -avH --port=873 --progress --delete /home/public/ work@192.168.23.67::www --password-file=/etc/rsync.passwd
[root@rsync etc]# rsync -avH --port=873 --progress --delete /home/public/ work@192.168.23.67::www --password-file=/etc/rsync.passwd sending incremental file list ./ test.txt 77 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2) sent 166 bytes received 36 bytes 404.00 bytes/sec total size is 77 speedup is 0.38
在rsync_server(數據源) 192.168.23.67上安裝Inotify-tools工具,實時觸發rsync進行同步
一、安裝Inotify-tools工
[root@zabbixserver ~]# ll /proc/sys/fs/inotify/max_ max_queued_events max_user_instances max_user_watches
rsync_client
1
2
3
|
[root@localhost rsync_client]
# ls
file
[root@localhost rsync_client]
#
|
在rsync_server(數據源) 10.15.43.100上安裝Inotify-tools工具,實時觸發rsync進行同步
一、安裝Inotify-tools工
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[root@localhost src]
# ll /proc/sys/fs/inotify #查看服務器內核是否支持inotify,出現下面的內容,說明服務器內核支持inotify
total 0
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_queued_events
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_instances
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_watches
[root@localhost src]
# uname -r #Linux下支持inotify的內核最小爲2.6.13
2.6.32-642.el6.x86_64
[root@localhost src]
# tar zxvf inotify-tools-3.14.tar.gz
[root@localhost src]
# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]
# ./configure --prefix=/app/inotify
[root@localhost inotify-tools-3.14]
# make && make install
[root@localhost inotify-tools-3.14]
# vim /etc/profile #設置系統環境變量
export
PATH=
/app/inotify/bin
:$PATH
[root@localhost inotify-tools-3.14]
# source /etc/profile
[root@localhost inotify-tools-3.14]
# echo " /app/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
[root@localhost inotify-tools-3.14]
# ln -s /app/inotify/include /usr/include/inotify
[root@localhost inotify-tools-3.14]
# sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances" #修改inotify默認參數(inotify默認內核參數值過小)
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.inotify.max_queued_events = 16384
fs.epoll.max_user_watches = 201420
[root@localhost inotify-tools-3.14]
# vim /etc/sysctl.conf
fs.inotify.max_user_instances = 65535
fs.inotify.max_user_watches = 99999999
fs.inotify.max_queued_events = 99999999
[root@localhost inotify-tools-3.14]
# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhost inotify-tools-3.14]
#
|
報錯:
若是以上測試都經過,說明inotify實時觸發rsync同步腳本運行正常。
至此,Linux下Rsync+Inotify-tools實現數據實時同步完成。若是要雙向同步能夠把以上反過來部署次。
報錯:
ps:
本人初次使用rsync,如本文有錯誤,歡迎聯繫本人。文中可能出現步驟不完善的狀況,具體安裝流程能夠參考原文博客http://ityunwei2017.blog.51cto.com/7662323/1951362。
本文轉載自博客:http://ityunwei2017.blog.51cto.com/7662323/1951362
rsync參考資料:http://man.linuxde.net/rsync
參考文章:rsync實時同步(搭配inotify-tools) http://blog.csdn.net/liyongming1982/article/details/12271869
http://blog.csdn.net/duguduchong/article/details/8699774