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:
- The Database: Contains all quizzes, questions, game history, and settings.
- 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.envfile (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/
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.
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:
- Prepare the new server: Follow the Prerequisites and Docker Installation guides.
- Copy Files: Transfer the following items from your old server:
docker-compose.ymlCaddyfile.env(Very important! Contains yourSECRET_KEYand passwords)- Your
*.sqldatabase backup. - Your
*.tar.gzmedia backup.
- Start Empty: Run
docker compose up -don the new server to initialize the containers and the database volume. - Restore: Perform the Restore Database and Restore Media Files steps described above.
- Restart: Run
docker compose restartto ensure the application loads the restored data correctly.
Your quiz platform is now live on the new server with all your data intact!