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:
- Enables maintenance mode
- Creates a MySQL database dump
- Backs up the
config/
directory - Backs up any custom
themes/
(optional) - Syncs the external data directory
- 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 databaseconfig/
β Configuration filesthemes/
β 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! π§¬