If you have inotify-tools installed you can use inotifywait
to trigger an action if a file or directory is written to:
#!/bin/sh
dir1=/path/to/A/
while inotifywait -qqre "attrib,modify,close_write,move,move_self,create,delete,delete_self" "$dir1"; do
/run/backup/to/B
done
Where the -qq
switch is completely silent, -r
is recursive (if needed) and -e
is the event to monitor. From man inotifywait
:
attrib The metadata of a watched file or a file within a watched directory was modified. This includes timestamps, file permissions, extended attributes etc.
modify A watched file or a file within a watched directory was written to.
close_write 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.
move 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 A watched file or directory was moved. After this event, the file or directory is no longer being watched.
create A file or directory was created within a watched directory.
delete A file or directory within a watched directory was deleted.
delete_self 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.