search
HomeOperation and MaintenanceLinux Operation and MaintenanceShare rsync+inotify real-time synchronization example tutorial

1.rsync

Compared with the traditional cp, tar backup methods, rsync has the advantages of high security, fast backup, and supports incremental backup. rsync can solve data backup needs that do not require high real-time performance, such as Regularly back up file server data to remote servers, regularly perform data mirroring on local disks, etc.

As the scale of application systems continues to expand, higher requirements are put forward for data security and reliability, rsync is also gradually exposed in high-end businesses There are many shortcomings. First of all, during rsync real-time synchronization, all files need to be scanned for comparison and differential transmission. If the number of files reaches millions or even tens of millions, scanning all files is very time-consuming, and only a small part of them are changing, which is a very inefficient way. Secondly, rsync cannot monitor and synchronize data in real time. Although it can perform start synchronization through the linux daemon, there will be a time difference between the two start actions, which will lead to inconsistencies between the server and the client, making it impossible to completely recover data in the event of an application failure. . Based on the above reasons, rsync+inotify appeared!

2.inotify (monitoring)

inotify is a powerful, fine-grained, asynchronous file System event monitoring mechanism. Through inotify, you can monitor various subtle events such as addition, deletion, modification, and movement in the file system. Using this kernel interface, third-party software can monitor various events under the file system. The situation changes, and inotify-tools is such a third-party software.

1. Server configuration (only need to install rsync)

Share to /tmp/:

1.Add virtual useruseradd rsync -s /sbin/nologin

chown -R rsync.rsync /tmp/

2.Configurationrsyncd.confConfiguration file

vim rsyncd.conf

#rsync_config_config_start

#rsyncd.conf start

##uid = rsync(user)

gid = rsync(user)

use chroot = no (to prevent security issues)

max connections = 200 (how many clients can connect to my backup server)

timeout = 300 (timeout, disconnect the connection after no action for a long time)

pid file = /var/run/rsyncd.pid (process number, Put the process number in this file)

lock file = /var/run/rsync.lock (equivalent to the concept of "lock", the concept of locking the door in the toilet)

log file = /var/log/rsyncd.log (an error occurred, you can view the log file)

[tmp](module)

path = /tmp/(path)

ignore errors

read only = false (read-only means false, readable and writable)

list = false (not allowed list)

hosts allow = 10.0.0.0/24 (allowed hosts)

hosts deny = 0.0.0.0/32 (denied hosts)

auth users = rsync_backup (support virtual users)

secrets file = /etc/rsync.password (user’s corresponding password file)

#rsync_config_config_______________end

3. Create a password file

echo “rsync_backup:123456” >/etc/rsync.password

All password files600Permissionschmod 600 /etc/rsync.password

4 .rsync --daemon daemonmode startup

5.Addrsync --daemon/etc/rc.local

echo “/ usr/bin/rsync --daemom” >>/etc/rc.local

2. Client configuration

Installationrsyncandinotify

1.Installation rsyncyumJust install it)

2. Create a password authentication file

echo “123456” >/etc/rsync.password Only password required

Set permissions600 chmod 600 /etc/rsync.password

3.Installationinotify

cd /home/cai/tools/

wget

54 tar xf inotify-tools-3.14.tar.gz

55 ls

56 cd inotify-tools-3.14

57 ./configure --prefix=/usr/local/inotify-tools-3.14

58 make && make install

59 yum install -y gcc

60 ./configure --prefix=/usr/local/inotify-tools-3.14

61 make && make install

62 cd /usr/local/inotify-tools-3.14/

63 ls

64 ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify

4.

Scripts are placed under

/server/scripts

vim /server/scripts /rsync.sh

#!/bin/sh

host=192.168.76.129

src=/tmp/

des=tmp

user=rsync_backup

/usr /local/inotify/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | \

while read files

do

/usr/bin/rsync -avz --delete $src $user@$host::$des --password-file=/etc/rsync.password

echo "${files} was rsynced" >> ;/var/log/rsyncd.log 2>&1

done

exit o

~         

and give

764 permission

Test script:

sh -x /server/scripts/rsync.sh

Run the script:

sh /server/scripts/rsync.sh &

Put the

rsync.sh script into the boot entry: echo “/tmp/rsync.sh” >>/ etc/rc.local

The above is the detailed content of Share rsync+inotify real-time synchronization example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Linux: Essential Commands and OperationsLinux: Essential Commands and OperationsApr 24, 2025 am 12:20 AM

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

Linux Operations: Managing Files, Directories, and PermissionsLinux Operations: Managing Files, Directories, and PermissionsApr 23, 2025 am 12:19 AM

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment