12 KiB
TrueNAS Nextcloud App → Docker Migration
This guide covers migrating a Nextcloud instance from a TrueNAS App (Kubernetes/k3s) to a Docker container.
Scripts
| Script | Purpose | Run On |
|---|---|---|
backup-nextcloud-truenas.sh |
Backup Nextcloud from TrueNAS App | TrueNAS host |
restore-nextcloud-docker.sh |
Restore Nextcloud to Docker | Docker host |
Prerequisites
On TrueNAS Host
- TrueNAS with Nextcloud app installed
- SSH access to TrueNAS (or TrueNAS Shell)
k3s kubectlavailable (usually at/usr/local/bin/k3s kubectl)pg_dump(PostgreSQL) ormariadb-dump/mysqldump(MariaDB) installed- Write access to backup destination dataset
On Docker Host
- Docker Engine installed
- Docker Compose (v2 plugin or standalone)
- Write access to Docker data directory
- Recommended: 4GB+ RAM for Nextcloud + Database + Redis
Quick Start
1. Backup on TrueNAS
# SSH into TrueNAS
ssh root@truenas.local
# Copy the backup script to TrueNAS
scp backup-nextcloud-truenas.sh root@truenas.local:/tmp/
# Run the backup script
APP_NAMESPACE=nextcloud \
BACKUP_DEST=/mnt/tank/backups/nextcloud \
/tmp/backup-nextcloud-truenas.sh
Environment Variables for Backup:
| Variable | Default | Description |
|---|---|---|
APP_NAMESPACE |
nextcloud |
Kubernetes namespace of Nextcloud app |
BACKUP_DEST |
/mnt/tank/backups/nextcloud |
Destination dataset path |
DB_TYPE |
postgresql |
Database type: postgresql or mariadb |
DB_HOST |
auto-detect | Database host |
DB_PORT |
auto-detect | Database port (5432 PG / 3306 MariaDB) |
DB_NAME |
nextcloud |
Database name |
DB_USER |
nextcloud |
Database user |
DB_PASS |
auto-detect | Database password |
PVC_DATA |
auto-detect | Data PVC name |
PVC_CONFIG |
auto-detect | Config PVC name |
PVC_APPS |
auto-detect | Custom apps PVC name |
RETENTION_COUNT |
7 |
Number of backups to keep |
COMPRESSION_LEVEL |
6 |
gzip compression (1-9) |
MAINTENANCE_MODE |
true |
Enable maintenance mode during backup |
2. Transfer Backup to Docker Host
# From your workstation, pull the backup from TrueNAS
scp root@truenas.local:/mnt/tank/backups/nextcloud/nextcloud_backup_*.tar.gz* .
# Copy to Docker host
scp nextcloud_backup_*.tar.gz* user@docker-host:/opt/nextcloud/
3. Restore on Docker Host
# SSH into Docker host
ssh user@docker-host
# Make scripts executable
chmod +x restore-nextcloud-docker.sh
# Run restore (set required passwords)
DB_TYPE=postgresql \
DB_ROOT_PASS=your_root_password \
DB_PASS=your_nextcloud_password \
./restore-nextcloud-docker.sh /opt/nextcloud/nextcloud_backup_20240115_120000.tar.gz
Environment Variables for Restore:
| Variable | Default | Description |
|---|---|---|
DOCKER_DIR |
/opt/nextcloud |
Docker project directory |
COMPOSE_FILE |
docker-compose.yml |
Compose filename |
DB_CONTAINER |
nextcloud-db |
Database container name |
NEXTCLOUD_CONTAINER |
nextcloud |
Nextcloud container name |
DB_TYPE |
postgresql |
Database type: postgresql or mariadb |
DB_ROOT_PASS |
required | Database root password |
DB_NAME |
nextcloud |
Database name |
DB_USER |
nextcloud |
Database user |
DB_PASS |
required | Database user password |
NEXTCLOUD_DATA_DIR |
${DOCKER_DIR}/nextcloud-data |
Host data directory |
REDIS_ENABLED |
true |
Enable Redis cache |
SKIP_DB |
false |
Skip database restore |
SKIP_FILES |
false |
Skip files restore |
FORCE |
false |
Skip confirmations |
4. Configure and Start Nextcloud
cd /opt/nextcloud
# Edit .env with your passwords and domain (IMPORTANT!)
nano .env
# Required changes in .env:
# - DB_ROOT_PASS=your_strong_root_password
# - DB_PASS=your_strong_user_password
# - NEXTCLOUD_TRUSTED_DOMAINS=your-domain.com,localhost,192.168.x.x
# Start containers
docker-compose up -d
# Check logs
docker-compose logs -f nextcloud
5. Post-Restore Steps
After containers are running, run these commands:
# Disable maintenance mode (if backup had it enabled)
docker-compose exec nextcloud php occ maintenance:mode --off
# Run database upgrades
docker-compose exec nextcloud php occ upgrade
# Fix database schema
docker-compose exec nextcloud php occ db:add-missing-indices
docker-compose exec nextcloud php occ db:add-missing-primary-keys
# Clear cache
docker-compose exec nextcloud php occ cache:clear
6. Access Nextcloud
Open your browser to http://<docker-host>:8080
- If database was restored: Your existing Nextcloud with all files/users
- If fresh database: Nextcloud installer will appear
Detailed Workflow
Backup Process (TrueNAS)
The backup script performs these steps:
- Detection: Finds Kubernetes namespace, database credentials, and PVCs
- Maintenance Mode: Enables
occ maintenance:mode --on(configurable) - Manifests: Exports all K8s resources
- Database: Dumps PostgreSQL or MariaDB database
- Files: Creates temporary pods to copy data from PVCs:
data/→ Nextcloud data directory (user files)config/→ Nextcloud config directory (config.php)apps/→ Nextcloud custom apps (if separate PVC)
- Archive: Creates compressed
.tar.gzwith checksum - Cleanup: Disables maintenance mode, removes old backups
- Info: Generates
RESTORE_INFO_<timestamp>.txt
Restore Process (Docker)
The restore script performs these steps:
- Validation: Verifies backup archive integrity (checksum)
- Extraction: Unpacks backup to temporary directory
- Docker Compose: Generates
docker-compose.ymland.envtemplate - Database: Recreates database and restores from SQL dump
- Files: Syncs data, config, and custom apps to host directory
- Permissions: Fixes file ownership for www-data (UID 33)
- Guidance: Shows next steps including
occcommands
Docker Compose Structure
The generated docker-compose.yml creates:
nextcloud/
├── docker-compose.yml
├── .env # Your passwords (EDIT THIS!)
├── nextcloud-data/ # Host-mounted Nextcloud data
│ ├── data/ # User files
│ ├── config/ # config.php
│ └── custom_apps/ # Custom apps (if used)
└── db-data/ # Database volume (Docker managed)
Services Included
| Service | Image | Purpose |
|---|---|---|
db |
postgres:16 or mariadb:10.11 |
Database |
redis |
redis:7-alpine |
Memory cache |
nextcloud |
nextcloud:latest |
Nextcloud app |
Common Scenarios
Migration with Same Database Credentials
# On TrueNAS, note the DB_PASS from backup output or secret
# On Docker host, use same passwords:
DB_TYPE=postgresql \
DB_ROOT_PASS=same_as_truenas \
DB_PASS=same_as_truenas \
./restore-nextcloud-docker.sh backup.tar.gz
Fresh Database, Restore Only Files
SKIP_DB=true \
./restore-nextcloud-docker.sh backup.tar.gz
Restore Only Database
SKIP_FILES=true \
./restore-nextcloud-docker.sh backup.tar.gz
Using MariaDB Instead of PostgreSQL
# Backup (TrueNAS)
DB_TYPE=mariadb ./backup-nextcloud-truenas.sh
# Restore (Docker)
DB_TYPE=mariadb \
DB_ROOT_PASS=xxx DB_PASS=yyy \
./restore-nextcloud-docker.sh backup.tar.gz
Custom Data Directory
NEXTCLOUD_DATA_DIR=/data/nextcloud \
./restore-nextcloud-docker.sh backup.tar.gz
Nextcloud-Specific Configuration
Trusted Domains
Critical: Update NEXTCLOUD_TRUSTED_DOMAINS in .env before starting:
NEXTCLOUD_TRUSTED_DOMAINS=localhost,nextcloud.example.com,192.168.1.100
Redis Configuration
Redis is enabled by default in the generated compose. Nextcloud will auto-configure it via environment variables.
Cron Jobs
Set up background jobs (recommended: system cron):
# Add to host crontab (runs every 5 minutes)
*/5 * * * * docker exec -u www-data nextcloud php -f /var/www/html/cron.php
Or use Docker cron container.
HTTPS / Reverse Proxy
Put Nextcloud behind a reverse proxy (Traefik, Nginx Proxy Manager, Caddy):
# Example Traefik labels in docker-compose.yml
labels:
- "traefik.enable=true"
- "traefik.http.routers.nextcloud.rule=Host(\`cloud.example.com\`)"
- "traefik.http.routers.nextcloud.tls.certresolver=letsencrypt"
- "traefik.http.services.nextcloud.loadbalancer.server.port=80"
Then set overwrite.cli.url and overwriteprotocol in config.php.
Troubleshooting
Backup Fails: "Database dump failed"
- Check database credentials are correct
- Ensure
pg_dump(PostgreSQL) ormariadb-dump(MariaDB) is installed on TrueNAS - Verify database is accessible:
# PostgreSQL pg_dump -h <DB_HOST> -U <DB_USER> -d <DB_NAME> -c # MariaDB mariadb -h <DB_HOST> -u <DB_USER> -p<DB_PASS> <DB_NAME> -e "SELECT 1"
Backup Fails: "PVC not found"
- Check PVC names:
k3s kubectl get pvc -n nextcloud - Set
PVC_DATA,PVC_CONFIG,PVC_APPSexplicitly
Restore Fails: "Database not ready"
- Check database container logs:
docker-compose logs db - Ensure
DB_ROOT_PASSin.envmatches what you passed to restore script - PostgreSQL: check
pg_isreadyworks
Nextcloud Shows "Database Connection Failed"
- Verify
.envhas correctPOSTGRES_HOST=dborMYSQL_HOST=db - Check
POSTGRES_DB/MYSQL_DATABASE,POSTGRES_USER/MYSQL_USER,POSTGRES_PASSWORD/MYSQL_PASSWORD - Ensure database container is healthy:
docker-compose ps
Permission Errors on Files
docker run --rm -v /opt/nextcloud/nextcloud-data:/data alpine:latest \
sh -c "chown -R 33:33 /data && find /data -type d -exec chmod 750 {} \\; && find /data -type f -exec chmod 640 {} \\;"
"Trusted Domain" Error
Add your domain to config.php or use NEXTCLOUD_TRUSTED_DOMAINS in .env:
docker-compose exec nextcloud php occ config:system:set trusted_domains 1 --value="your-domain.com"
Large Backup Times Out
For very large instances, run restore in steps:
# Restore DB first
SKIP_FILES=true ./restore-nextcloud-docker.sh backup.tar.gz
# Then files
SKIP_DB=true ./restore-nextcloud-docker.sh backup.tar.gz
Security Notes
- Never commit
.envto version control - contains passwords - Use strong passwords for
DB_ROOT_PASSandDB_PASS - Restrict backup access - backup archives contain all user files and database
- Use HTTPS in production - put Nextcloud behind a reverse proxy
- Regular backups - Set up automated backups for the Docker deployment
- Update Nextcloud regularly -
docker-compose pull && docker-compose up -d
File Structure After Restore
/opt/nextcloud/
├── docker-compose.yml
├── .env # ← EDIT THIS FIRST
├── nextcloud_backup_*.tar.gz # Backup archive (can delete after verify)
├── nextcloud_backup_*.sha256 # Checksum
├── RESTORE_INFO_*.txt # Restore documentation
└── nextcloud-data/
├── data/
│ ├── user1/files/...
│ └── ...
├── config/
│ ├── config.php
│ └── ...
└── custom_apps/
└── ...
Support
If you encounter issues:
- Check the log output from both scripts
- Verify all prerequisites are met
- Ensure network connectivity between containers
- Check TrueNAS app logs:
k3s kubectl logs -n nextcloud -l app=nextcloud - Check Docker logs:
docker-compose logs nextcloud - Run
docker-compose exec nextcloud php occ statusfor Nextcloud health
License
MIT License - Feel free to use and modify for your needs.