rsync+inotify 双向同步
(注意:双向同步前双方同步的文件一定要一致,否则同步时会出现文件丢失的现象)
环境部署
主机名 主机IP地址 系统版本 系统内核版本
inotify-master 192.168.1.65 CentOS release 6.5 (Final) 2.6.32-642.15.1.el6.x86_64
inotify-master 192.168.1.67 CentOS release 6.5(Final) 2.6.32-642.15.1.el6.x86_64

1、默认系统已经安装rsync,可以查看是否已经安装
[root@test2 home]# rpm -aq |grep rsync
rsync-3.0.6-12.el6.x86_64

2、服务器端需要安装inotify,下载地址

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@test2 home]# tar -xvf inotify-tools-3.14.tar.gz
[root@test2 home]# cd inotify-tools-3.14
[root@test2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14
[root@test2 inotify-tools-3.14]# make && make install

3、服务器需要增加rsyncd.conf文件
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 100
pid file = /var/log/rsyncd.pid
lock file = /var/log/rsyncd.lock
log file = /var/log/rsyncd.log
[backup1]
path = /data/
igonre errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts denoy = 0.0.0.0/32
auth users=rsync_backup
secrets file = /etc/rsyncd.password

4、在root@test2服务器上增加rsyncd.password
vi rsyncd.password
rsync_backup:abcdefg
其中rsync_backup为rsyncd.conf下的auth users
密码可以自己定义
在home目录下添加rsyncpassword
Vi /home/rsyncpassword
abcdefg
chmod 600 /home/rsyncpasswordbr/>5、在root@test2修改rsyncd.password文件的权限
chmod 600 /etc/rsyncd.password

6、在root@test2增加rsync 用户
[root@test2 ~]# useradd rsync -s /sbin/nologin -M
[root@test2~]# cat /etc/passwd
rsync:x:501:501::/home/rsync:/sbin/nologin
7、客户端需要满足以下条件
需要在/proc/sys/fs/inotify/ 有以下文件
[root@test2 inotify-3.14]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 4月 18 10:21 max_queued_events
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_instances
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_watches
8、增加用户
[root@test2 ~]# useradd rsync -s /sbin/nologin -M
[root@test2 ~]#cat /etc/passwd
rsync:x:500:500::/home/rsync:/sbin/nologin
9、创建rsyncd.password
vi rsyncd.password
abcdefg
只需要创建密码即可,且密码与服务端的一致
10、创建文件目录并赋予权限
[root@test2~]# mkdir /data/
[ root@test2 ~]# chown -R rsync.rsync /data/
drwxr-xr-x 2 rsync rsync 4096 4月 18 11:06 data

11、服务器开启rsync服务并查看服务是否启动,使用的端口。
[root@test2 ~]# rsync --daemon
[root@test2 ~]# ps -ef |grep rsync
root 19045 1 0 10:45 ? 00:00:00 rsync --daemon
root 19168 25971 0 10:46 pts/1 00:00:00 grep rsync
[root@test2 ~]# netstat -anplt |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 19045/rsync
tcp 0 0 :::873 :::
LISTEN 19045/rsync br/>12、在root@test4上测试连接
[root@test2 ~]# rsync -avz aa [email protected]::backup1 --password-file=/home/rsyncpassword
rsync: failed to connect to 192.168.1.65: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
出现No route to host (113),一般都为防火墙开启导致
(1)首先用ping 测试与192.168.1.65的联通性
root@test2 ~]# ping 192.168.1.65
PING 192.168.1.67 (192.168.1.65) 56(84) bytes of data.
64 bytes from 192.168.1.65: icmp_seq=1 ttl=64 time=0.744 ms
64 bytes from 192.168.1.65: icmp_seq=2 ttl=64 time=0.206 ms
(2)telnet ,测试端口是否可以通
[root@test2 ~]# telnet 192.168.1.65 873
Trying 192.168.1.67...
telnet: connect to address 192.168.1.65: No route to host
说明873端口不通
(3)在防火墙增加允许873端口,vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
重启防火墙service iptables restart
[root@test4 ~]# service iptables restart
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
(4)telnet ,测试端口是否可以通
[root@test4 ~]# telnet 192.168.1.65 873
Trying 192.168.1.65...
Connected to 192.168.1.65.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> quit
Connection closed.
(5)rsync -avz aa.sh [email protected]::backup1 --password-file=/home/rsyncpassword

[root@test4 home]# rsync -avz aa.sh [email protected]::backup1 --password-file=/home/rsyncpassword
sending incremental file list

sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 912 speedup is 26.82
13、创建inotify.sh
source /etc/profile
host01=192.168.1.67 #inotify-slave的ip地址
src=/data/ #本地监控的目录
dst=backup
user=rsync_backup #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/home/rsyncpassword #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify-3.14 #inotify的安装目录
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do

rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

14、默认系统已经安装rsync,可以查看是否已经安装
[root@test4 home]# rpm -aq |grep rsync
rsync-3.0.6-12.el6.x86_64

15、服务器端需要安装inotify,下载地址

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@test4 home]# tar -xvf inotify-tools-3.14.tar.gz
[root@test4 home]# cd inotify-tools-3.14
[root@test4 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14
[root@test4 inotify-tools-3.14]# make && make install

16、服务器需要增加rsyncd.conf文件
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 100
pid file = /var/log/rsyncd.pid
lock file = /var/log/rsyncd.lock
log file = /var/log/rsyncd.log
[backup]
path = /data/
igonre errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts denoy = 0.0.0.0/32
auth users=rsync_backup
secrets file = /etc/rsyncd.password

17、在root@test4服务器上增加rsyncd.password
vi rsyncd.password
rsync_backup:abcdefg
其中rsync_backup为rsyncd.conf下的auth users
密码可以自己定义
在home目录下添加rsyncpassword
Vi /home/rsyncpassword
abcdefg
chmod 600 /home/rsyncpasswordbr/>18、在root@test4修改rsyncd.password文件的权限
chmod 600 /etc/rsyncd.password

19、在root@test4增加rsync 用户
[root@test4 ~]# useradd rsync -s /sbin/nologin -M
[root@test4~]# cat /etc/passwd
rsync:x:501:501::/home/rsync:/sbin/nologin
20、客户端需要满足以下条件
需要在/proc/sys/fs/inotify/ 有以下文件
[root@test4 inotify-3.14]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 4月 18 10:21 max_queued_events
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_instances
-rw-r--r-- 1 root root 0 4月 18 10:21 max_user_watches
21、增加用户
[root@test4 ~]# useradd rsync -s /sbin/nologin -M
[root@test4 ~]#cat /etc/passwd
rsync:x:500:500::/home/rsync:/sbin/nologin
22、创建rsyncd.password
vi rsyncd.password
abcdefg
只需要创建密码即可,且密码与服务端的一致
23、创建文件目录并赋予权限
[root@test4~]# mkdir /data/
[ root@test4 ~]# chown -R rsync.rsync /data/
drwxr-xr-x 2 rsync rsync 4096 4月 18 11:06 data

11、服务器开启rsync服务并查看服务是否启动,使用的端口。
[root@test4 ~]# rsync --daemon
[root@test4~]# ps -ef |grep rsync
root 19045 1 0 10:45 ? 00:00:00 rsync --daemon
root 19168 25971 0 10:46 pts/1 00:00:00 grep rsync
[root@test2 ~]# netstat -anplt |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 19045/rsync
tcp 0 0 :::873 :::
LISTEN 19045/rsync br/>24、在root@test2上测试连接
[root@test2 ~]# rsync -avz aa [email protected]::backup1 --password-file=/home/rsyncpassword
rsync: failed to connect to 192.168.1.67: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
出现No route to host (113),一般都为防火墙开启导致
(1)首先用ping 测试与192.168.1.67的联通性
root@test2 ~]# ping 192.168.1.67
PING 192.168.1.67 (192.168.1.67) 56(84) bytes of data.
64 bytes from 192.168.1.67: icmp_seq=1 ttl=64 time=0.744 ms
64 bytes from 192.168.1.67: icmp_seq=2 ttl=64 time=0.206 ms
(2)telnet ,测试端口是否可以通
[root@test2 ~]# telnet 192.168.1.67 873
Trying 192.168.1.67...
telnet: connect to address 192.168.1.67: No route to host
说明873端口不通
(3)在防火墙增加允许873端口,vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
重启防火墙service iptables restart
[root@test4 ~]# service iptables restart
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
(4)telnet ,测试端口是否可以通
[root@test4 ~]# telnet 192.168.1.67 873
Trying 192.168.1.67...
Connected to 192.168.1.67.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> quit
Connection closed.
(5)rsync -avz aa.sh [email protected]::backup --password-file=/home/rsyncpassword

[root@test4 home]# rsync -avz aa.sh [email protected]::backup --password-file=/home/rsyncpassword
sending incremental file list

sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 912 speedup is 26.82
25、创建inotify.sh
source /etc/profile
host01=192.168.1.65 #inotify-slave的ip地址
src=/data/ #本地监控的目录
dst=backup1
user=rsync_backup #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/home/rsyncpassword #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify-3.14 #inotify的安装目录
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do

rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

26、分别在两台服务器上启动inotify.sh
/home/inotify.sh &