Ever wanted to backup your self-hosted Nextcloud instance without sweating bullets? Tired of rsync-ing folders manually or risking broken databases? Buckle up, sysadmin – here’s a neat little Bash script that does the dirty work for you. From enabling maintenance mode to syncing your external data and dumping your MySQL database – it’s all handled in a single run. 🧠


πŸ“œ What Does This Script Do?

This script performs a 6-step backup of your remote Nextcloud server:

  1. Enables maintenance mode
  2. Creates a MySQL database dump
  3. Backs up the config/ directory
  4. Backs up any custom themes/ (optional)
  5. Syncs the external data directory
  6. Disables maintenance mode again

All of that with rsync, ssh and a little shell magic. The result: A fully automated backup you can cron the hell out of.


🧠 The Script (Just Paste & Tweak)


#!/bin/bash

# === CONFIGURATION ===

REMOTE_USER="root"
REMOTE_HOST="your.remote.host"
REMOTE_SSH_PORT=22
SSH_KEY="$HOME/.ssh/id_rsa"

# Local backup destination (no versioning)
LOCAL_BACKUP_DIR="/data/backup/nextcloud/yourinstance"
mkdir -p "$LOCAL_BACKUP_DIR"

# Remote Nextcloud directory (contains everything)
REMOTE_NC_DIR="/var/www/html/nextcloud"

# Database credentials (use .my.cnf or secured method in production)
REMOTE_DB_NAME="your_db_name"
REMOTE_DB_USER="your_db_user"
# REMOTE_DB_PASS is omitted for security

# === BACKUP START ===

echo "πŸ›‘οΈ  [1/4] Enabling maintenance mode ..."
ssh -i "$SSH_KEY" -p $REMOTE_SSH_PORT -o LogLevel=ERROR "$REMOTE_USER@$REMOTE_HOST" \
  "sudo -u www-data php $REMOTE_NC_DIR/occ maintenance:mode --on" > /dev/null

echo "πŸ—ƒοΈ  [2/4] Creating database dump ..."
ssh -i "$SSH_KEY" -p $REMOTE_SSH_PORT -o LogLevel=ERROR "$REMOTE_USER@$REMOTE_HOST" \
  "mysqldump -u $REMOTE_DB_USER -p'********' $REMOTE_DB_NAME" > "$LOCAL_BACKUP_DIR/db.sql"

echo "πŸ“¦ [3/4] Backing up entire Nextcloud directory from $REMOTE_NC_DIR ..."
rsync -az --delete -e "ssh -i $SSH_KEY -p $REMOTE_SSH_PORT -o LogLevel=ERROR" \
  "$REMOTE_USER@$REMOTE_HOST:$REMOTE_NC_DIR/" "$LOCAL_BACKUP_DIR/nextcloud/"

echo "βœ… [4/4] Disabling maintenance mode ..."
ssh -i "$SSH_KEY" -p $REMOTE_SSH_PORT -o LogLevel=ERROR "$REMOTE_USER@$REMOTE_HOST" \
  "sudo -u www-data php $REMOTE_NC_DIR/occ maintenance:mode --off" > /dev/null

πŸ”’ Don’t Forget to Replace Your Secrets

  • <your_db_name> – Your Nextcloud database name
  • <your_db_user> – The database user
  • <your_db_password> – The password (keep it safe, folks!)
  • REMOTE_HOST / PORT / DIRS – Match these to your server layout

It’s good practice to use environment variables or external .env files for secrets. Hardcoding is fine for quick’n’dirty scripts – just don’t push them to GitHub πŸ˜….


πŸ§ͺ Test Before You Automate

Before you throw this into your crontab, test it manually and inspect your backup folder. A full backup should include:

  • db.sql – Your complete Nextcloud database
  • config/ – Configuration files
  • themes/ – Custom themes (if any)
  • datafiles/ – All user-uploaded files

You can add logging, error handling, versioning and email reports – but this basic script gets the job done for most home-hosted clouds. ☁️


πŸ€– TL;DR

If you self-host Nextcloud, this is the script you didn’t know you needed. SSH in, rsync your heart out, dump that DB, and sleep well knowing your cloud is safe – at least until someone trips over the NAS power cable. πŸ˜‰

Happy hacking & backup responsibly! 🧬

By raphael

Leave a Reply