什么是rsync?

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

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

rsync工作方式:

rsync有三种工作方式:

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

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

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

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

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

详情参考网址:https://www.cnblogs.com/f-ck-need-u/p/7220009.html#auto_id_1

什么是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.pass 1 <span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/web1.pass 4.启动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.local 1 echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local 测试 在web1服务器节点的/web/wwwroot目录下添加、删除、修改文件,然后到web2服务器节点对应目录去查看文件是否跟随发布节点同步变化。