Rsync was recorded in detail in my previous backup server blog, and it will not be re recorded here.
Lack of Rsync
Compared with the traditional cp and tar backup methods, rsync has the advantages of high security, fast backup and supporting incremental backup. rsync can solve the data backup requirements with low real-time requirements, such as regularly backing up the file server data to the remote server, regularly mirroring the local disk, etc.
With the continuous expansion of the scale of the application system, there are better requirements for the security and reliability of data. rsync has gradually exposed many deficiencies in the high-end business system. First, when rsync synchronizes data, it needs to scan all files for comparison and differential transmission. If the number of files reaches the order of millions or even tens of millions, scanning all files will be very time-consuming. And what is changing is often a small part of it, which is a very inefficient way. Secondly, rsync cannot monitor and synchronize data in real time. Although it can trigger synchronization through crontab, there must be a time difference between the two trigger actions, which may lead to inconsistency between the data of the server and the client, and it is impossible to completely recover the data in case of application failure. For the above reasons, rsync+inotify combination appears!
What is inotify
inotify is a powerful, fine-grained and asynchronous file system event monitoring mechanism. The Linux kernel has been introduced since 2.6.13, allowing the monitor to open an independent file descriptor and monitor one or more files for the event set, such as opening, closing, moving / renaming, deleting, creating or changing attributes.
Inotify tools is a set of C development interface library functions for inotify file monitoring tool under linux. At the same time, it also provides a series of command-line tools, which can be used to monitor file system events. Inotify tools is written in C. It does not depend on others except that the kernel is required to support inotify. Inotify tools provides two tools: inotifywait, which is used to monitor the changes of files or directories, and inotifywatch, which is used to count the number of file system accesses.
To install inotify tools:
yum install -y inotify-tools
Command line operation
- /usr/bin/inotifywait - wait for specific file system events (open, close, delete, etc.) on the monitored file or directory. It is blocked after execution. It is suitable for use in shell scripts.
- /usr/bin/inotifywatch - collects statistics on the usage of the monitored file system, which refers to the statistics of the number of file system events.
Detailed explanation of common parameters of inotifywait command
[root@backup ~]# inotifywait --help inotifywait 3.14 Wait for a particular event on a file or set of files. Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ] Options: -h|--help Show this help text. @<file> Exclude the specified file from being watched. --exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>. --excludei <pattern> Like --exclude but case insensitive. -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. -d|--daemon Same as --monitor, except run in the background logging events to a file specified by --outfile. Implies --syslog. -r|--recursive Watch directories recursively. --fromfile <file> Read files to watch from <file> or `-' for stdin. -o|--outfile <file> Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. -q|--quiet Print less (only print events). -qq Print nothing (not even events). --format <fmt> Print using a specified printf-like format string; read the man page for more details. --timefmt <fmt> strftime-compatible format string for use with %T in --format string. -c|--csv Print events in CSV format. -t|--timeout <seconds> When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out. -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. Exit status: 0 - An event you asked to watch for was received. 1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred. 2 - The --timeout option was given and no events occurred in the specified interval of time. Events: access file or directory contents were read modify file or directory contents were written attrib file or directory attributes changed close_write file or directory closed, after being opened in writeable mode close_nowrite file or directory closed, after being opened in read-only mode close file or directory closed, regardless of read/write mode open file or directory opened moved_to file or directory moved to watched directory moved_from file or directory moved from watched directory move file or directory moved to or from watched directory **create** file or directory created within watched directory **delete** file or directory deleted within watched directory delete_self file or directory was deleted unmount file system containing file or directory unmounted
The meaning of each parameter is explained in detail with a list below
-e: – various event meanings of event
Test monitoring events
Open two windows
Enter the following in the first window: [root@backup ~]# ls /backup [root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e create /backup In the second window: enter the following [root@backup ~]# cd /backup [root@backup backup]# touch chensiqi Return to the first window and the following contents will appear: 17 03 11 07 19 /backup/chensiqi #Command description inotifywait: ionotify Command tools for -mrq: -q Enter only brief information -r，Recursively monitor the entire directory, including subdirectories -m Continuous monitoring --timefmt Specifies the time format of the output --format: Specifies the format of the output information -e create: Formulate the time type of monitoring and create monitoring create event.
Enter the following information in the first window: [root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e delete /backup In the second window, enter the following information: [root@backup backup]# rm -rf chensiqi The following information will appear in the first window: 17 03 11 07 29 /backup/chensiqi #Command description: -e delete: Specifies the type of event to listen for. Listening deletion delete event
Enter the following information in the first window: inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e close_write /backup In the second window, enter the following information: [root@backup backup]# touch close_write.log [root@backup backup]# echo 111 >> close_write.log [root@backup backup]# rm -f close_write.log The following information will appear in the first window: 17 03 11 07 38 /backup/close_write.log 17 03 11 07 39 /backup/close_write.log #Command description: -e close_write:Specify the listening type. Monitor the closing of file write mode.
Enter the following information in the first window: [root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e moved_to /backup In the second window, enter the following information: The following information will appear in the first window: [root@backup backup]# touch chensiqi [root@backup backup]# mv chensiqi chen [root@backup backup]# mkdir ddddd [root@backup backup]# mv chen ddddd/
inotify monitoring command format
inotifywait -mrq --timefmt "%F" --format "%T %w %f Event information:%e" /data -e CREATE create Create delete Delete moved_to Move in close_write modify
You can use Rsync + inotify to write scripts for real-time data synchronization, but this method has many disadvantages. Therefore, we generally do not synchronize data in this way, but use sersync. Its function is more powerful.
- Support configuration file management
- Real daemon socket
- The failed file can be retransmitted regularly (scheduled task function)
- HTTP interface of the third party (e.g. updating cdn cache)
- Default multi process rsync synchronization
**It can be understood that sersync is inotify + rsync**
Deploying the sersync synchronization service
- Need to go to the website to download rpm package
- PS: the software shall be uniformly stored in / server/tools Directory as far as possible
- Decompress the software package and save the decompressed data
unzip sersync_installdir_64bit.zip root@nfs01 tools]# tree sersync_installdir_64bit sersync_installdir_64bit └── sersync ├── bin --- sersync Software command directory │?? └── sersync ├── conf --- sersync Software configuration directory │?? └── confxml.xml └── logs --- sersync Software log directory [root@nfs01 tools]# mv sersync_installdir_64bit/sersync/ /usr/local/
- Write configuration file:
vim conf/confxml.xml # Some data descriptions of the configuration file: 6 <filter start="false"> 7 <exclude expression="(.*)\.svn"></exclude> 8 <exclude expression="(.*)\.gz"></exclude> 9 <exclude expression="^info/*"></exclude> 10 <exclude expression="^static/*"></exclude> 11 </filter> Note: exclude the specified data information and do not conduct real-time transmission synchronization 12 <inotify> 13 <delete start="true"/> 14 <createFolder start="true"/> 15 <createFile start="false"/> 16 <closeWrite start="true"/> 17 <moveFrom start="true"/> 18 <moveTo start="true"/> 19 <attrib start="false"/> 20 <modify start="false"/> 21 </inotify> Description: definition inotify Events that the program needs to monitor 24 <localpath watch="/opt/tongbu"> 25 <remote ip="127.0.0.1" name="tongbu1"/> 26 <!--<remote ip="192.168.8.39" name="tongbu"/>--> 27 <!--<remote ip="192.168.8.40" name="tongbu"/>--> 28 </localpath> 29 <rsync> 30 <commonParams params="-artuz"/> 31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <rsync> explain: sersync Need to use rsync ，This paragraph is configuration rsync Content
- Start the sersync service program
PS: sersync Not like before yum It can be used directly as the software, and needs to be executed in the script path specified by it. Of course, you can also modify the environment variables [root@nfs01 bin]# export PATH="$PATH:/usr/local/sersync/bin" [root@nfs01 bin]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin ## Start real-time synchronization sersync -dro /usr/local/sersync/conf/confxml.xml parameter-d: Enable daemon mode parameter-r: Before monitoring, connect the monitoring directory with the remote host rsync Push the command again parameter-o: Specifies the configuration file, which is used by default confxml.xml file -o /usr/local/sersync/conf/confxml.xml ## Stop real-time synchronization service killall sersync ## Automatic startup after startup /etc/rc.local <-- sersync -dro /usr/local/sersync/conf/confxml.xml