环境介绍:
Linux版本:CentOS6.5
rsync版本:3.0.6
inotify版本:3.14
主机1IP:192.168.213.131 //绿色代表
主机2IP:192.168.213.134 //红色代表
压缩包位置:/opt/tools
安装位置:/usr/local/rsync /usr/local/inotify
配置文件:/etc/rsync/rsyncd.conf
用户密码文件:/etc/rsync/rsync.password
密码文件:/etc/rsync/rsync.pass
主机1安装配置
一、rsync安装配置
1.用yum安装rsync远程同步
yum -y install rsync
2.从rsync默认安装目录复制一份安装文件到/usr/local/rsync(这步可以省略,只是为了不混乱)
cp -r /usr/share/doc/rsync-3.0.6 /usr/local/rsync
3.建立主机1配置文件和密码文件
1)主机1的rsync同步用户名和密码
touch /etc/rsyncd/rsync.password
echo "rsync_test:123456" >> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password
主机2的rsync同步密码
touch /etc/rsyncd/rsync.pass
echo "123456" >> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.pass
2)vim /etc/rsyncd/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
[data]
path= /data/
read only = no
auth users= rsync_test
secrets file= /etc/rsyncd/rsync.password
4.启动rsync并设置开机启动
rsync --daemon
或者/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
二、inotify的安装和配置
1.获取inotify的压缩安装包
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
2.解压并且安装inotify
tar -zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install
3.创建inotify的启动脚本并授权
vim /usr/lcoal/inotify/inotify.sh
#!/bin/bash
#
host=192.168.213.134 //主机2的IP
data_dir=/data //要同步的本地文件夹
dst=tmp //主机2配置文件中的模块名称
username=rsync_sy //主机2配置文件中的用户名称
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $data_dir |while read files
do
rsync -avz -R --delete ./ --timeout=100 --password-file=/etc/rsync/rsync.pass $username@$host::$dst >/dev/null 2>&1
done
chmod 755 inotify.sh
4.启动脚本,并在后台运行
sh inotify.sh &
关闭:
kill -9 `ps -ef|grep inotify |awk -F ' ' '{print $2}'`
主机2安装配置
一、rsync安装配置
1.用yum安装rsync远程同步
yum -y install rsync
2.从rsync默认安装目录复制一份安装文件到/usr/local/rsync(这步可以省略,只是为了不混乱)
cp -r /usr/share/doc/rsync-3.0.6 /usr/local/rsync
3.建立主机2配置文件和密码文件
1)主机2的rsync同步用户名和密码
touch /etc/rsyncd/rsync.password
echo "rsync_sy:123456" >> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.password
主机1的rsync同步密码
touch /etc/rsyncd/rsync.pass
echo "123456" >> /etc/rsyncd/rsync.password
chmod 600 /etc/rsyncd/rsync.pass
2)vim /etc/rsyncd/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
[tmp]
path= /tmp/test
read only = no
auth users= rsync_sy
secrets file= /etc/rsyncd/rsync.password
4.启动rsync并设置开机启动
rsync --daemon
或者/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
二、inotify的安装和配置
1.获取inotify的压缩安装包
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
2.解压并且安装inotify
tar -zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install
3.创建inotify的启动脚本并授权
vim /usr/lcoal/inotify/inotify.sh
#!/bin/bash
#
host=192.168.213.131 //主机1的IP
data_dir=/tmp/test //要同步的本地文件夹
dst=data //主机1配置文件中的模块名称
username=rsync_test //主机1配置文件中的用户名称
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $data_dir |while read files
do
rsync -avz -R --delete ./ --timeout=100 --password-file=/etc/rsync/rsync.pass $username@$host::$dst >/dev/null 2>&1
done
chmod 755 inotify.sh
4.启动脚本,并在后台运行
sh inotify.sh &
关闭:
kill -9 `ps -ef|grep inotify |awk -F ' ' '{print $2}'`
双同步测试
一、主机1上创建一个文件,再在主机2上查看
1.查看主机1原本有多少文件
[root@server data]# ll
total 356
-rw-r--r-- 1 root root 0 Aug 8 17:24 1
-rw-r--r-- 1 root root 0 Aug 8 17:25 2
-rwxr-xr-x 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r-- 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz
2. 查看主机2原本有多少文件
[root@CentOS test]# ll
total 356
-rw-r--r--. 1 root root 0 Aug 8 17:24 1
-rw-r--r--. 1 root root 0 Aug 8 17:25 2
-rwxr-xr-x. 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r--. 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz
3.在主机1上新建一个文件
[root@server data]# touch a
[root@server data]# ll
total 356
-rw-r--r-- 1 root root 0 Aug 8 17:24 1
-rw-r--r-- 1 root root 0 Aug 8 17:25 2
-rw-r--r-- 1 root root 0 Aug 8 18:17 a
-rwxr-xr-x 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r-- 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz
4.在主机2上查看是否增加文件
[root@CentOS test]# ll
total 356
-rw-r--r--. 1 root root 0 Aug 8 17:24 1
-rw-r--r--. 1 root root 0 Aug 8 17:25 2
-rw-r--r--. 1 root root 0 Aug 8 18:17 a
-rwxr-xr-x. 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r--. 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz
5.在主机2上新建一个文件
[root@CentOS test]# touch b
[root@CentOS test]# ll
total 356
-rw-r--r--. 1 root root 0 Aug 8 17:24 1
-rw-r--r--. 1 root root 0 Aug 8 17:25 2
-rw-r--r--. 1 root root 0 Aug 8 18:17 a
-rw-r--r--. 1 root root 0 Aug 8 18:18 b
-rwxr-xr-x. 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r--. 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz
6.在主机1上查看是否增加文件
[root@server data]# ll
total 356
-rw-r--r-- 1 root root 0 Aug 8 17:24 1
-rw-r--r-- 1 root root 0 Aug 8 17:25 2
-rw-r--r-- 1 root root 0 Aug 8 18:17 a
-rw-r--r-- 1 root root 0 Aug 8 18:18 b
-rwxr-xr-x 1 root root 372 Aug 8 15:18 inotify.sh
-rw-r--r-- 1 root root 358772 Aug 8 15:18 inotify-tools-3.14.tar.gz