The previous blog introduces the main use of rsync and the installation and configuration of rsync in server mode. Inotify is an event mechanism used in Linux kernel to monitor file system changes. Such as file creation, modification, and deletion, and notify the application by event. Inotify-tools is a kernel-based inotify mechanism that allows you to monitor file system events.
Inotify-implemented file system monitoring events:
In_access: The file is accessed.
In_modify: File Modified
In_attrib, File Attribute Modified
In_close_write: The file opened in writable mode is disabled.
In_close_nowrite: files opened in non-writable mode are closed.
In_open: The file is opened.
In_moved_from: The file is removed from the monitored directory.
In_moved_to: The file is moved to the monitored directory.
In_create: create a file or subdirectory in the monitored directory
In_delete: the file or directory is deleted.
In_delete_self: indicates that an executable file is deleted when it is executed.
In_move_self, self-moving, that is, an executable file moves itself during execution
Use the following parameters in the/proc interface to set the memory size that inotify can use:
1./proc/sys/fs/inotify/max_queue_events
When an application calls inotify, it needs to initialize the inotify instance and set an event queue for it. The value in this file is used to set the maximum length of the queue; events that exceed this limit will be discarded.
2./proc/sys/fs/inotify/max_user_instances
The value in this file is used to set the maximum number of inotify instances that can be created by each user ID (User identified by ID ).
3./proc/sys/fs/inotify/max_user_watches
The value in this file is used to set the maximum number of files or directories that can be monitored by each user ID.
Inotifywait command
Inotifywait is especially suitable for waiting for an event in a script and performing corresponding operations based on a specific event. For example, you can use the script to monitor the changes, creation, deletion, and attribute information on files in a specified directory, then, use the rsync command to synchronize the files corresponding to an event to other hosts. The common options are as follows:
-M, -- monitor: the default action of inotifywait is to exit after a specific event of the specified file is monitored. This option can be used for continuous monitoring;
-R, -- Recursive: recursively monitors all files in a specified directory, including new files or subdirectories. If the number of files in the directory to be monitored is large, you usually need to modify the/proc/sys/fs/inotify/max_users_watchs kernel parameters because the default value is 8192.
-E <event>, -- event <event>: Specifies the specific event to be monitored. All events are monitored by default. <event> here, including access, modify, attrib, close_write, close_nowirte, close, open, moved_to, moved_from, move, create, delete, delete_selt, etc;
-- Timefmt <FMT>: When % T is used in the -- format option, the -- timefrt option can be used to specify a custom time format that complies with the strftime specification, available time format characters can be obtained through the strftime manual page; -- common parameters after timefrt are '% d/% m/% Y % H: % m ';
-- Format <FMT>: Customize the output format of inotifywait, for example, -- format '% T % w % F'. Common Format characters are as follows:
% W: display the name of the monitored file;
% F: if the object of an event is a directory, the name of the monitored directory is displayed. The default value is an empty string;
% T: Use the custom time format in the -- timefmt Option
Example:
# Inotify-r-m -- timefmt '% d/% m/% Y % H: % m' -- format' % T % w % F'-e create, delete, modify, close_write/tmp/src
Persistent monitoring of create, delete, modify, close_write events in the/tmp/src directory and all its internal files.
File sync backup between two hosts
File Source host a: 192.168.100.107 source file to be synchronized:/var/mydata
File backup server B: 192.168.100.105 backup storage location:/tmp/mydata
Assume that the rsync service has been installed and configured on server B, and the service is enabled properly. Rsync has been installed on host a, and the file synchronization function has been tested between hosts through rsync. Install inotify-tools and write the corresponding script.
1) Compile and install inotify-Tools
The source code package inotify-tools-3.13.tar.gz has been downloaded from the official website and is located in the/tmp/directory.
# tar xf inotify-tools-3.13.tar.gz# cd inotify-tools-3.13# ./configure# make && make install# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf# ldconfig
2) write the script/root/mydatasync. Sh.
#!/bin/bash#syncServer=192.168.100.105syncShareName=mydatasrcdir=/var/mydata/inotifywait -mr --timefmt "%y%m%d %H%M" --format "%T %w %f" -e create,modify,delete,close_write,attrib $srcdir | while read DATE TIME DIR FILE; do fileName=${DIR}${FILE} rsync -azv --password-file=/etc/rsyncd.passwd $srcdir [email protected]${syncServer}::${syncShareName} &>/dev/null if [ $? -eq 0 ]; then echo "At ${TIME} on ${DATE}, file ${fileName} was backde up by rsync." >> /var/log/backup.log fidone
3) grant the script executable permission
# chmod u+x /root/mydatasync.sh
If you want the file sync backup to be automatically started at startup, you can do the following:
# echo "/root/mydatasync.sh" >> /etc/rc.d/rc.local
Practical testing experience:
In the preceding method, files or directories added to the source and the file content are synchronized to the backup server. If a file or directory is deleted from the source, the backup server does not delete the file or directory. Therefore, this synchronous backup is not suitable for scenarios that require strict consistency between source and server resources!
Rsync + inotify enables synchronous backup of files between two hosts