How to mongodump from OpenShift and mongorestore locally on MongoDB 2.4.9?

4.3k views Asked by At

I just did this myself (as a RockMongo export and import was corrupted) so just posting here.

Note this was for MongoDB verison 2.4.9 with corresponding versions of mongodump and mongorestore.

1

There are 1 answers

1
user1063287 On BEST ANSWER

Read the documentation relevant to your versions first, backup, make sure the solution below is relevant to your scenario etc.

http://docs.mongodb.org/v2.4/reference/program/mongodump/
http://docs.mongodb.org/v2.4/reference/program/mongorestore/

Getting the OC command line interface

The steps below use the OpenShift command line interface (to run the oc commands).

The download link for your relevant operating system can be found by going to:

OpenShift Online Console > [Click on help icon] > Command Line Tools

That location also contains the command required to login.

It looks something like this:

oc login https://api.pro-ap-southeast-2.openshift.com --token=******

You can then run the following to display all the pods in your project:

oc get pods

BEGIN 20/11/18 update

I just had to revisit these steps again, the following may be helpful to others:

01) To view all MongoDB environment variables, from local computer run:

oc exec mongodb-XX-XXXXX env 

(gleaned from comments here)

02) To perform the dump, go to pod terminal in openshift console:

OpenShift Online Console > Applications > Pods > MongoDB-**-***** > Terminal

and enter this:

mongodump --host MONGODB_SERVICE_HOST:MONGODB_SERVICE_PORT --username admin --password "MONGODB_ADMIN_PASSWORD"

replacing the variable names with the actual values displayed from running the previous command.

I had to use the username admin rather than the environment variable value for MONGODB_USER.

I got this returned in terminal when running the above command:

2023-01-14T06:09:37.689+0000    writing admin.system.users to 
2023-01-14T06:09:37.690+0000    done dumping admin.system.users (2 documents)
2023-01-14T06:09:37.690+0000    writing admin.system.version to 
2023-01-14T06:09:37.692+0000    done dumping admin.system.version (2 documents)
2023-01-14T06:09:37.692+0000    writing <my-db-name-1>.<my-collection-name-1> to 
2023-01-14T06:09:37.692+0000    writing <my-db-name-2>.<my-collection-name-2> to 
2023-01-14T06:09:37.692+0000    writing <my-db-name-3>.<my-collection-name-3> to 
2023-01-14T06:09:37.692+0000    writing users.users to 
2023-01-14T06:09:37.694+0000    done dumping <my-db-name-1>.<my-collection-name-1> (100 documents)
2023-01-14T06:09:37.694+0000    writing <my-db-name-4>.<my-collection-name-4> to 
2023-01-14T06:09:37.695+0000    done dumping <my-db-name-4>.<my-collection-name-4> (3 documents)
2023-01-14T06:09:37.695+0000    writing <my-db-name-5>.<my-collection-name-5> to 
2023-01-14T06:09:37.696+0000    done dumping <my-db-name-5>.<my-collection-name-5> (1 document)
2023-01-14T06:09:37.707+0000    done dumping users.users (6 documents)
2023-01-14T06:09:37.709+0000    done dumping <my-db-name-3>.<my-collection-name-3> (13 documents)
2023-01-14T06:09:37.716+0000    done dumping <my-db-name-2>.<my-collection-name-2> (78 documents)

03) If you want to zip the dump folder, do this from pod terminal in console:

# sanity check to see where you are
pwd  
# returns /opt/app-root/src

# list the directory contents
ls 
# returns dump 

# zip the folder 
tar czf my_dump.tar.gz dump

# list the directory contents again
ls
# returns dump  my_dump.tar.gz  

(gleaned from comments here)

04) To download the folder, from local PC terminal, do this:

oc rsync mongodb-20-XXXXX:/opt/app-root/src/dump /c/Users/Your-Directory

Or if you want to download the zip file instead, do this:

oc rsync mongodb-20-XXXXX:/opt/app-root/src/my_dump.tar.gz /c/Users/Your-Directory  

(gleaned from official docs and blog post here)

For reference, if you wanted to do a mongoexport of a collection to a .json file (as opposed to a mongodump of all databases), you can do the following from the pod terminal and then download the file using oc rsync as shown above:

mongoexport --host ***.**.**.** --port 27017 --authenticationDatabase admin --username admin --password "*******" --db <your-db-name> --collection <your-collection-name> --out my_collection_export.json

Random update (14/01/23):

Annoyingly, I cannot see what version of mongodump is being used when I run the following from the pod terminal:

mongodump --version
mongodump version: built-without-version-string
git version: built-without-git-spec
Go version: go1.8.5
   os: linux
   arch: amd64
   compiler: gc
OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013

So I don't know if that will be problematic when it comes to doing a mongorestore and needing to ensure that the mongodump and mongorestore versions are compatible :/.

END 20/11/18 update


SSH In

rhc ssh [app-name]
cd app-root/repo/

Check what version of mongodump you have:

mongodump --version
mongodump version 2.4.9

mongodump

The command below will dump *ALL* databases.

mongodump --host $OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT --username $OPENSHIFT_MONGODB_DB_USERNAME --password $OPENSHIFT_MONGODB_DB_PASSWORD  

Zip Dump Folder

zip -r dump.zip dump

Exit SSH

exit

Download via SCP

(Replace the environment variable below with the actual value).

scp [email protected]:~/app-root/repo/dump.zip /var/www/html

SSH back in and delete dump files

rhc ssh [app-name]
cd app-root/repo/
rm -r dump 
rm -r dump.zip

In local command line, go to the directory where you downloaded the zip file:

cd /var/www/html

Unzip Dump Folder

unzip dump.zip -d dump

See what version of mongorestore you have and that everything is compatible:

mongorestore --version
mongorestore version 2.4.9

At this point, I deleted all my local *corresponding* databases in RockMongo so that the restore process would create them from scratch.

mongorestore

mongorestore dump

The default host and port used is localhost and 27017.