Backing up a little thing called Mongo : A Basic Backup and Restore of MongoDB

5 minute read time.

Today we are going to discuss how to backup and restore Mongo DB. For those who do not know what Mongo DB is, please visit their website here (https://www.mongodb.com/what-is-mongodb). Sage Enterprise Management (Sage X3) uses Mongo DB as a storage database for the Administration Module. It also acts as part of the first layer of security along side the Syracuse Component. Just like Microsoft SQL and Oracle DB; Mongo DB also needs to be backed up. Backing up Mongo DB is not super complicated but does require a bit of knowledge of the Mongo DB directory, Syracuse directory and how to use the Windows command prompt or Linux BASH terminal.

For the sake of this blog, we will be using windows OS with Mongo DB installed.

The first thing we want to do is to locate the “mongodump.exe” executable within the MongoDB directory.

Here is an example of what it may look like:

 

Note: The “mongodump.exe” executable is a standard function within MongoDB it allows you to “dump” the database and to a location. It formats the exported tables as a BSON file type. BSON is easily compressed for easy storage. The same BSON files can be used with the “mongorestore.exe” executable to “restore” the “dump” back to Mongo DB. You can read more here (https://docs.mongodb.com/manual/reference/program/mongodump/)

We are going to want to remember the path in the address bar at the top (highlighted blue). We are going to use this address to launch the mongodump.exe from the command.exe function within Windows OS. Now we want to go to Windows Start bar and click on the Start icon. In the search field we will be searching for cmd.exe. Then we will right click and run as administrator. We run as administrator to make sure we have enough rights.

You should have a black box window that is the command prompt (CMD).

At this point we are going to change the directory to one where mongodump.exe is located.

In our example it is: C:\Sage\FreeComponents\X3ERPV11MDBSVR\mongodb-win32-x86_64-2008plus-ssl-3.2.6\bin

In the command prompt (CMD) we will type the following:

Cd C:\Sage\FreeComponents\X3ERPV11MDBSVR\mongodb-win32-x86_64-2008plus-ssl-3.2.6\bin

The CMD should now look like so.

At this point there is a few options that we are presented with.

  1. Mongo DB has an SSL certificate
  2. Mongo DB does not have an SSL certificate

If Mongo DB was configured with an SSL certificate in place, then the request to dump will be different than without an SSL certificate.

 

We will start with option #1 “with SSL”.

We are going to follow this format when setting up the CMD command we are going to run

Mongodump.exe –host [servername]:[mongodb_port] –db.syracuse /ssl –sslPEMKeyFile [location_of_PEMKEY_for_mongodb] –out [where_you_want_the_dump_to_be_saved]

Some of the information will be pretty standard. The MongoDB port for example is 27017 by default (which we use) and the PEMKEY can be found in the Syracuse cert folder. If your running the backup on the same machine you can sometimes get away with localhost as the servername.

This is what we should have so far.

mongodump.exe --host localhost:27017

To find the PEMKEY we are going to have to investigate the Syracuse directory to find it. Syracuse has a folder labeled certs and should be somewhat easy to find. Note: Syracuse may be on a separate server.

Example location.

Then we drill into the mongodb folder

 

Then we should be able to find the client.pem key

 

Note the file path in the address bar because we will need it for the CMD command as well

 

So right now, our command should look something like this:

mongodump.exe --host localhost:27017 --db syracuse /ssl --sslPEMKeyFile C:\Sage\SAFEX3\X3ERPV11SYRSVR\syracuse\certs\mongodb\client.pem

 

The “--db Syracuse” and “/ssl –sslPEMKeyFile” are standard format. The db.Syracuse is the name that Sage EM/X3 uses for the name of the database and the /ssl –sslPEMKeyFile is the call for the SSL Certification authentication to be used. We are connecting with the client.pem as authorization.

The last part to add is the –out [location]. We need to tell Mongodump were to dump everything.

So, our final command should look like this:

mongodump.exe --host localhost:27017 --db syracuse /ssl --sslPEMKeyFile C:\Sage\SAFEX3\X3ERPV11SYRSVR\syracuse\certs\mongodb\client.pem --out c:\mongoback

Now we will copy/paste or type in our command and hit enter to run it.

Note: if an SSL Certification is not being used then you do not include the /ssl command and PEMKey in your command line.

                e.x.: mongodump.exe --host localhost:27017 --db syracuse --out c:\mongoback

If all went correctly you should see a log running through that is going to dump each collection (table) to a BSON format file.

……………………..

Once the CMD resets we are safe to go and look at our “—out” directory.

There should be one BSON and one JSON file for each collection (table) in the database.

At this point we are done dumping (backing up) the Mongo DB database.

Normal situation the DBA or IT would then archive these files for safety (compress and move off the network).

 

We can now safely move on to our next section, which is restoring a backup (dump).

We will go ahead and use the same dump we just made to restore back to the same DB.

To restore a Mongo DB Database is just the opposite of what we just did. It will have the same format and structure in CMD as the mongodump.exe.

We are going to follow the same protocols.

Here is an example:

mongorestore.exe --port 27017 /ssl --sslPEMKeyFile C:\Sage\SAFEX3\X3ERPV11SYRSVR\syracuse\certs\mongodb\client.pem /d syracuse --drop C:\mongoback\syracuse

 

We are going to drop the HOST and just keep the port.

We then add “/d syracuse –drop” to drop the old database

Then lastly, we add the location of out dump.

Note that the path is down to the files not the original location.

C:\mongoback\syracuse

 

If done correctly you will get a running log like so.

……………………………..

Done!

If you want to have a GUI to confirm your findings you can download (for free) a GUI application for Mongo DB called Robomongo. It’s a nice freeware that adds a GUI interface like how MS SQL has Microsoft SQL Management Studio.

You can read more on Robomongo here (https://robomongo.org)

I have another blog post where I discuss more about Robomongo which you can read here:

https://www.sagecity.com/support_communities/sage_erp_x3/b/sageerp_x3_product_support_blog/posts/how-do-you-find-stuff-in-mongo-db