1.1 inotfify是什么?
inotify是一种强大的、细颗粒的、异步的文件系统监控机制,内核从2.6.13起,加入Inotify可以监控文件系统中添加、删除、修改移动等各种事件,利用这个内核接口,就可以监控文件系统下文件的各种变化情况。
1.2 rsync是什么?
是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
一、安装rsync
[root@master ~]# yum install rsync -y
[root@node1 ~]# yum install rsync -y
二、免密登录
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
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:
20:83:81:73:75:a1:8a:1b:de:79:39:91:4d:60:2e:8a root@master
The key's randomart image is:
+--[ RSA 2048]----+
| ....oo. |
|o .ooo. |
| o..+... |
|.....o+. |
|E.. o .S |
|.o. . o |
|.. o + |
| . . |
| |
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.100.1.11
三、安装inotify
安装依赖
[root@master ~]# yum install epel-release -y
[root@node1 ~]# yum install epel-release -y
安装inotify
[root@master ~]# yum install inotify-tools -y
[root@node1 ~]# yum install inotify-tools -y
四、实时同步
master编写脚本
mkdir /scripts/
vi 1.sh
#!/bin/bash
#/usr/local/redis/conf/需要同步的数据目录
inotifywait -mrq /usr/local/redis/conf/ --format "%w%f" -e create,delete,moved_to,close_write | while read line
do
rsync -az --delete /usr/local/redis/conf/ root@192.168.143.11:/usr/local/redis/conf/
done
//运行
/bin/bash /scripts/1.sh &
//开机自启
chmod +x /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
/bin/bash /scripts/1.sh &