centos 7 编译安装以及配置rsync+inotify 文件实时同步操作记录

准备工作:

服务器A 源文件服务器 192.168.0.1

服务器B 数据备份服务器 192.168.0.2

注意:服务器A修改文件 实时同步到 服务器B, 服务器A和B都需要安装rsync,

并且B服务器的rsync要实时运行保持随时准备接收数据的状态,并且服务器A还需要安装inotify。

一、 安装rsync  、inotify

看一下你服务器有没有安装这两款软件  rpm -aq|grep rsync     rpm -aq|grep inotify

我们尽可能采用编译安装的方式,能学习到更多

下载  rsync-3.0.9.tar.gz 和inotify-tooles-3.14.tar.gz的压缩包,我下载放到了我服务器上的/usr/local/src 目录下,这个随意

  

 二、配置rsync

创建 /home/wwwroot/test 目录  

mkdir /home/wwwroot/test

#修改权限,必须要有读,写,执行权限,否而会失败
chmod 770 /home/wwwroot/test

#修改所属
chown nobody.nobody /home/wwwroot/test

  

 1、在B服务器上新建rsync配置文件

touch /etc/rsyncd.conf  我们放在 /etc目录下

内容如下:

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:

uid = nobody 
gid = nobody
use chroot = no
max connections = 4
log file = /var/log/rsyncd.log #日志记录
lock file = /var/run/rsyncd.lock #锁文件
pid file = /var/run/rsyncd.pid
motd file = /etc/rsyncd.motd

transfer logging = yes

port = 873 #rsync使用的端口
read only = yes #是否允许客户端上传数据,yes 表示不允许

#timeout = 900
#ignore nonreadable = yes
#dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[test]
path = /home/wwwroot/test
auth user = rcyncuser
secrets file = /etc/rsyncd.secrets #用来认证客户端的密钥文件 
#hosts allow = 192.168.0.1 #设置允许那些主机进行同步数据
#hosts deny = * #除了hosts allow定义的主机外,拒绝其他所有
#exclude = // #排除指定目录

  

2、 创建 rsyncd.secrets 文件,用来存放认证A服务器的密钥文件

echo "rcyncuser:123" > /etc/rsyncd.secrets

#设置密钥文件权限
chmod 600 /etc/rsyncd.secrets

#开启rsync
rsync --daemon
echo "rsync --daemon" >> /etc/rc.local

#rsync默认端口是873,设置防火墙永久允许
firewall-cmd --permanent --add-port=873/tcp    

firewall-cmd --reload

  

二、A服务器rsync inotify 配置

1、rsync安装 同上, 可以不用配置 /etc/rsyncd.conf

 2、设置密码文件

echo "123" > /etc/rsyncd.passwd

chmod 600 /etc/rsyncd.passwd

  

现在其实就可以用过rsync命令来查看 B服务器上的组

raync -avz rsyncuser@192.168.0.1::    这样运行后会返回:  test

3、安装 inotify

4、编写安装脚本 inotify.sh 

#!/bin/bash
#export PATH=/bin:/usr/bin:/usr/local/bin

src=/home/wwwroot/www_57yn_cn/uploads/
passfile=/etc/rsync.password
client=192.168.0.2
desc=test

inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' -e modify,create,move,delete,attrib $src | while read line
do
        #if [-f $line];then
                echo "" > /var/log/inotify_test 2>&1
                rsync -avz --delete --progress --password-file=$passfile $src rsyncuser@$client::$desc >> /var/log/sync_test 2>&1
        #else
        #       cd $src && 
        #       rsync -az ./ --delete --password-file=$passfile $src rsyncuser@$client::$desc
     #fi
done

  

执行inotify.sh

sh inotify.sh &  #后台执行

原文地址:https://www.cnblogs.com/murenhui/p/8929840.html