Introduction to Rsync
rsync (Remote Sync) is an open source fast backup tool that can mirror and synchronize the entire directory tree between different hosts, support incremental backup, and maintain links and permissions.
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, and the initiating end should have read permissions to this location.
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.
Rsync backup method
- Initiator: The client responsible for rsync synchronization operation is called the initiator. It notifies the server that I want to back up your data.
- Backup source: The server responsible for responding to rsync synchronization operations from the client is called the backup source, and the server that needs to be backed up
- Server: Run the rsyncd service. Generally speaking, the server needs to be backed up.
- Client: store backup data
Rsync synchronization method
- Full backup: Each backup backs up all files or directories from the backup source to the destination.
- Differential backup: Backs up data that has changed since the last full backup (he targets the last full backup, and does not clear the archive attributes during the backup process)
- Incremental backup: Back up data that has changed since the last backup (it doesn’t care what type of backup it is, just back up the changed data, and it will clear the archive attributes)
Rsync synchronization source
rsync synchronization source: refers to the remote server for backup operations, also called backup source.
For example: Server A synchronizes the data of Server B, and Server B is the backup source – downlink synchronization
The opposite is – upstream synchronization
Rsync common command options
Options | Function |
---|---|
-a | Regulations Mode, recursive and retaining object properties, equivalent to -rlptgoD |
-v | Display synchronization process information |
-z | Compress during transmission |
-H | Keep hard link files |
-A | Retain ACL attribute information |
–delete | Delete files that exist in the target location but not in the synchronization source |
–checksum | Determine whether to skip the file based on the checksum of the object |
Options | Function |
---|---|
-m | Continuous Monitoring |
-r | Recursive monitoring All sub-objects |
-q | Simplify output information |
-e | Specify which event types to monitor |
modify | Modify |
create | Create |
move | Move |
delete | Delete |
rsync + inotify implementation configuration
Server side configuration
Modify configuration file
vim /etc/rsyncd.conf read only=no kill (cat /var/run/rsyncd.pid) rsync --daemon netstat -natp |grep rsync
Modify the kernel file (an optimization operation)
Client configuration
Modify configuration file
Write a trigger synchronization script
vim /opt/inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /abc" RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /abc/ [email protected]::wwwroot" $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ] ; then $RSYNC_CMD fi done #Script explanation First define a variable to monitor changes in information such as creation, deletion, movement, modification, attributes, etc. in this directory. Define another variable for rsync synchronization to synchronize the information in the directory in /abc to the specified directory in the rsync server Write another while loop, first execute the monitoring variable, and then execute the while loop. The content of the loop is to read the directory event file, and if the rsync process synchronization operation does not exist, execute the variables for the synchronization operation.
chmod + x /opt/inotify.sh ./inotify.sh ---------------#Join self-start----------------------- chmod +x /etc/rc.d/rc.local echo "/opt/inotify.sh" >>/etc/rc.d/rc.local
Client-side verification
4. Summary
Advantages
- Can be real-time
Disadvantages
-
When bandwidth is particularly abundant, the synchronization rate of rsync is slower than scp.
-
To synchronize millions of data, parameters must be written strictly as required, otherwise synchronization may fail.