I self-host several services in Docker containers, and some of them rely on a MySQL server. One of these containers is Wiki.js, where the frontend runs in a separate container while the database is managed by the MySQL container.
Wanting to back up the MySQL server, I initially committed the running Docker container under a different name, thinking this would create a backup that I could restore later. However, I quickly realized this wasn’t a proper backup solution. If I needed to download and run the backup elsewhere, I considered uploading the committed image to Docker Hub, but it wasn’t a practical approach.
After committing the container as a new image, I attempted to run it and reconnect it to the Wiki.js frontend. To do this, I stopped the running MySQL container and started the newly committed image. However, when the container ran, the frontend failed to connect to the SQL server. After debugging and researching online, I learned that committing a container does not back up the actual database data, since databases store their data in persistent volumes, not just inside the container itself. Here, I want to share the proper command to back up the MySQL database correctly, allowing you to save a copy and restore it in a new container when needed
Command to back up the MySQL container data to your designated folder:
docker exec <container-ID> /usr/bin/mysqldump -u root --password=password <database_name> > /path/example/backup.sql
Command to restore the backed-up data to the SQL server:
cat backup.sql | docker exec -i <container-ID> /usr/bin/mysql -u root --password=password <database_name>