Skip to main content

Backup & Restore

Since TriviaFlow runs in Docker, backing up your data is straightforward. You don't need to stop the server to create a backup, ensuring zero downtime for your players.

Your application state consists of two parts:

  1. The Database: Contains all quizzes, questions, game history, and settings.
  2. Media Files: Contains all the images you uploaded to the Media Library.

💾 Creating a Backup

We recommend creating a backups/ folder on your host machine to store these files.

1. Database Backup (SQL)

You can use the mysqldump utility directly from inside the running container. This command exports the entire database into a single SQL file.

# syntax: docker compose exec [service_name] mysqldump [args] > [host_file]

docker compose exec db sh -c 'exec mysqldump "$MYSQL_DATABASE" -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > backup_$(date +%F).sql

Explanation:

  • exec db: Runs the command inside the database container.
  • $MYSQL_...: Uses the environment variables from your .env file (so you don't have to type passwords).
  • > backup_...sql: Saves the output to a file on your host machine with today's date.

2. Media Backup

Your images are stored in the media/ directory on your host (mapped via docker-compose.yml). To back them up, simply create a compressed archive of this folder.

tar -czf media_backup_$(date +%F).tar.gz media/
Automation

You can combine these two commands into a simple shell script and run it daily via cron to automate your backups.


♻️ Restoring Data

Whether you made a mistake or are migrating to a completely new server, here is how to get your data back.

1. Restore Database

To import an SQL backup, we pipe the file content back into the mysql command inside the container.

Warning

This operation will overwrite the current database. All current progress and quizzes created since the backup will be lost.

# 1. Stop the application to prevent data inconsistency (Optional but recommended)
docker compose stop web

# 2. Import the SQL file
docker compose exec -T db sh -c 'exec mysql "$MYSQL_DATABASE" -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' < backup_2025-01-01.sql

# 3. Start the application again
docker compose start web

Note: The -T flag is crucial here as it disables pseudo-TTY allocation, allowing the input redirection (<) to work.

2. Restore Media Files

Restoring media is as simple as extracting the archive back into your project folder.

# 1. Remove current media folder (to avoid ghosts)
rm -rf media/

# 2. Extract backup
tar -xzf media_backup_2025-01-01.tar.gz

# 3. Verify permissions (Docker needs to read them)
chmod -R 755 media/

🚨 Migration / Disaster Recovery

If you need to move TriviaFlow to a completely new server, follow these steps:

  1. Prepare the new server: Follow the Prerequisites and Docker Installation guides.
  2. Copy Files: Transfer the following items from your old server:
    • docker-compose.yml
    • Caddyfile
    • .env (Very important! Contains your SECRET_KEY and passwords)
    • Your *.sql database backup.
    • Your *.tar.gz media backup.
  3. Start Empty: Run docker compose up -d on the new server to initialize the containers and the database volume.
  4. Restore: Perform the Restore Database and Restore Media Files steps described above.
  5. Restart: Run docker compose restart to ensure the application loads the restored data correctly.

Your quiz platform is now live on the new server with all your data intact!