Files
2026-10-22 18:12:25 +03:00

411 lines
12 KiB
Markdown

# 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 kubectl` available (usually at `/usr/local/bin/k3s kubectl`)
- `pg_dump` (PostgreSQL) or `mariadb-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
```bash
# 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
```bash
# 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
```bash
# 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
```bash
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:
```bash
# 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:
1. **Detection**: Finds Kubernetes namespace, database credentials, and PVCs
2. **Maintenance Mode**: Enables `occ maintenance:mode --on` (configurable)
3. **Manifests**: Exports all K8s resources
4. **Database**: Dumps PostgreSQL or MariaDB database
5. **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)
6. **Archive**: Creates compressed `.tar.gz` with checksum
7. **Cleanup**: Disables maintenance mode, removes old backups
8. **Info**: Generates `RESTORE_INFO_<timestamp>.txt`
### Restore Process (Docker)
The restore script performs these steps:
1. **Validation**: Verifies backup archive integrity (checksum)
2. **Extraction**: Unpacks backup to temporary directory
3. **Docker Compose**: Generates `docker-compose.yml` and `.env` template
4. **Database**: Recreates database and restores from SQL dump
5. **Files**: Syncs data, config, and custom apps to host directory
6. **Permissions**: Fixes file ownership for www-data (UID 33)
7. **Guidance**: Shows next steps including `occ` commands
---
## 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
```bash
# 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
```bash
SKIP_DB=true \
./restore-nextcloud-docker.sh backup.tar.gz
```
### Restore Only Database
```bash
SKIP_FILES=true \
./restore-nextcloud-docker.sh backup.tar.gz
```
### Using MariaDB Instead of PostgreSQL
```bash
# 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
```bash
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:
```bash
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):
```bash
# 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):
```yaml
# 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"
1. Check database credentials are correct
2. Ensure `pg_dump` (PostgreSQL) or `mariadb-dump` (MariaDB) is installed on TrueNAS
3. Verify database is accessible:
```bash
# 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"
1. Check PVC names: `k3s kubectl get pvc -n nextcloud`
2. Set `PVC_DATA`, `PVC_CONFIG`, `PVC_APPS` explicitly
### Restore Fails: "Database not ready"
1. Check database container logs: `docker-compose logs db`
2. Ensure `DB_ROOT_PASS` in `.env` matches what you passed to restore script
3. PostgreSQL: check `pg_isready` works
### Nextcloud Shows "Database Connection Failed"
1. Verify `.env` has correct `POSTGRES_HOST=db` or `MYSQL_HOST=db`
2. Check `POSTGRES_DB`/`MYSQL_DATABASE`, `POSTGRES_USER`/`MYSQL_USER`, `POSTGRES_PASSWORD`/`MYSQL_PASSWORD`
3. Ensure database container is healthy: `docker-compose ps`
### Permission Errors on Files
```bash
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`:
```bash
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:
```bash
# 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
1. **Never commit `.env` to version control** - contains passwords
2. **Use strong passwords** for `DB_ROOT_PASS` and `DB_PASS`
3. **Restrict backup access** - backup archives contain all user files and database
4. **Use HTTPS in production** - put Nextcloud behind a reverse proxy
5. **Regular backups** - Set up automated backups for the Docker deployment
6. **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:
1. Check the log output from both scripts
2. Verify all prerequisites are met
3. Ensure network connectivity between containers
4. Check TrueNAS app logs: `k3s kubectl logs -n nextcloud -l app=nextcloud`
5. Check Docker logs: `docker-compose logs nextcloud`
6. Run `docker-compose exec nextcloud php occ status` for Nextcloud health
---
## License
MIT License - Feel free to use and modify for your needs.