Nextcloud CLI shell script

With the recent instantiation of my simple scanning station, there is now another data sink in my home network that wants backup. And in the case of the docspell database, we’re looking at quite relevant data, so a backup definitely is called for. Where backups are concerned I am partial to the dandelion strategy: spread your data around often, and to all kinds of places. One of the locations that I like to use is the Nextcloud instance that is covering all our “Cloud data” needs.

Nextcloud interaction options

Nextcloud offers a sync client for most platforms, of course including Linux (my scanning station is running on Ubuntu, on a Raspberry Pi). However, that default client is a GUI application. My Raspi is running headless, and I don’t want to pull in dozens of additional packages to run a desktop that I’ll never use. Also, for the use case at hand I simply want to push some files into Nextcloud, no fancy syncing required.

What I need is a simply command-line interface for file upload, that I can use from a cron script that will periodically push backup files to the Nextcloud server. Nextcloud offers a WebDAV interface for basic file functions, and that means that some scripting around curl should be enough.

That escalated quickly

Ok then - a while ago I already wrote a simple script to just push a file to a specified location on a Nextcloud file server. I also had a second script that could delete a file/folder. Wouldn’t it be nice if there was a full command-line interface (CLI) client that could do it all? File upload and download, deletion, directory creation, useful error handling?

Of course it would!

So that’s what I build the last couple of evenings - the result can be found here. It is a reasonably complete nextcloud files cli, including some ‘fancy’ options like checking whether a file already exists before uploading.

nx_client usage

Nextcloud cli for downloading/uploading/deleting files

Usage: nx_client.sh [-h] [-c <configfile>] [-u <file> -f | -p | -d | -o] <destination>

<destination> is the location on your nextcloud file server instance, starting from the root folder. Nextcloud credentials and settings are sourced from ~/.nx_client

Options:
                no option will download the file at <destination> to the current directory
 -o <file>      download file at <destination> to local <file>
 -u <file>      upload <file> to <destionation>, if it doesn't already exist
 -f             force re-upload of <file> even if it already exists
 -d             delete file or directory at <destionation>
 -p             create <destination> directory
 -c             nextcloud configuration file to use, defaults to ~/.nx_client
 -h             display this help

The configuration file (default: ~/.nx_client) needs to define the variables BASEURL and CREDS - for example:
 BASEURL="https://example.nextcloud.host/remote.php/webdav"
 CREDS="example@user.account:examplePassword"

Skipping the boring bits

It took me a lot of fiddling to get nx_client to this point - none of which is interesting enough to write about. It’s a nifty little tool I think, way beyond what’s needed for the initial backup use case. Which now is as simple as putting together something like this:

#!/bin/bash

logger -t "$0" "uploading <APP> backup"
for file in /path/to/backup/files/*; do
    nx_client.sh -c /home/user/.nx_client -u "$file" /Backup/APP/
done

Call that from your crontab, and you’ll have a simple pipeline that takes a bunch of files you want off-site and pushes them to a Nextcloud instance.

So what’s with docspell?

The nx_client and backup scripts above work for anthing that’s a file, of course - so what’s with the docspell data (docspell is using postgresql)? As usual, people have been here before, in this case there is this nice dockerized pgbackup-to-local-filesystem solution, courtesy of PauRE. With that, the backup chain to Nextcloud looks like this:

  • dockspell/postgresql
  • docker-postgres-backup-local
  • cronjob calling a simple backup script (see previous section)
  • nx_client for performing the uploads

Works like a charm - one more dandelion seed taken care of!


Waymarks

  • Nothing much - the profusion of different styles of statements { and brackets! [] [[]] () (()) } in the context of bash if statements is not pretty
  • Nextcloud rocks, as usual
  • The most complicated part of all this is: which files to push off-site, how often? (compare with what pgbackup exports)