0870e7e7dfb7ce8bfa3656726ceab1db991b797e
TrueNAS Piwigo App → Docker Migration
This repository contains scripts to backup a Piwigo instance running as a TrueNAS App (Kubernetes/k3s) and restore it to a Docker container.
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
- TrueNAS with Piwigo app installed
- SSH access to TrueNAS (or use TrueNAS Shell)
k3s kubectlavailable (usually at/usr/local/bin/k3s kubectl)mariadb-dumpormysqldumpinstalled- Write access to backup destination dataset
On Docker Host
- Docker Engine installed
- Docker Compose (v2 plugin or standalone)
- Write access to Docker data directory
Quick Start
1. Backup on TrueNAS
# 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
# You can set environment variables to customize behavior
APP_NAMESPACE=piwigo \
BACKUP_DEST=/mnt/tank/backups/piwigo \
/tmp/backup-piwigo-truenas.sh
Environment Variables for Backup:
| Variable | Default | Description |
|---|---|---|
APP_NAMESPACE |
piwigo |
Kubernetes namespace of Piwigo app |
BACKUP_DEST |
/mnt/tank/backups/piwigo |
Destination dataset path |
DB_HOST |
auto-detect | Database host |
DB_PORT |
3306 |
Database port |
DB_NAME |
piwigo |
Database name |
DB_USER |
piwigo |
Database user |
DB_PASS |
auto-detect | Database password |
PVC_UPLOADS |
auto-detect | Uploads PVC name |
PVC_CONFIG |
auto-detect | Config PVC name |
PVC_GALLERIES |
auto-detect | Galleries PVC name |
RETENTION_COUNT |
7 |
Number of backups to keep |
COMPRESSION_LEVEL |
6 |
gzip compression (1-9) |
2. Transfer Backup to Docker Host
# 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
# 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
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)
The backup script performs these steps:
- Detection: Finds Kubernetes namespace, database credentials, and PVCs
- Manifests: Exports all K8s resources (deployments, services, PVCs, secrets, etc.)
- Database: Dumps MariaDB/MySQL database with
mariadb-dump/mysqldump - Files: Creates temporary pods to copy data from PVCs:
uploads/→ Piwigo upload directoryconfig/→ Piwigo local/config directorygalleries/→ Piwigo galleries (if separate PVC)
- Archive: Creates compressed
.tar.gzwith checksum - Cleanup: Removes old backups per retention policy
- Info: Generates
RESTORE_INFO_<timestamp>.txtwith restore instructions
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 uploads, config, and galleries to host directory
- Permissions: Fixes file ownership for www-data (UID 33)
- 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
If you want to keep the same database passwords:
# On TrueNAS, note the DB_PASS from backup output or secret
# 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
SKIP_DB=true \
./restore-piwigo-docker.sh backup.tar.gz
Restore Only Database
SKIP_FILES=true \
./restore-piwigo-docker.sh backup.tar.gz
Custom Data Directory
PIWIGO_DATA_DIR=/data/piwigo \
./restore-piwigo-docker.sh backup.tar.gz
Troubleshooting
Backup Fails: "Database dump failed"
- Check database credentials are correct
- Ensure
mariadb-dumpormysqldumpis installed on TrueNAS - Verify database is accessible from TrueNAS host:
mariadb -h <DB_HOST> -u <DB_USER> -p<DB_PASS> <DB_NAME> -e "SELECT 1"
Backup Fails: "PVC not found"
- Check PVC names in TrueNAS:
k3s kubectl get pvc -n piwigo - Set
PVC_UPLOADS,PVC_CONFIG,PVC_GALLERIESexplicitly
Restore Fails: "Database not ready"
- Check database container logs:
docker-compose logs db - Ensure
DB_ROOT_PASSin.envmatches what you passed to restore script - Increase startup wait time in script if needed
Piwigo Shows "Database Connection Failed"
- Verify
.envhas correctDB_HOST=db(service name) - Check
DB_NAME,DB_USER,DB_PASSmatch restore values - Ensure database container is healthy:
docker-compose ps
Permission Errors on Files
# Fix permissions manually
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
For very large galleries, increase Docker Compose healthcheck timeouts or run restore in steps:
# 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
- Never commit
.envto version control - contains passwords - Use strong passwords for
DB_ROOT_PASSandDB_PASS - Restrict backup access - backup archives contain all photos and database
- Use HTTPS in production - put Piwigo behind a reverse proxy (Traefik, Nginx Proxy Manager, Caddy)
- Regular backups - Set up automated backups for the Docker deployment
Advanced: Reverse Proxy (HTTPS)
Example with Traefik (add to docker-compose.yml):
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:
- 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 piwigo -l app=piwigo - Check Docker logs:
docker-compose logs piwigo
License
MIT License - Feel free to use and modify for your needs.
Languages
Shell
98%
Dockerfile
2%