CentOS7之Rsync+Inotify架构实现实时同步文件和文件夹

什么是rsync?html

sync官方网站: https://www.samba.org/ftp/rsync/rsync.htmllinux

rsync是能够实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,能够实现触发式的实时同步。web

rsync工做方式:shell

rsync有三种工做方式:vim

(1).本地文件系统上实现同步。命令行语法格式为上述”Local”段的格式。bash

(2).本地主机使用远程shell和远程主机通讯。命令行语法格式为上述”Access via remote shell”段的格式。服务器

(3).本地主机经过网络套接字链接远程主机上的rsync daemon。命令行语法格式为上述”Access via rsync daemon”段的格式。网络

前二者的本质是经过管道通讯,即便是远程shell。而方式(3)则是让远程主机上运行rsync服务,使其监听在一个端口上,等待客户端的链接。ide

由上面能够知道:rsync能够本地,远程同步文件。能够定时,或者时间间隔可是不能实时!工具

详情参考网址:http://www.javashuo.com/article/p-hgmuxrfn-cm.html

什么是inotity?

Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件,也能够检测整个目录。当检测的对象是一个目录的时候,目录自己和目录里的内容都会成为检测的对象。

此种机制的出现的目的是当内核空间发生某种事件以后,能够当即通知到用户空间。方便用户作出具体的操做。

由上可知道:inotiy用于监听文件夹变化

inotity详情参考:https://blog.csdn.net/longwang155069/article/details/54016789

那么由rsync+inotity 就能够作到实时去同步文件了

web1服务器(内发发布节点:rsync+inotify)
1.安装Inotify

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
</span>

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
</span>
yum install rsync -y
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
tar zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure –prefix=/usr/local/rsync
make
make install

–安装完后,就会产生下面两个命令
/usr/local/bin/inotifywait
/usr/local/bin/inotifywatch

安装完毕后:

vim /etc/rsyncd.conf

建立密码文件
vim /etc/server.pass

web123
vim /etc/server.pass

web123
<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/server.pass
1
<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/server.pass
/usr/bin/rsync --daemon
1
/usr/bin/rsync --daemon
echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local
1
echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local
[root@zabbix-server ~]# ps -ef | grep rsync
root 23846 1 0 15:09 ? 00:00:00 /usr/bin/rsync --daemon
root 23857 10697 0 15:09 pts/0 00:00:00 grep --color rsync
1
2
3
[root@zabbix-server ~]# ps -ef | grep rsync
root 23846 1 0 15:09 ? 00:00:00 /usr/bin/rsync --daemon
root 23857 10697 0 15:09 pts/0 00:00:00 grep --color rsync
编写脚本

vim /web/inotifyrsync.sh
#!/bin/bash
host1=192.168.100.26 //被同步服务器IP
src=/web/wwwroot/
dst1=web1
user1=web1user
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1 > /dev/null 2>&1
echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
done
vim /web/inotifyrsync.sh
#!/bin/bash
host1=192.168.100.26 //被同步服务器IP
src=/web/wwwroot/
dst1=web1
user1=web1user
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1 > /dev/null 2>&1
echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
done

为其指定可执行权限,而后放入后台运行
<span class="hljs-attribute">chmod</span> <span class="hljs-number">755</span> /web/inotifyrsync.sh
/web/inotifyrsync.sh &
1
2
<span class="hljs-attribute">chmod</span> <span class="hljs-number">755</span> /web/inotifyrsync.sh
/web/inotifyrsync.sh &
将脚本加入系统自启动文件
echo <span class="hljs-string">"/web/inotifyrsync.sh &"</span> <span class="hljs-meta">>> </span>/etc/rc.local
1
echo <span class="hljs-string">"/web/inotifyrsync.sh &"</span> <span class="hljs-meta">>> </span>/etc/rc.local

被同步服务器设置

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
yum install rsync -y
</span>

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
yum install rsync -y
</span>
vim /etc/rsyncd.conf

#/etc/rsyncd: configuration file for rsync daemon mode
#See rsyncd.conf man page for more options.
#configuration example:
uid = nobody
gid = nobody
use chroot = yes
max connections = 10
strict mode=yes
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
[web1]
path = /web1/wwwroot
comment = web1 file
ignore errrors
read only=no
write only=no
hosts allow=*
hosts deny=192.168.100.10
list=false
uid=root
gid=root
auth users=web1user
secrets file=/etc/web1.pass

#/etc/rsyncd: configuration file for rsync daemon mode
#See rsyncd.conf man page for more options.
#configuration example:
uid = nobody
gid = nobody
use chroot = yes
max connections = 10
strict mode=yes
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
[web1]
path = /web1/wwwroot
comment = web1 file
ignore errrors
read only=no
write only=no
hosts allow=*
hosts deny=192.168.100.10
list=false
uid=root
gid=root
auth users=web1user
secrets file=/etc/web1.pass
建立密码文件(文件格式:user:pass)
vim /etc/web1.pass
1
vim /etc/web1.pass

修改密码文件权限<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/web1.pass1<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/web1.pass4.启动rsync守护进程/usr/bin/rsync --daemon 1/usr/bin/rsync --daemon 5.加入系统自启动文件echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local1echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local测试在web1服务器节点的/web/wwwroot目录下添加、删除、修改文件,而后到web2服务器节点对应目录去查看文件是否跟随发布节点同步变化。

相关文章
相关标签/搜索