🛡️ Nextcloud Backup Like a Pro – Automated with Bash

A backup that does not make you sweat blood: maintenance mode on, back up data and database, maintenance mode off. Sounds simple – the devil is in the order.

Comic: at 2 o'clock at night a cloud with a life ring sits on the desk while a script copies boxes into a safe, the man sleeps

🌐 Also available in: Deutsch · Français · Español · Italiano · Português · Nederlands · Polski

In short: Backing up Nextcloud without breaking a sweat: this lean Bash script switches on maintenance mode, syncs the data directory, dumps the database and switches maintenance mode off again. The post shows the script ready to adapt, where your own credentials go and why you test first and automate afterwards.

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. 🧠


Table of Contents

📜 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 (opens in a new tab) 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. ☁️


🎯 Conclusion

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! 🧬


📚 Related posts:

Some text, images, videos and audio on this site are AI-assisted or AI-generated. Details