Samiul Alam
Samiul Alam

Reputation: 2434

How to use mongodump command from Nodejs app container?

I have a Node js app running in a docker container that depends on mongo container which uses the image mongo from docker hub. I want to make a rest API GET: download/db. When a get request hits I want it to download the dumped backup copy of database.

For that, I used shell command

mongodump --host localhost --uri mongodb://localhost:27017/db_name --gzip --out /tmp/backup-007434030202704722

But it shows an error /bin/sh: mongodump: not found. I don't know what is the problem here. What might be the problem?

Upvotes: 2

Views: 2742

Answers (2)

OmkieIT Solutions
OmkieIT Solutions

Reputation: 434

The documentation mentions that the user or roles needs to have the 'find' action allowed, or to use the 'backup' role: https://docs.mongodb.com/manual/reference/program/mongodump/#required-access

Login to your server - root via putty or SSH

The 'backup' role would need to be granted to any non-admin users, but it seems it's not needed for the main admin user I setup and I was able to use the following:

mongodump -u "admin" --authenticationDatabase "admin"

This will prompt the password and once you enter the BACKUP dump is created in the server...

A new directory named "dump" in working directory path and looks to have dumped all databases into it. This is currently at /root/dump/ as an example of what it contains, and more examples of using the command can be seen on https://docs.mongodb.com/manual/reference/program/mongodump/#mongodump-with-access-control

If you want to take the Backup individually then use the following process:

The --db flag can be added to specify just one or a few databases, and --out flag can be added to specify an output directory, so if you instead wanted to create the dumps of all databases in a specific directory (/backup/mongodumps for example) you would do something like the following:

mongodump -u "admin" --authenticationDatabase "admin" --out /backup/mongodumps/

or if you just wanted one database to a specifc directory:

mongodump -u "admin" --authenticationDatabase "admin" --db [DB name] --out /backup/mongodumps/

There are also other examples at https://docs.mongodb.com/manual/reference/program/mongodump/#mongodump-with-access-control which include compressing the dumps, or dumping them to a single archive.

Additional info:

Otherwise if you want to create dumps as a user other than admin, then the 'backup' role will need to be granted to those other users.

Upvotes: 0

Thilak
Thilak

Reputation: 995

I guess you are running this shell command in your node container, but mongodump will not be a part of the node image.

You might have to modify your shell command something like this:

docker run -d --rm -v mongo-backup:/mongo-backup mongo mongodump --db db_name --gzip --out /mongo-backup/backup-007434030202704722

And if you add mongo-backup volume to your Node JS application container, then you can see this backup file in your node js container.

Upvotes: 1

Related Questions