391 lines
11 KiB
Markdown
391 lines
11 KiB
Markdown
# TrueNAS Piwigo App → Docker Migration
|
|
|
|
This repository contains scripts to backup a Piwigo instance running as a TrueNAS App and restore it to a Docker container.
|
|
|
|
**Updated for TrueNAS 27+ (Electric Eel)** - No kubectl/k3s required. Works with direct dataset access.
|
|
|
|
## Scripts
|
|
|
|
| Script | Purpose | Run On |
|
|
|--------|---------|--------|
|
|
| `backup-piwigo-truenas.sh` | Backup Piwigo from TrueNAS App | TrueNAS host |
|
|
| `restore-piwigo-docker.sh` | Restore Piwigo to Docker | Docker host |
|
|
|
|
---
|
|
|
|
## Prerequisites
|
|
|
|
### On TrueNAS Host (27+)
|
|
- TrueNAS 27+ with Piwigo app installed
|
|
- SSH access to TrueNAS (or TrueNAS Shell)
|
|
- `docker` available (for database dump via container)
|
|
- **OR** `mariadb-dump`/`mysqldump` installed on host
|
|
- Write access to backup destination dataset
|
|
- Read access to Piwigo app datasets (configured in app settings)
|
|
|
|
### On Docker Host
|
|
- Docker Engine installed
|
|
- Docker Compose (v2 plugin or standalone)
|
|
- Write access to Docker data directory
|
|
|
|
---
|
|
|
|
## Quick Start
|
|
|
|
### 1. Backup on TrueNAS
|
|
|
|
```bash
|
|
# SSH into TrueNAS
|
|
ssh root@truenas.local
|
|
|
|
# Copy the backup script to TrueNAS
|
|
scp backup-piwigo-truenas.sh root@truenas.local:/tmp/
|
|
|
|
# Run the backup script with your dataset paths
|
|
APP_NAMESPACE=piwigo \
|
|
BACKUP_DEST=/mnt/tank/backups/piwigo \
|
|
PIWIGO_UPLOADS_PATH=/mnt/.ix-apps/app_mounts/piwigo/config/upload \
|
|
PIWIGO_CONFIG_PATH=/mnt/.ix-apps/app_configs/piwigo/versions/1.4.17 \
|
|
PIWIGO_GALLERIES_PATH=/mnt/.ix-apps/app_mounts/piwigo/config/galleries \
|
|
DB_PASS=your_database_password \
|
|
/tmp/backup-piwigo-truenas.sh
|
|
```
|
|
|
|
**Environment Variables for Backup:**
|
|
| Variable | Default | Description |
|
|
|----------|---------|-------------|
|
|
| `BACKUP_DEST` | `/mnt/tank/backups/piwigo` | Destination dataset path |
|
|
| `DB_HOST` | `localhost` | Database host (if using host tools) |
|
|
| `DB_PORT` | `3306` | Database port |
|
|
| `DB_NAME` | `piwigo` | Database name |
|
|
| `DB_USER` | `piwigo` | Database user |
|
|
| `DB_PASS` | **required** | Database password (or auto-detected) |
|
|
| `DB_CONTAINER` | auto-detect | Database container name. Auto-detects TrueNAS 27+ style (`ix-*-mariadb`, `ix-*-mysql`) then falls back to generic patterns |
|
|
| `DB_PASSWORD_ENV` | auto-detect | Password env var in container (`MYSQL_PASSWORD`, `MYSQL_ROOT_PASSWORD`, `MARIADB_PASSWORD`, etc.) |
|
|
| `PIWIGO_UPLOADS_PATH` | **required** | Host path to uploads dataset |
|
|
| `PIWIGO_CONFIG_PATH` | **required** | Host path to config dataset |
|
|
| `PIWIGO_GALLERIES_PATH` | optional | Host path to galleries dataset |
|
|
| `RETENTION_COUNT` | `7` | Number of backups to keep |
|
|
| `COMPRESSION_LEVEL` | `6` | gzip compression (1-9) |
|
|
|
|
### 2. Transfer Backup to Docker Host
|
|
|
|
```bash
|
|
# From your workstation, pull the backup from TrueNAS
|
|
scp root@truenas.local:/mnt/tank/backups/piwigo/piwigo_backup_*.tar.gz* .
|
|
|
|
# Copy to Docker host
|
|
scp piwigo_backup_*.tar.gz* user@docker-host:/opt/piwigo/
|
|
```
|
|
|
|
### 3. Restore on Docker Host
|
|
|
|
```bash
|
|
# SSH into Docker host
|
|
ssh user@docker-host
|
|
|
|
# Make scripts executable
|
|
chmod +x restore-piwigo-docker.sh
|
|
|
|
# Run restore (set required passwords)
|
|
DB_ROOT_PASS=your_root_password \
|
|
DB_PASS=your_piwigo_password \
|
|
./restore-piwigo-docker.sh /opt/piwigo/piwigo_backup_20240115_120000.tar.gz
|
|
```
|
|
|
|
**Environment Variables for Restore:**
|
|
| Variable | Default | Description |
|
|
|----------|---------|-------------|
|
|
| `DOCKER_DIR` | `/opt/piwigo` | Docker project directory |
|
|
| `COMPOSE_FILE` | `docker-compose.yml` | Compose filename |
|
|
| `DB_CONTAINER` | `piwigo-db` | Database container name |
|
|
| `PIWIGO_CONTAINER` | `piwigo` | Piwigo container name |
|
|
| `DB_ROOT_PASS` | **required** | MariaDB root password |
|
|
| `DB_NAME` | `piwigo` | Database name |
|
|
| `DB_USER` | `piwigo` | Database user |
|
|
| `DB_PASS` | **required** | Database user password |
|
|
| `PIWIGO_DATA_DIR` | `${DOCKER_DIR}/piwigo-data` | Host data directory |
|
|
| `SKIP_DB` | `false` | Skip database restore |
|
|
| `SKIP_FILES` | `false` | Skip files restore |
|
|
| `FORCE` | `false` | Skip confirmations |
|
|
|
|
### 4. Start Piwigo
|
|
|
|
```bash
|
|
cd /opt/piwigo
|
|
|
|
# Edit .env with your passwords (IMPORTANT!)
|
|
nano .env
|
|
|
|
# Start containers
|
|
docker-compose up -d
|
|
|
|
# Check logs
|
|
docker-compose logs -f piwigo
|
|
```
|
|
|
|
### 5. Access Piwigo
|
|
|
|
Open your browser to `http://<docker-host>:8080`
|
|
|
|
- If database was restored: Your existing Piwigo with all photos/users
|
|
- If fresh database: Piwigo installer will appear
|
|
|
|
---
|
|
|
|
## Detailed Workflow
|
|
|
|
### Backup Process (TrueNAS 27+)
|
|
|
|
The backup script performs these steps:
|
|
|
|
1. **Validate Paths**: Checks provided dataset paths or auto-detects common locations
|
|
2. **Validate Database**: Tests connection via host tools OR docker exec
|
|
3. **Database**: Dumps MariaDB/MySQL database using:
|
|
- Host tools (`mariadb-dump`/`mysqldump`) if available
|
|
- `docker exec` into database container if host tools not available
|
|
4. **Files**: Copies data directly from host filesystem paths using `rsync`:
|
|
- `uploads/` → Piwigo upload directory
|
|
- `config/` → Piwigo local/config directory
|
|
- `galleries/` → Piwigo galleries (if separate path)
|
|
5. **Archive**: Creates compressed `.tar.gz` with SHA256 checksum
|
|
6. **Cleanup**: Removes old backups per retention policy
|
|
7. **Info**: Generates `RESTORE_INFO_<timestamp>.txt` with restore instructions
|
|
|
|
### 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 uploads, config, and galleries to host directory
|
|
6. **Permissions**: Fixes file ownership for www-data (UID 33)
|
|
7. **Guidance**: Shows next steps to start containers
|
|
|
|
---
|
|
|
|
## Docker Compose Structure
|
|
|
|
The generated `docker-compose.yml` creates:
|
|
|
|
```
|
|
piwigo/
|
|
├── docker-compose.yml
|
|
├── .env # Your passwords (EDIT THIS!)
|
|
├── piwigo-data/ # Host-mounted Piwigo data
|
|
│ ├── upload/ # Photos and derivatives
|
|
│ ├── local/config/ # Piwigo configuration
|
|
│ └── galleries/ # Gallery structure (if used)
|
|
└── db-data/ # Database volume (Docker managed)
|
|
```
|
|
|
|
---
|
|
|
|
## Common Scenarios
|
|
|
|
### Migration with Same Database Credentials
|
|
|
|
```bash
|
|
# On TrueNAS, set DB_PASS
|
|
# On Docker host, use same passwords:
|
|
DB_ROOT_PASS=same_as_truenas \
|
|
DB_PASS=same_as_truenas \
|
|
./restore-piwigo-docker.sh backup.tar.gz
|
|
```
|
|
|
|
### Fresh Database, Restore Only Files
|
|
|
|
```bash
|
|
SKIP_DB=true \
|
|
./restore-piwigo-docker.sh backup.tar.gz
|
|
```
|
|
|
|
### Restore Only Database
|
|
|
|
```bash
|
|
SKIP_FILES=true \
|
|
./restore-piwigo-docker.sh backup.tar.gz
|
|
```
|
|
|
|
### Custom Data Directory
|
|
|
|
```bash
|
|
PIWIGO_DATA_DIR=/data/piwigo \
|
|
./restore-piwigo-docker.sh backup.tar.gz
|
|
```
|
|
|
|
---
|
|
|
|
## Finding TrueNAS 27+ Dataset Paths
|
|
|
|
In TrueNAS 27+, app data is stored in `.ix-apps` datasets. Find your paths:
|
|
|
|
```bash
|
|
# List app mounts
|
|
ls /mnt/.ix-apps/app_mounts/
|
|
|
|
# List app configs
|
|
ls /mnt/.ix-apps/app_configs/
|
|
|
|
# For Piwigo, typical paths are:
|
|
# /mnt/.ix-apps/app_mounts/piwigo/config/upload
|
|
# /mnt/.ix-apps/app_configs/piwigo/versions/X.X.X
|
|
# /mnt/.ix-apps/app_mounts/piwigo/config/galleries
|
|
```
|
|
|
|
---
|
|
|
|
## Database Credentials
|
|
|
|
### Option 1: Set DB_PASS directly (recommended)
|
|
```bash
|
|
DB_PASS=your_password ./backup-piwigo-truenas.sh
|
|
```
|
|
|
|
### Option 2: Auto-detect from container
|
|
The script checks these env vars in the database container:
|
|
- `MYSQL_PASSWORD`
|
|
- `MYSQL_ROOT_PASSWORD`
|
|
- `MARIADB_PASSWORD`
|
|
- `MARIADB_ROOT_PASSWORD`
|
|
- Docker secret files (`*_FILE` variants)
|
|
|
|
### Option 3: Specify container and env var
|
|
```bash
|
|
DB_CONTAINER=ix-piwigo-mariadb-1 \
|
|
DB_PASSWORD_ENV=MYSQL_ROOT_PASSWORD \
|
|
./backup-piwigo-truenas.sh
|
|
```
|
|
|
|
---
|
|
|
|
## Troubleshooting
|
|
|
|
### Backup Fails: "Database dump failed"
|
|
|
|
1. Check database credentials are correct
|
|
2. Ensure `mariadb-dump`/`mysqldump` is installed on TrueNAS **OR** docker is available
|
|
3. Verify database is accessible from TrueNAS host:
|
|
```bash
|
|
# If host tools available
|
|
mariadb -h localhost -u piwigo -p<DB_PASS> piwigo -e "SELECT 1"
|
|
|
|
# If using docker (TrueNAS 27+ container name pattern)
|
|
docker exec ix-piwigo-mariadb-1 mariadb -u piwigo -p<DB_PASS> -e "SELECT 1"
|
|
```
|
|
|
|
### Backup Fails: "Path does not exist"
|
|
|
|
1. Verify dataset paths in TrueNAS app configuration
|
|
2. Check `/mnt/.ix-apps/app_mounts/` and `/mnt/.ix-apps/app_configs/`
|
|
3. Set `PIWIGO_UPLOADS_PATH`, `PIWIGO_CONFIG_PATH`, `PIWIGO_GALLERIES_PATH` explicitly
|
|
|
|
### Backup Fails: "Cannot connect to database in container"
|
|
|
|
1. Check container name: `docker ps | grep -iE 'ix-.*mariadb|ix-.*mysql'`
|
|
2. Verify password env var name: `docker inspect <container> | grep -i password`
|
|
3. Try setting `DB_PASSWORD_ENV=MYSQL_ROOT_PASSWORD` (common for root password)
|
|
4. Script now auto-detects `ix-*-mariadb` and `ix-*-mysql` containers
|
|
|
|
### 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
|
|
|
|
### Piwigo Shows "Database Connection Failed"
|
|
|
|
1. Verify `.env` has correct `DB_HOST=db` (service name)
|
|
2. Check `DB_NAME`, `DB_USER`, `DB_PASS` match restore values
|
|
3. Ensure database container is healthy: `docker-compose ps`
|
|
|
|
### Permission Errors on Files
|
|
|
|
```bash
|
|
docker run --rm -v /opt/piwigo/piwigo-data:/data alpine:latest \
|
|
sh -c "chown -R 33:33 /data && chmod -R 755 /data"
|
|
```
|
|
|
|
### Large Backup Times Out
|
|
|
|
```bash
|
|
# Restore DB first
|
|
SKIP_FILES=true ./restore-piwigo-docker.sh backup.tar.gz
|
|
# Then files
|
|
SKIP_DB=true ./restore-piwigo-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 photos and database
|
|
4. **Use HTTPS in production** - put Piwigo behind a reverse proxy (Traefik, Nginx Proxy Manager, Caddy)
|
|
5. **Regular backups** - Set up automated backups for the Docker deployment
|
|
|
|
---
|
|
|
|
## Advanced: Reverse Proxy (HTTPS)
|
|
|
|
Example with Traefik (add to `docker-compose.yml`):
|
|
|
|
```yaml
|
|
services:
|
|
piwigo:
|
|
# ... existing config ...
|
|
labels:
|
|
- "traefik.enable=true"
|
|
- "traefik.http.routers.piwigo.rule=Host(\`photos.example.com\`)"
|
|
- "traefik.http.routers.piwigo.tls.certresolver=letsencrypt"
|
|
- "traefik.http.services.piwigo.loadbalancer.server.port=80"
|
|
networks:
|
|
- piwigo-net
|
|
- traefik-net # external network
|
|
|
|
networks:
|
|
traefik-net:
|
|
external: true
|
|
```
|
|
|
|
---
|
|
|
|
## File Structure After Restore
|
|
|
|
```
|
|
/opt/piwigo/
|
|
├── docker-compose.yml
|
|
├── .env # ← EDIT THIS FIRST
|
|
├── piwigo_backup_*.tar.gz # Backup archive (can delete after verify)
|
|
├── piwigo_backup_*.sha256 # Checksum
|
|
├── RESTORE_INFO_*.txt # Restore documentation
|
|
└── piwigo-data/
|
|
├── upload/
|
|
│ ├── 2024/01/15/...
|
|
│ └── ...
|
|
├── local/config/
|
|
│ ├── config.inc.php
|
|
│ └── ...
|
|
└── galleries/
|
|
└── ...
|
|
```
|
|
|
|
---
|
|
|
|
## 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 in TrueNAS UI
|
|
5. Check Docker logs: `docker-compose logs piwigo`
|
|
|
|
---
|
|
|
|
## License
|
|
|
|
MIT License - Feel free to use and modify for your needs. |