inotify+rsync監控實時同步

監控到Centos7-001機器上的網站內容發生變化時,就同步Centos7-001:/var/www/html/目錄到Centos7-002上html

Centos7-001上所作的操做web

  • 安裝 rsync
# yum install  rsync -y
  • 生成密鑰對centos

    [root@centos7-001 ~]# 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 root@centos7-001
    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 root@192.168.195.129
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.195.129'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)
[root@centos7-001 ~]# 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 root@$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上所作的操做bash

  • 安裝 rsync
# yum install rsync -y
  • 觀察本機的 /web-back目錄

inotify+rsync監控實時同步

相關文章
相關標籤/搜索