In my article on Thursday, I showed how you can set up your own Nextcloud instance on your server in just a few minutes using Docker.

But true data sovereignty also means: regular, reliable backups are absolutely essential!

Fortunately, the backup system from Part 5 can easily be extended with a simple plugin for Nextcloud.

The way to an own clowd (Part 8.1) - Create Nextcloud backups

Create a file named /opt/backup/plugins/nextcloud.sh with the following content:

#!/bin/bash

PLUGIN_OUTPUT_DIR="$1"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S.%3N')] $*"
}

# Source directory of Nextcloud (adjust if necessary)
NEXTCLOUD_DATA_DIR="/opt/nextcloud/data"

# Backup destination
BACKUP_DEST="$PLUGIN_OUTPUT_DIR/files_$(date '+%Y-%m-%d').tar.gz"
log "[INFO] Starting backup from $NEXTCLOUD_DATA_DIR to $BACKUP_DEST ..."
tar -czf "$BACKUP_DEST" -C "$NEXTCLOUD_DATA_DIR" .

if [ $? -eq 0 ]; then
    log "[INFO] Backup of Nextcloud files was successful"
else
    log "[ERROR] Backup of Nextcloud files failed!"
fi

Then make the script executable:

chmod +x /opt/backup/plugins/nextcloud.sh

Conclusion

The plugin is now automatically detected by the central backup system and executed on every run. This way, your Nextcloud files are regularly backed up and can be easily restored in case of emergency.

Quick & Dirty

If you want to get started immediately, just copy & paste:

cat <<'EOF' > /opt/backup/plugins/nextcloud.sh
#!/bin/bash

PLUGIN_OUTPUT_DIR="$1"
NEXTCLOUD_DATA_DIR="/opt/nextcloud/data"
BACKUP_DEST="$PLUGIN_OUTPUT_DIR/files_$(date '+%Y-%m-%d').tar.gz"

tar -czf "$BACKUP_DEST" -C "$NEXTCLOUD_DATA_DIR" .
EOF

chmod +x /opt/backup/plugins/nextcloud.sh