Recursive Inotify Scripting With Lsyncd
Tags: bash, chmod, chown, enforcing, ftp, inotify, linux, lsyncd, permissions, rsync, scripting, sftp, unix
Lsyncd is a tool which was built to keep two locations in sync with each other efficiently by only sending updates when file changes are detected with inotify events. However, lsyncd is actually quite extensible in that it that supports scripting for each of it’s various types of inotify events. This allows us to perform customized tasks when file changes are detected.
Here are a few examples:
Enforce File Permissions Recursively With Inotify
This lsyncd config will ensure that that files changed, moved or created under the defined directory have mode 777.
#/etc/lsyncd.d/chown.lsyncd settings = { statusFile = "/tmp/chown.lsyncd.stat", statusIntervall = 1, logfacility = daemon, } chown = { delay = 5, maxProcesses = 5, onCreate = "chmod 777 ^sourcePathname", onModify = "chmod 777 ^sourcePathname", onMove = "chmod 777 ^d.targetPathname", onStartup = "sysctl fs.inotify.max_user_watches=1048576; sysctl fs.inotify.max_queued_events=2097152; chmod -R 777 ^source" } sync { chown, source="/path/to/files", target="/dev/null", } |
To start lsyncd run this:
lsyncd -pidfile /var/run/chown.lsyncd /etc/lsyncd.d/chown.lsyncd |
This will result in the defined “source” directory being monitored for file changes and additionally when lsyncd is started it will recursively chmod the “source” directory to ensure that any potentially missed file have the correct permissions. You may notice the sysctl commands that are being run as the “onStartup” command. This is because my watched directory is quite large, and requires adjustments to the default inotify sysctl values.
Inotify Backup – Backup Files When Changed
The below will watch your home directory for file changes and after detecting a changed file will immediately copy that file to the backup destination using rsync while appending a date-stamp to the backup file. To accomplish remote backups you could specify a remote rsync server, or use rsync+ssh with pre shared keys.
#/etc/lsyncd.d/backup.lsyncd settings = { statusFile = "/tmp/backup.lsyncd.stat", statusIntervall = 1, logfacility = daemon, } backup = { delay = 5, maxProcesses = 5, onCreate = "rsync -a --backup --suffix=-`date +%F-%T` ^sourcePathname ^target", onModify = "rsync -a --backup --suffix=-`date +%F-%T` ^sourcePathname ^target", } sync { backup, source="/home", target="/var/backups/" } |
To start lsyncd run this:
lsyncd -pidfile /var/run/backup.lsyncd /etc/lsyncd.d/backup.lsyncd |
I chose lsyncd over the various alternatives out there because to me it made the most sense. I liked that it is built to run as a daemon, and does the vast majority of the heavy lifting for me. Watching a directory recursively was a must have for me, and it requires a minimal amount of scripting for most uses.