搭建实时同步系统rsync+inotify
rsync是linux、Unix系统下的文件同步数据传输工具
Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多
192.168.1.6 server
192.168.1.200 clinet
首先配置服务端,安装rsync服务
[root@localhost ~]# yum install rsync
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rsync.i386 0:3.0.6-4.el5_7.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Updating:
rsync i386 3.0.6-4.el5_7.1 base 338 k
Transaction Summary
====================================================================================================================================
Install 0 Package(s)
Upgrade 1 Package(s)
Total download size: 338 k
Is this ok [y/N]: y
Downloading Packages:
rsync-3.0.6-4.el5_7.1.i386.rpm | 338 kB 00:01
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : rsync 1/2
Cleanup : rsync 2/2
Updated:
rsync.i386 0:3.0.6-4.el5_7.1
Complete!
[root@localhost ~]# mkdir /etc/rsyncd //创建rsyncd主目录
[root@localhost ~]# cd /etc/rsyncd/
[root@localhost rsyncd]# vi rsyncd.conf //创建配置文件
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.6
uid =root
gid = root
use chroot = yes
read only = no
hosts allow = *
write only = no
strict modes = yes
motd file = /etc/rsyncd/rsyncd.motd
timeout = 300
transfer logging = yes
log file = /var/log/rsync.log
[ lianxi ]
path = /ceshi
list = false
ignore errors
auth users = tiger
secrets file = /etc/rsyncd/rsyncd.secrets
exclude = error_log httpd.pid
"rsyncd.conf" [New] 21L, 500C written
[root@localhost rsyncd]# ls
rsyncd.conf
[root@localhost rsyncd]# vi rsyncd.secrets
tiger:123456
[root@localhost rsyncd]# chmod 600 rsyncd.sevrets
[root@localhost rsyncd]# /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
//查看服务是否启动
[root@localhost ceshi]# ps -aux|grep rsync
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
root 14274 0.0 0.2 4456 540 ? Ss 22:02 0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
root 14276 0.0 0.2 4164 688 pts/4 R+ 22:02 0:00 grep rsync
OK了,服务端配置OK了,下面开始配置客户端安装inotify
[root@localhost ~]# tar -zxvf inotify-tools-3.14.tar.gz
[root@localhost ~]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure && make && make install
出现下面的东西证明inot已经安装OK了
[root@localhost /]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 37580 05-19 12:09 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 35790 05-19 12:09 /usr/local/bin/inotifywatch
开始配置rsync的客户端
[root@localhost /]# mkdir /etc/rsyncd
You have new mail in /var/spool/mail/root
[root@localhost /]# cd /etc/rsyncd/
[root@localhost rsyncd]# vi rsyncd.password
123456
[root@localhost rsyncd]# chmod 600 rsyncd.password
[root@localhost rsyncd]# touch rsyncd.log
//创建时时同步脚本
[root@localhost ]# vi /root/dstb.sh
#!/bin/bash
host=192.168.1.6
src=/fuwu
dst=lianxi
user=tiger
/usr/local/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/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd/rsyncd.password $src $user@$host::$dst
echo "${files} was rsyncd" >>/etc/rsyncd/rsyncd.log 2>&1
done
[root@localhost ]# /root/dstb.sh &
在fuwu目录中新建个文件,查看效果
root@localhost fuwu]# touch ww.txt
[root@localhost fuwu]#
sending incremental file list
fuwu/
fuwu/aa.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=4/6)
fuwu/bb.txt
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=3/6)
fuwu/ee.txt
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=2/6)
fuwu/ww.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=1/6)
fuwu/yibian.txt
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=0/6)
sent 309 bytes received 107 bytes 277.33 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 126 bytes received 9 bytes 270.00 bytes/sec
total size is 0 speedup is 0.00
ls
aa.txt bb.txt ee.txt ww.txt yibian.txt
然后查看服务端文件都有了,好了一切OK了
在搭建过程中遇到以下问题
问题一:
把rsync服务杀掉之后再启动服务的时候无法启动
[root@localhost ceshi]# /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
failed to create pid file /var/run/rsyncd.pid: File exists
解决方法:
[root@localhost ceshi]# rm -rf /var/run/rsyncd.pid
问题二
在客户端配置好之后,执行同步脚本,发现不行,提示以下错误
[root@localhost fuwu]# touch aa.txt
[root@localhost fuwu]# @ERROR: auth failed on module ceshi
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
@ERROR: auth failed on module ceshi
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
看到这首先想到服务端的rsync服务有问题 ,杀掉rsync服务之后,查看配置文件,发现配置文件中少了个字母,添加上之后还是不行,然后手动执行了同步的命令发现还是不行,然后又回头
排错,最终发现密码的文件名字和配置文件名字不一样,修改完毕后,重启rsync服务,然后执行同步脚本,OK了,大功告成!

浙公网安备 33010602011771号