rsync+inotify两服务器双同步

本文详细介绍在CentOS6.5环境下,如何使用rsync版本3.0.6与inotify版本3.14进行两台主机间的实时文件同步。包括rsync的安装、配置、启动及inotify的安装、配置过程,以及通过测试验证双主机间文件变化的实时同步效果。

环境介绍:

 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值