Downloading a Bson Collection with mongodmp in mongocompass and kubernetes Databank

2/27/2022

I am trying to backup a database from a kubernetes cluster to my computer as a bson file. I have connected my mongodb compass to the kubernetes cluster using port-forwarding. Can anyone help me with the command I need to download a particular Collection (450gb) from a databank to my desktop?

I've been trying for a while now but I cant seem to find the way around it.

In mongocompass there is unfortunately no way to download a collection as a bson file. The port I have forwarded the kubernetes pod to is 27017.

-- Codein
kubernetes
mongodb
mongodb-compass
mongodump

1 Answer

2/28/2022

From the mongodb official docs:

Run mongodump from the system command line, not the mongo shell.

So, assuming you configured Kubernetes Port Forwarding on your local machine like this:

$ kubectl port-forward service/mongo 28015:27017

And you've got output similar to this:

Forwarding from 127.0.0.1:28015 -> 27017
Forwarding from [::1]:28015 -> 27017

You can now export data from mongodb using the following command:

$ mongodump --username root --port=28015 -p secretpassword

This will create a dump directory in the current working directory and export data there.

Also, to export only specific collection use the following option:

--collection=<collection>, -c=<collection>

Specifies a collection to backup. If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.

You can check other available option here.

-- mozello
Source: StackOverflow