I’m trying to refine the process by which I backup and restore Dockerized MongoDB containers. My previous effort is basically a brute-force copy-and-paste job on the container’s data directory. It works, but I’m concerned about restoring data between containers installed with different versions of MongoDB. Apparently this is tricky enough even with the benefit of recovery tools like
mongorestore, which is what I’m using below.
In short, I need to dump my data from a data-only MongoDB container, bundle the files uploaded to my Express application, and restore it all on another server. Here’s how I did it…
I’m a big fan of
docker-compose. I use it to manage all my containers. The following method requires that the composition be running so that
mongodump can be run against the running Mongo container (which, in turn, accesses the data-only container). Assuming the name of the container is
This will create a root-owned directory called
myapp-mongo-dump in your current directory. It contains all the BSON and JSON meta-data for this database. For convenience, I change ownership of this resource:
Then, for transport, I archive the directory:
My app allows file uploads, so the database is pointing to a bunch of files stored on the file system. My files are contained in a directory called
Now I have two archived files:
Here I use
In the previous command, for simplicity, I transferred the files into the user’s home folder. These will need to be moved into the root of the project folder on the new server. Once there, assuming the same app has been setup and deployed, I first unpack the uploaded files:
Then I restore the data to the data-only container through the running Mongo instance (assumed to be called
With that, all data is restored. I didn’t even have to restart my containers to begin using the app on its new server.