Show pageOld revisionsBacklinksAdd to bookExport to MarkdownBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Inotify how-to ====== ===== Inotifywait ===== This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees. ===== inotifywatch ===== inotifywatch collects filesystem usage statistics and outputs counts of each inotify event. We will use inotifywait, since we do not need statistics. ===== Syntax ===== <code bash> inotifywait filename inotifywait example </code> <code bash> inotifywait /tmp Setting up watches. Watches established. /tmp/ MODIFY test </code> As you can see in the above example an event (in this case a “modify action” was performed on a file named “test” inside /tmp) triggered the output. Now ''%%inotifywait%%'' by default checks for all events including if a file was opened but not written to, Since we only want ''%%rsync%%'' to trigger on change events like when a file is modified, we will need to specify the ''%%-e%%'' flag along with the list of events we want to be notified about. <code bash> inotifywait -m -r -e \ modify,attrib,close_write,move,create,delete /tmp Setting up watches. Beware: since -r was given, this may take a while! Watches established. /tmp/ MODIFY a /tmp/ CLOSE_WRITE,CLOSE a </code> The -m flag is for continuous monitoring as by default inotifywait will exit on the first event and -r means recursively or check through sub-directories as well. ===== List of inotifywait events ===== <code bash> access </code> **Description:** A watched file or a file within a watched directory was read from. <code bash> modify </code> **Description:** A watched file or a file within a watched directory was written to. <code bash> attrib </code> **Description:** The metadata of a watched file or a file within a watched directory was modified. This includes timestamps, file permissions, extended attributes etc. <code bash> close_write </code> **Description:** A watched file or a file within a watched directory was closed, after being opened in writeable mode. This does not necessarily imply the file was written to. <code bash> close_nowrite </code> **Description:** A watched file or a file within a watched directory was closed, after being opened in ''%%read-only%%'' mode. <code bash> close </code> **Description:** A watched file or a file within a watched directory was closed, regardless of how it was opened. Note that this is actually implemented simply by listening for both close_write and close_nowrite, hence all close events received will be output as one of these, not ''%%close%%''. <code bash> open </code> **Description:** A watched file or a file within a watched directory was opened. <code bash> moved_to </code> **Description:** A file or directory was moved into a watched directory. This event occurs even if the file is simply moved from and to the same directory. <code bash> moved_from </code> **Description:** A file or directory was moved from a watched directory. This event occurs even if the file is simply moved from and to the same directory. <code bash> move </code> **Description:** A file or directory was moved from or to a watched directory. Note that this is actually implemented simply by listening for both moved_to and moved_from, hence all close events received will be output as one or both of these, not ''%%move%%''. <code bash> move_self </code> **Description:** A watched file or directory was moved. After this event, the file or directory is no longer being watched. <code bash> create </code> **Description:** A file or directory was created within a watched directory. <code bash> delete </code> **Description:** A file or directory within a watched directory was deleted. <code bash> delete_self </code> **Description:** A watched file or directory was deleted. After this event the file or directory is no longer being watched. Note that this event can occur even if it is not explicitly being listened for. <code bash> unmount </code> **Description:** The filesystem on which a watched file or directory resides was unmounted. After this event the file or directory is no longer being watched. Note that this event can occur even if it is not explicitly being listened to. ===== Scripting ===== Now let’s use inotifywait with our script. <code bash> while true #run indefinitely do inotifywait -r -e modify,attrib,close_write,move,create,delete /dir && /bin/bash backup-script done </code> Since we want to continuously monitor changes, we use an infinite while loop and the Logic “&&” operator will ensure that our backup script is only triggered on a successful completion of the inotifywait event <code bash> + true + inotifywait -r -e modify,attrib,close_write,move,create,delete / Setting up watches. Beware: since -r was given, this may take a while! Watches established. /dir DELETE a + /bin/bash backup-script + rsync -avz -e “ssh ” /path/to/yourfile user@backupserver.com:/backup/ 2> \ /tmp/error.txt + mail -s “backup complete” user@youremail.com + echo “backup for $(date) “ + true + inotifywait -r -e modify,attrib,close_write,move,create,delete /dir Setting up watches. Beware: since -r was given, this may take a while! Watches established. </code> ===== Resources ===== * If you have a suggestion feel free to let me know. The complete source code to the file syncing daemon with rsync and inotify can be found at [[https://github.com/Leo-g/backup-bash|backup-bash]] * [[https://github.com/rvoicilas/inotify-tools/wiki|Sourcecode for inotify-tools on github]] ===== Setup using crontab ===== The simplest way to configure ''%%inotifywait%%'' is using crontab a script. The entry could look like this: <code bash> */5 * * * * /opt/jobs/crontab-watch.sh </code> ==== Script for crontab ==== **Script name:** ''%%/opt/jobs/crontab-watch.sh%%'' <code bash> #!/bin/bash script='/opt/jobs/inotifywait-openvpn.sh' w=`basename $script` ps ax|grep -v grep|grep -q $w if [ $? -eq 0 ]; then sleep 1 else $script #& fi </code> ==== Script for inotify-tools ==== **Script name:** ''%%/opt/jobs/inotifywait-openvpn.sh%%'' <code bash> #!/bin/bash inotify='/usr/bin/inotifywait' iopts='modify,attrib,close_write,move,create,delete' files='/usr/local/openvpn_as/etc/db/certs.db /usr/local/openvpn_as/etc/db/config.db /usr/local/openvpn_as/etc/db/log.db /usr/local/openvpn_as/etc/db/userprop.db' script="/opt/jobs/sync-openvpn.sh" while true; do $inotify -q -e $iopts $files && $script done </code> ==== Script for backup or action ==== **Script name:** ''%%/opt/jobs/sync-openvpn.sh%%'' <code bash> #!/bin/bash me=`basename $0` log="/var/log/$me.log" d=`date '+%Y-%m-%d-%H:%M:%s'` p=`ps ax|grep -v grep|grep $me|wc -l` if [ $p -gt 2 ]; then echo -e "$d Error:\tScript already running" >> $log exit 1 else echo -e "$d Info:\tStarting backup now..." >> $log # code or script for backup fi </code> inotify.txt Last modified: 2019/02/08 16:59by admin