mongodump -u username -p password --gzip --db databasename
mongodump a database

mongodump is a free utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances; i.e. can export data from standalone, replica set, and sharded cluster deployments.

How do you restore a mongodb database dump?

mongorestore --gzip --archive=file.gz --nsInclude 'database.*' --username username --password password
mongorestore a database

mongorestore utility restores a binary backup created by mongodump. The mongorestore utility restores data by connecting to a running mongod directly

How to take a mongodb database dump and restore it inside a docker container?

#mongorestore inside docker container
docker exec [container_name] sh -c 'mongorestore -d database -u user -p password --archive' < dump.gz
mongodump a database inside docker container

The docker exec command runs a new command in a running container.

The command started using docker exec only runs while the container’s primary process (PID 1) is running, and it is not restarted if the container is restarted.

Why does one need to take a database back up or restore it?

All online businesses growth and survival depend on your data and important information. During any kind of disaster, a recovery process and backups help to restore computing devices and also restore data after files have been destroyed or removed. Backups of servers and databases are important for data loss prevention, which can fully interrupt business operations. Failing to have a backup policy for databases is a recipe for disaster.