rsync_innotify实现文件双向同步

主机:
192.168.40.105   host5
192.168.40.106   host6

系统
centos 6.8

必备的软件包
inotify-tools-3.14.tar.gz
http://pan.baidu.com/s/1nvSAs1F

首先实现双机互信
host5
生成密钥对
[root@test5 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
46:e0:39:fc:e6:d6:d0:6c:72:f6:3b:fc:0f:1e:34:5b root@test5
The key's randomart image is:
+--[ RSA 2048]----+
|      .          |
|     o o         |
|      = .        |
|       + o       |
|        S *   o E|
|       + B . . + |
|        o ... +  |
|       .    oo o |
|            .oo..|
+-----------------+
把公钥复制到远程主机
[root@test5 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.106
The authenticity of host '192.168.40.106 (192.168.40.106)' can't be established.
RSA key fingerprint is 52:69:48:c4:70:3c:9c:91:c1:c2:c5:2b:c2:29:30:49.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.106' (RSA) to the list of known hosts.
root@192.168.40.106's password:
Now try logging into the machine, with "ssh '192.168.40.106'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
测试,看是否不用输入密码直接执行远程命令
[root@test5 ~]# ssh 192.168.40.106 'hostname'
test6

host6
同样执行实现密钥进行认证
[root@test6 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
54:b3:69:7d:94:33:6c:73:9a:e8:6f:b4:cf:e6:87:b3 root@test6
The key's randomart image is:
+--[ RSA 2048]----+
|          o  ... |
|         . = .B .|
|        . + .o.B |
|       . .  ..o  |
|        S  .     |
|            . .  |
|             o o |
|              *.o|
|             .E*=|
+-----------------+
[root@test6 ~]# ssh-copy-id -i .ssh/id_rsa.pub 192.168.40.105
The authenticity of host '192.168.40.105 (192.168.40.105)' can't be established.
RSA key fingerprint is c4:72:2a:50:9e:b6:9d:4b:86:0e:4d:e6:c5:e8:50:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.40.105' (RSA) to the list of known hosts.
root@192.168.40.105's password:
Now try logging into the machine, with "ssh '192.168.40.105'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@test6 ~]# ssh 192.168.40.105 'hostname'
test5

保持两主机时间一致,通过ntpdate命令同步一台时间服务器
两主机都执行
ntpdate NTP_SERVER

host5上
通过脚本自动化安装
rsync各参数和选项使用介绍
rsync --help
rsync是远程文件同步工具,有多种工作模式,详细使用可通过man rsync查看
将rsync以服务的方式启动
chkconfig rsync on启动rsync
rsync是受xinetd管理的,需要启动xinetd,只输出错误信息,正常输出都重定向到/dev/null中
SRC是源目录
DES是目标目录
HOST为目标主机

安装脚本为:
# cat agentd.sh
#!/bin/bash

SRC="/data/static/"
DES="/data/static/"
HOST=192.168.40.106

yum install -y xinetd 1> /dev/null
chkconfig rsync on

cat > /etc/rsyncd.conf << EOF
# Global setting
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log

# Directory to be synced
[tools]
path = /data/static
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.40.106
hosts deny = *
list = false
uid = root
gid = root
EOF

service xinetd start

#sync directory
rsync -vzrtopg --progress ${SRC} root@${HOST}:${DES} 1> /dev/null

运行脚本
bash agentd.sh

host6上
通过自动化安装
安装脚本为:
# cat agentd.sh
#!/bin/bash

SRC="/data/static/"
DES="/data/static/"
HOST=192.168.40.105

yum install -y xinetd 1> /dev/null
chkconfig rsync on

cat > /etc/rsyncd.conf << EOF
# Global setting
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log

# Directory to be synced
[tools]
path = /data/static
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.40.105
hosts deny = *
list = false
uid = root
gid = root
EOF

service xinetd start

#sync directory
rsync -vzrtopg --progress ${SRC} root@${HOST}:${DES} 1> /dev/null

运行脚本
bash agentd.sh

host5上
通过自动化安装
inotify工具能监控目录下各文件属性是否变化,当发生改变时,会执行脚本配置的命令,通过配置脚本同步使用rsync命令远程同步文件。要实现双向同步,则再另外一台主机上也部署好inotify工具,监控好对应的目录,即可实现文件的双向同步,从而实现文件共享。
inotifywait各参数和选项使用介绍
inotifywait --help
安装脚本为:
# cat server.sh
#!/bin/bash
yum install -y automake  gcc  gcc-c++  libtool 1> /dev/null

HOST=192.168.40.106
SRC="/data/static/"
DES=tools
wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master
unzip master
cd inotify-tools-master/
./autogen.sh 1> /dev/null
./configure 1> /dev/null
make 1> /dev/null && make install 1> /dev/null

cd ~
cat > rsync_inotify.sh << EOF
#!/bin/bash

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
  /usr/bin/rsync -vzrtopg --delete --progress ${SRC} root@${HOST}::${DES} 1> /dev/null
  echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash rsync_inotify.sh &

运行脚本
bash server.sh

 

[root@test5 src]# ps aux | grep rsync
root      9667  0.0  0.0 106072  1296 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root      9669  0.0  0.0 106072   492 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root      9672  0.0  0.0 103312   876 pts/0    S+   18:22   0:00 grep rsync

 

host6上
通过自动化安装
安装脚本为:
# cat server.sh
#!/bin/bash
yum install -y automake  gcc  gcc-c++  libtool 1> /dev/null

HOST=192.168.40.105
SRC="/data/static/"
DES=tools
wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master
unzip master
cd inotify-tools-master/
./autogen.sh 1> /dev/null
./configure 1> /dev/null
make 1> /dev/null && make install 1> /dev/null

cd ~
cat > rsync_inotify.sh << EOF
#!/bin/bash

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib ${SRC} | while read file
do
  /usr/bin/rsync -vzrtopg --delete --progress ${SRC} root@${HOST}::${DES} 1> /dev/null
  echo "\${file} was rsynced" >> /tmp/rsync.log 2>&1
done
EOF

bash rsync_inotify.sh &

运行脚本
bash server.sh

 

[root@test6 src]# ps aux | grep rsync
root     20202  0.0  0.0 106072  1292 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root     20204  0.0  0.0 106072   488 pts/0    S    18:21   0:00 bash rsync_inotify.sh
root     28661  0.0  0.0 103316   836 pts/0    S+   18:22   0:00 grep rsync

 

参考链接:
# http://www.cnblogs.com/linuxzkq/p/rsync-inotify.html
# http://www.2cto.com/os/201211/172046.html
# http://www.2cto.com/os/201211/172046.html 

 

测试:
新增文件
[root@test5 src]# ls /data/static/
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'
[root@test5 src]# cp -a /etc/init /data/static/
[root@test5 src]# ls /data/static/
init
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'
init
[root@test5 src]# ls /data/static/init/
ck-log-system-restart.conf  init-system-dbus.conf   quit-plymouth.conf  rcS-sulogin.conf                 serial.conf
ck-log-system-start.conf    kexec-disable.conf      rc.conf             readahead-collector.conf         splash-manager.conf
ck-log-system-stop.conf     plymouth-shutdown.conf  rcS.conf            readahead.conf                   start-ttys.conf
control-alt-delete.conf     prefdm.conf             rcS-emergency.conf  readahead-disable-services.conf  tty.conf
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/init'
ck-log-system-restart.conf
ck-log-system-start.conf
ck-log-system-stop.conf
control-alt-delete.conf
init-system-dbus.conf
kexec-disable.conf
plymouth-shutdown.conf
prefdm.conf
quit-plymouth.conf
rc.conf
rcS.conf
rcS-emergency.conf
rcS-sulogin.conf
readahead-collector.conf
readahead.conf
readahead-disable-services.conf
serial.conf
splash-manager.conf
start-ttys.conf
tty.conf

删除文件
[root@test5 src]# ls /data/static/
init
[root@test5 src]# rm -rf /data/static/init
[root@test5 src]# ls /data/static/
[root@test5 src]# ssh 192.168.40.106 'ls /data/static/'

更多测试可以自己实验

转载于:https://my.oschina.net/u/1762991/blog/835740

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值