Automatically scan external changes for Nextcloud local storage.
Nextcloud uses a database to keep track of the files it stores. However, if a file is externally modified, the database won't know. It seems that the only possible way to inform the database is to run occ files:scan, which can be time- and resource-consuming if the whole storage is periodically scanned.
This Python script uses inotifywait to watch for changes and issues scan requests for the modified part on demand.
- Linux with systemd
- Nextcloud 14+ installation
phpandinotifywaitinPATH- Python 3.7+ as
/usr/bin/python
The script should work if modified for other environments.
- Clone this repo:
git clone https://github.com/Blaok/nextcloud-inotifyscan; cd nextcloud-inotifyscan - Install the script and config in the correct locations:
sudo make install - Figure out which Unix
usernameis running Nextcloud; onDebian/Ubuntuthis is usuallywww-data - Create
/etc/nextcloud-inotifyscan/username.iniaccording to the example given in/etc/nextcloud-inotifyscan/sample.ini - Enable and start the service:
sudo systemctl enable --now nextcloud-inotifyscan@username
- The
datadir path is now read from/path/to/nextcloud/config.phpautomatically. - Multiple users and multiple Nextcloud installation instances can be watched in a single service.
- Local filesystem folders added via the external storage app are supported.
- This script is tested on
ubuntu-latestusing GitHub Actions- To install
inotifywaiton Ubuntu, usesudo apt install inotify-tools
- To install
- This script ignores hidden files (
inotifywait --exclude '/\.'), as Nextcloud does - A similar project implemented in
php, files_inotify,doesn't seem to work at this point in timeexists but I'm not sure if it works - Watching ~2000 directories with ~30000 files,
inotifywaitconsumes less than 4MB memory (RES+SHR) - To watch more than 8192 directories,
fs.inotify.max_user_watchesmay need to be increased viasysctl - Nextcloud 14+ is required for the
--shallowflag