inotify+rsync监控实时同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了inotify+rsync监控实时同步相关的知识,希望对你有一定的参考价值。

监控到Centos7-001机器上的网站内容发生变化时,就同步Centos7-001:/var/www/html/目录到Centos7-002上

Centos7-001上所做的操作

  • 安装 rsync
# yum install  rsync -y
  • 生成密钥对

    [[email protected] ~]# ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Created directory ‘/root/.ssh‘.
    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:
    SHA256:3e6TNDwQTTP5BeDBusXs/7Ma/sx7LFVmSpa8PVEDNaY [email protected]
    The key‘s randomart image is:
    +---[RSA 2048]----+
    |           +*++= |
    |          ..++o.+|
    |           =.E oo|
    |         .o.+ *.+|
    |        S .*.o *o|
    |          ..* o.o|
    |           ..* o.|
    |           .+ *.o|
    |            .+oO*|
    +----[SHA256]-----+
  • 传递公钥给 Centos7-002(可查看Centos机器上是否生成 /root/.ssh/authorized_keys文件)
# ssh-copy-id [email protected]
Are you sure you want to continue connecting (yes/no)? yes
[email protected]‘s password: 
  • 安装 inotify-tools 工具
# yum install epel-release -y
# yum install inotify-tools -y
# rpm -q inotify-tools
  • 利用 inotifywait 命令监控本机的 /var/www/html/目录,如发生变化就同步到Centos7-002:/web_backup上(写一个脚本 inotify.sh),在后台运行此脚本(Centos7-002上需要先安装 rsync)
[[email protected] ~]# cat inotify.sh 
#!/bin/bash
dir=/var/www/html/
remote_dir=192.168.195.129:/web_backup
inotifywait -rqm -e modify,create,move,delete $dir | while read a b c
do
    rsync -azP --delete $dir [email protected]$remote_dir
done
  • 在本地上的/var/www/html/创建文件、目录,删除文件、目录,修改文件、目录
    cd /var/www/html/
    cp -f /etc/passwd ./
    mkdir apeng
    mkdir aa
    echo inotify > aa/test.txt

Centos7-002上所做的操作

  • 安装 rsync
# yum install rsync -y
  • 观察本机的 /web-back目录

技术分享图片

以上是关于inotify+rsync监控实时同步的主要内容,如果未能解决你的问题,请参考以下文章

inotify+rsync实现实时同步

inotify +rsync 实时同步

inotify+rsync监控实时同步

inotify+rsync实现实时同步

配置rsync+inotify实时同步

rsync+inotify实时同步