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 collects filesystem usage statistics and outputs counts of each inotify event. We will use inotifywait, since we do not need statistics.
inotifywait filename inotifywait example
inotifywait /tmp Setting up watches. Watches established. /tmp/ MODIFY test
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.
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
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.
access
Description: A watched file or a file within a watched directory was read from.
modify
Description: A watched file or a file within a watched directory was written to.
attrib
Description: The metadata of a watched file or a file within a watched directory was modified. This includes timestamps, file permissions, extended attributes etc.
close_write
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.
close_nowrite
Description: A watched file or a file within a watched directory was closed, after being opened in read-only
mode.
close
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
.
open
Description: A watched file or a file within a watched directory was opened.
moved_to
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.
moved_from
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.
move
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
.
move_self
Description: A watched file or directory was moved. After this event, the file or directory is no longer being watched.
create
Description: A file or directory was created within a watched directory.
delete
Description: A file or directory within a watched directory was deleted.
delete_self
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.
unmount
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.
Now let’s use inotifywait with our script.
while true #run indefinitely do inotifywait -r -e modify,attrib,close_write,move,create,delete /dir && /bin/bash backup-script done
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
+ 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.
The simplest way to configure inotifywait
is using crontab a script. The entry could look like this:
*/5 * * * * /opt/jobs/crontab-watch.sh
Script name: /opt/jobs/crontab-watch.sh
#!/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
Script name: /opt/jobs/inotifywait-openvpn.sh
#!/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
Script name: /opt/jobs/sync-openvpn.sh
#!/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