# 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 kubectl` available (usually at `/usr/local/bin/k3s kubectl`) - `mariadb-dump` or `mysqldump` 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 --- ## 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 # 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 ```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://: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: 1. **Detection**: Finds Kubernetes namespace, database credentials, and PVCs 2. **Manifests**: Exports all K8s resources (deployments, services, PVCs, secrets, etc.) 3. **Database**: Dumps MariaDB/MySQL database with `mariadb-dump`/`mysqldump` 4. **Files**: Creates temporary pods to copy data from PVCs: - `uploads/` → Piwigo upload directory - `config/` → Piwigo local/config directory - `galleries/` → Piwigo galleries (if separate PVC) 5. **Archive**: Creates compressed `.tar.gz` with checksum 6. **Cleanup**: Removes old backups per retention policy 7. **Info**: Generates `RESTORE_INFO_.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 If you want to keep the same database passwords: ```bash # 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 ```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 ``` --- ## Troubleshooting ### Backup Fails: "Database dump failed" 1. Check database credentials are correct 2. Ensure `mariadb-dump` or `mysqldump` is installed on TrueNAS 3. Verify database is accessible from TrueNAS host: ```bash mariadb -h -u -p -e "SELECT 1" ``` ### Backup Fails: "PVC not found" 1. Check PVC names in TrueNAS: `k3s kubectl get pvc -n piwigo` 2. Set `PVC_UPLOADS`, `PVC_CONFIG`, `PVC_GALLERIES` 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. Increase startup wait time in script if needed ### 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 # 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: ```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: `k3s kubectl logs -n piwigo -l app=piwigo` 5. Check Docker logs: `docker-compose logs piwigo` --- ## License MIT License - Feel free to use and modify for your needs.