Explored in previous posts and videos, MongoDB is a great way to store information without a schema. All of the information is stored in a JSON like format called BSON which essentially are documents, but similar to a SQL Database the data cannot be directly read on the host system.
I was interested in learning some more about how to backup the files. Turns out that MongoDB provides a dump tool similar to MySQL called mongodump and mongorestore.
To get started one can run the following command on their host system or container. If you used the MongoDB Docker image, the mongodump command can be run inside the container directly.
Local Run
mongodump -u=<user> -p=<password> --authenticationDatabase=admin --db=<database to download>
The part '--authenticationDatabase' is important. On the MongoDB container the users allowed to access the database are specified are in that 'admin' database on the Mongo instance, and it must be defined in the mongodump command for the connection to be successful.
Similar to mysqldump, the mongodump command can also be used from a remote host.
Install the MongoDB Command Line Database Tools on the remote host. They offer the tools for Windows, Linux, and MacOS.
*Note on Fedora 36, using the RedHat/CentOS 8.0 package worked. *
Modify the previous command to the below:
Remote Run
mongodump --host=<Domain/IP> --port=<Mongo service port - default is 27017> -u=<user> -p=<password> --authenticationDatabase=admin --db=<database to download>
Assuming a connection can be made all of the database files and relevant metadata will come down to the directory where the command was run. A successful run will look similar to the below.
Once complete you will see a directory called 'dump' with the database(s) listed as directories inside.
To specify the output location on the local system (the system being used to access and backup the MongoDB instance) you may use the '-o' or '--output' flag.
Remote Run | Specify Download Location
mongodump --host=<Domain/IP>
--port=<Mongo service port> -u=<user>
-p=<password> --authenticationDatabase=admin --db=<database to
download> -o=<directory location>
Restoring data from a Mongodump
The companion utility to mongodump is mongorestore. Mongorestore works similarly to mongodump, but the user specifies the db and the back up files.
mongorestore --host <Domian/IP:Port> --authenticationDatabase=admin -u <user> -p <password> -d <db to restore> <backup folder location>
One additional tip:
If the container was installed using the same docker-compose from the PHP and MongoDB tutorial, the port for the MongoDB Instance isn't exposed on the local system. This is good from a security standpoint. The companion web server accesses using the hostname from the private Podman / Docker network. To access the container host from the physical machine you need to use the IP address of the MongoDB container itself.
Using either Docker or Podman, run the inspect command to check to find the Network information.
sudo podman inspect <container name or ID> | grep "IPAddress" OR
sudo docker inspect <container name or ID> | grep "IPAddress"
Additional information:
Information about using mongodump
Information about using mongorestore