Rsync remote synchronization +inotify monitoring

1. Introduction to rsync synchronization

A fast incremental backup tool
rsync (Remote Sync, remote synchronization) is an open source fast backup tool that supports local replication or synchronization with other SSH and rsync hosts.

cp: Copy the original file completely to the specified path, and make a full backup

rsync: do local copy

1. First compare the original file and the file in the target location to find the differences.

2. Synchronize data based on the difference between the file at the target location and the original file.

Rsync is a data mirroring backup tool under Linux systems. The fast incremental backup tool Remote Sync can be used to synchronize remotely and synchronize between different hosts. It can realize full backup and incremental backup, maintain links and permissions, and adopt optimized Synchronization algorithm, compression is performed before transmission, so it is very suitable for applications such as centralized backup or off-site backup. At the same time, Rsync supports local replication or synchronization with other SSH and rsync hosts.

In a remote synchronization task, the client responsible for initiating the rsync synchronization operation is called the initiator, and the server responsible for responding to the rsync synchronization operation from the client is called the synchronization source. During the synchronization process, the synchronization source is responsible for providing the original location of the file, to which the initiating end should have read permissions.

2. Configure rsync backup source

In a remote synchronization task, the client responsible for initiating rsync synchronization operations is called the initiator, and the server responsible for responding to rsync synchronization operations from the client is called the synchronization source. During the synchronization process, the synchronization source is responsible for providing the original location of the file, to which the initiating end should have read permissions.

rsync synchronization source: refers to the remote server for backup operations, also called backup source

rsync sync source→Internet→rsync client

The synchronization source is called downstream synchronization above, and downstream synchronization is understood as download. If the client wants to synchronize data, it needs to download the data from the server (source server) through the network. The server only needs to respond to the client (initiator).

rsync sync source:

1. Initiator: The client responsible for initiating rsync synchronization operation is called the initiator. It notifies the server that I want to back up your data.

2. Backup source: Responsible for responding to the client rsync, the synchronization operation service is called the backup source, and the server that needs to be backed up

3. Server: The service that runs rsync, generally speaking, it is the server that needs to be backed up.

4. Client: stores backup data

rsync principle

Operating mode and port

Using c/s mode (client/server mode) is actually point-to-point transmission, just use the rsync command directly.

rsync listens on port 873

3. Basic usage of rsync command

Basic format: rsync [options] original location target location

Common options:

-v: Display detailed (verbose) information about the synchronization process.

-z: Compress when transferring files.

-a: Archive mode, retains file permissions, attributes and other information, equivalent to the combination option “-rlptgoD”.

–delete: Delete files that exist in the target location but not in the original location.

Two expression methods for configuring sources

Format one:

Username@host address::shared module name local directory
rsync -avz [email protected]::wwwroot /opt/

#Indicates that the files defined in the wwwroot module of the 20.0.0.55 synchronization source will be synchronized to the local /data directory

Format two:

rsync://username@host address/shared module name local directory
rsync -avz rsync://[email protected]/wwwroot /opt/

Configure rsync source server

systemctl stop firewalld
setenforce 0
rpm -q rsync #General systems have rsync installed by default
 
#Create the /etc/rsyncd.conf configuration file
vim /etc/rsyncd.conf #Add the following configuration items
uid=root
gid=root
use chroot = yes #imprisoned in the source directory
address = 192.168.80.10 #Listening address
port = 873 #Listen to port tcp/udp 873, which can be viewed through cat /etc/services | grep rsync
log file = /var/log/rsyncd.log #Log file location
pid file = /var/run/rsyncd.pid #The file location where the process ID is stored
hosts allow = 192.168.80.0/24 #Client address allowed to be accessed
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #File types that are no longer compressed during synchronization
 
[wwwroot] #Shared module name
path = /var/www/html #The actual path of the source directory
comment = Document Root of www.kgc.com
read only = yes #Whether it is read-only
auth users = backuper #Authorized accounts, multiple accounts separated by spaces
secrets file = /etc/rsyncd_users.db #Data file to store account information
 
#If you use anonymous mode, just remove the "auth users" and "secrets file" configuration items.
#Create data files for backup accounts

#Create data files for backup accounts
vim /etc/rsyncd_users.db
backuper:abc123 #No need to create a system user with the same name
 
chmod 600 /etc/rsyncd_users.db

# Ensure that all users have read permissions to the source directory /var/www/html
chmod + r /var/www/html/
ls -ld /var/www/html/

#Start rsync service program
rsync --daemon #Start the rsync service and run it as an independent listening service (daemon process)
 
netstat -anpt | grep rsync

#Close rsync service
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid

Configure rsync downstream synchronization

Initiator configuration

rsync -az [-v] –password-file password file path [–delete] username@source server address::source directory module name local target directory

Common options:
-r: Recursive mode, including all files in the directory and subdirectories.
-l: Symbolic link files are still copied as symbolic link files.
-v: Display detailed (verbose) information about the synchronization process.
-z: Compress when transferring files.
-a: Archive mode, retains file permissions, attributes and other information, equivalent to the combination option "-rlptgoD".
-p: Preserves the file's permission flags.
-t: Keep the timestamp of the file.
-g: Preserve the file's group tag (only for superusers).
-o: Keep the file's owner mark (for superuser use only).
-H: Keep hard link files.
-A: Preserve ACL attribute information.
-D: Preserve device files and other special files.
--delete: Delete files that exist in the target location but not in the original location.
--checksum: Determine whether to skip files based on checksum (rather than file size or modification time).

#Download the specified resources to the local/opt directory for backup.
Format one:
rsync -avz [email protected]::wwwroot /opt Password: abc123 (backuper password)