rsync + inotify real-time synchronization configuration

I. Overview

  • Remote Sync, remote synchronization, it is an open source fast backup tool, you can mirror the entire directory tree synchronization between different hosts
  • Incremental backup support, and holding the connection permission, and the use of synchronization algorithms optimized compression is performed before transmission, thus suitable for off-site backup beds, mirrored servers, etc.
  • Support local replication, or synchronized with other SSH, rsync host

Second, the experimental environment Introduction

2 servers,

A client client: 192.168.5.129/24

An rsync server: 192.168.5.130/24

Third, the purpose of the experiment

  • Remote client can get the file rsync server.
  • And monitoring client to get files via inotify mechanism, the preparation of related shell scripts;
  • Let rsync client documents and files in real-time synchronization.

Four, rsync commonly used commands

Here Insert Picture Description

Fifth, the experimental procedure

5.1, rsync server configuration

yum -y install httpd                ##安装web服务器用于测试
yum -y install rsync            ##安装rsync

  • Rsync modify configuration files
uid = nobody
 gid = nobody
 use chroot = yes                            ####禁锢在源目录
 address = 192.168.5.129                   ####监听地址
 port 873                                       ####监听端口号
 log file = /var/log/rsyncd.log            ####日志文件位置
 pid file = /var/run/rsyncd.pid            ####存放进程ID的文件位置
 hosts allow = 192.168.5.0/24              ####允许访问的客户机地址
[wwwroot]                                           ####共享模块名称
 path = /var/www/html                       ####源目录的实际路径
 comment = Document Root of www.51xit.top
 read only =no                                  #####是否只读
 dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z             ####同步时不在压缩的文件类型
 auth users =backuper                               #####授权账户
 secrets file = /etc/rsyncd_users.db                 ####存放账户信息的数据文件

  • Creating authorized account and password
vi /etc/rsyncd_users.db
backuper:pwd123              ###账号backuper密码pwd123
chmod 600 /etc/rsyncd_users.db   ###修改权限
  • Write relevant content for the site in httpd shared client, change site permissions
echo “1111> /var/www/html/111.html
echo “2222> /var/www/html/222.html
chmod 777 /var/www/html
  • Turn off the firewall, start the rsync service
systemctl stop firewalld
rsync --daemon                   ###启动服务,如果要停止这个服务kill $(cat /var/run/rsyncd.pid)
netstat -anpt |grep rsync                   ##检查873端口是否开启
tcp        0      0 192.168.5.129:873       0.0.0.0:*               LISTEN      90024/rsync 

5.2 Client client configuration

  • Create a file folder abc rsync is used to receive,
yum -y install rsync  
mkdir /opt/abc  
systemctl stop firewalld         
  • Abc change folder permissions
useradd -M -s /sbin/nologin backuper   ###创建程序性用户,此用户要和rsync配置中的用户一致

cd /opt/abc

chmod 777 /opt/abc/

chown backuper:backuper abc

  • Add free interaction
vi /etc/server.pass
pwd123
chmod 600 /etc/server.pass             ##修改文件权限
  • Download monitoring software and configure the inotify-tools
yum install gcc gcc-c++ -y             ##安装环境依赖

tar xzvf inotify-tools-3.14.tar.gz -C /opt    ###解压

cd inotify-tools-3.14/

./configure                   ###编译并安装

make && make install
  • Modify the parameters of inotify
vi /etc/sysctl.conf
fs.inotify.max_queued_events = 32768             ###监控队列大小
fs.inotify.max_user_instances = 1024             ###最多监控实例数
fs.inotify.max_user_watches = 1048576          ###每个实例最多监控文件数

sysctl -p  ####重新加载
  • Writing shell scripts to perform synchronization operations
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/abc/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/abc/ [email protected]::wwwroot"
 
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
#       echo "${FILE} was rsynced" >>/opt/inotify_rsync.log

    fi
done
  • Modify the script permissions
chmod +x /opt/inotify.sh 

Sixth, verification

  • Performing synchronization on the remote client, obtaining rsync sharing module [wwwroot] files into folder abc
rsync -az --delete --password-file=/etc/server.pass backuper@192.168.5.129::wwwroot /opt/abc

Here Insert Picture Description

  • Open client inotify monitor, and execute the script /opt/inotify.sh
inotifywait -mrq -e modify,create,move,delete  /opt/abc   ##开启对abc文件夹的监控

cd /opt                                 ##执行shell脚本
./inotify.sh                 

  • Re-open a client terminal, writing documents in / opt / abc in
vi /opt/abc/333.html
333
  • Inotify-tools are able to discover real-time monitoring of the operation of our / opt / abc folder

Here Insert Picture Description

  • Into the rsync server, a shell script has been found by the client in the abc file in sync to the rsync server

Here Insert Picture Description

Published 60 original articles · won praise 66 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42953006/article/details/105061365