inotifywait: how to monitor files and make automatic backups when a modification is detected

inotifywait

inotifywait It's part of the inotify-tools package and listens for kernel events, specifically syscalls for files (create, modify, move, delete) to trigger actions in scripts. It's ideal when you want to perform a backup or synchronization only after a change, avoiding unnecessary polling.

In this tutorial I will show you How to use it to detect any changes to a file and immediately generate a backupto always have an up-to-date backup with the latest modifications…

How to install inotify-tools on Linux

To install the inotify-tools utility on your distro, simply use the following commands:

In Debian, Ubuntu and derivatives (Linux Mint, Pop!_OS, etc.):
sudo apt update sudo apt install inotify-tools -y
In Fedora, RHEL and derivatives:
sudo dnf install inotify-tools -y
On Arch Linux and derivatives (Manjaro, EndeavourOS):
sudo pacman -S inotify-tools --noconfirm
On openSUSE and SUSE: sudo zypper install inotify-tools

To verify that it has been installed correctly, you can run:

inotifywait --help

If it returns the list of options and parameters, the first step is successfully completed.

How to install and configure Rclone

Rclone is considered the "Swiss Army knife" for managing cloud files from the terminal. It supports a multitude of cloud storage services natively, internally managing the decryption and encryption of your files. For installation, you can use the following:

sudo curl https://rclone.org/install.sh | sudo bash

Once installed, Start the Rclone interactive setup wizardFor example, I'll be using Proton Drive, but it's similar for other cloud providers:

rclone-config

Carefully follow these steps in the terminal's interactive wizard:

  1. Type n (New remote) to create a new connection.
  2. Give the connection a name. We suggest something simple like: ProtonDrive.
  3. The assistant will show you a numbered list of dozens of cloud services. Type proton drive or find the number corresponding to "Proton Drive" in the list and press Enter.
  4. Next, it will ask for your UsernameEnter the email address associated with your Proton account.
  5. It will ask if you want to enter your password interactively. Select y (Yes) and type your Proton password (it will not be displayed on the screen for security reasons).
  6. Two-Step Verification (2FA): If you have two-factor authentication enabled in Proton (highly recommended), Rclone will ask for your authenticator app's OTP code. Enter it and press Enter.
  7. It will ask if you want to use "advanced configuration" (Edit advanced config). Type n (No).
  8. Finally, you'll see a summary of the settings. If everything is OK, type y (Yes, this is OK) and then q to exit the wizard.

To verify that the connection is working, run the following command to list the folders in your cloud:

rclone lsd ProtonDrive:

If you see your Proton Drive folders in the terminal, rclone is connected and working…

Example script (monitor and upload modified file)

text editor script sh

Now, to use inotifywait to detect changes in files and copy them directly to the cloud, you can open your favorite text editor and type this:

#!/usr/bin/env bash WATCH_DIR="/home/ /Example" REMOTE="PortonDrive:backups/Example" SYNC_DELAY=3 inotifywait -m -r -e close_write,create,moved_to --format '%w%f' "WATCH_DIR" | while read FILE do sleep $SYNC_DELAY if [ -f "$FILE" ]; then REL="{FILE#$WATCH_DIR/}" echo "Uploading $REL to $REMOTE" rclone copyto "$FILE" "$REMOTE/$REL" --create-empty-src-dirs --transfers=4 --retries=3 fi done

Remember to modify the paths to suit your needs, and the cloud name to the one you entered. Once you've done that, It saves the changes and generates a script file called, for example, monitor-backup.sh and give it execute permissions:

chmod +x monitor-backup.sh

And to execute it:

./monitor-backup.sh

Once running, in this case, it will check for changes in the ~/Example folder and if anything new occurs, it will be stored in the cloud, within /backups/.

And by the way, if your system uses systemd and you can create units, consider using .path + .service to integrate monitoring with the service manager, although in any case inotifywait It's the most direct option…


Add as preferred source in Google