MongoDB Restore


At T1CG we constantly maintaining and updating our systems. This leads to vast amounts of system configuration of databases and images. In this blog we will discuss Mongo backup, restores, and linux volume configuration.

Intro to MongoDB

MongoDB is an open-source NoSQL database that allows for scalability and flexability. Many of you have probably heard of more common SQL databases such as MySQL, Oracle, and Microsoft SQL Server. But NoSQL is a database that does not have relationships. With NoSQL databases becoming more commonly used by companies, Mongo is becoming an industry standard. Mongo can be managed either through a command line terminal or third party tools such as Robo3T. In this blog post we will cover Mongo backups and restoring that backup to a server. You will need to have Mongo install locally on your machine. Please follow the link below to the Mongo docs for easy install and setup guides.

MongoDB Installation

MongoDB Shell Instructions

MongoDB Backups

Mongo Backups are called Dumps therefore I will use the words interchangeably. Mongo does has the ability to “mongoimport” and “mongoexport” but those are not recommended because they do not maintain datatype integrity. Therefore we will focus on “mongodump” to backup. Mongodump’s can create backups for an entire server, a database, or a collection(table). Dumps must be run on a command line while the mongod service is running. If you ran a mongodump without any specifications it will run on the local system using port 27017. In order to specify port, output location, etc you must add flags using – -. An example to put the back in the directory of /data/backup would be:

mongodump --out /data/backup/

A more complex example specifying host, port, username, password, and output directory:

mongodump --host --port 27017 --u root --p 'rootpwd' --out /data/backup/mongodump02-21-18

Now that we know how to backup lets restore the system!

MongoDB Restore

The Mongo restore utility restores from a mongodump. The restore only works when connecting to a system with an active mongo service running. Mongorestore can either restore an entire database or pieces of the database. The main difference in syntax between a dump and restore is when doing a restore the directory of the backup does not need a flag. For example:

mongorestore restore/backup/dump-2018-02-21

A more complex example that allows for you push a backup to another system:

mongorestore --host --port 3017 --username root --password 'rootpwd' /restore/backup/mongodump02-21-18

MongoDB Backup and Restore

Linux Volume Configuration

Linux Volumes are an easy way to manage file directories. The first step is the figure out the name of directory you want to configure. You can find the available disk devices by running lsblk.

[root-user ~]$ lsblk
   xvdf  202:80   0  100G  0 disk  /
   xvdh 202:1    0    80G  0 disk 

The lsblk shows the root directory and the additional directory named xvdh. The next step is to set the filesystem of the directory. We do this by running the mkfs command with the -t flag which specifies the type of file system followed by the full directory path that you want to change.

[root-user ~]$ mkfs -t xfs /dev/xvdh

The next step is to create the directory that you want to mount. This is where the mongorestore will eventually put the mongodump.

[root-user ~]$ mkdir /data/db

The last step is to mount the directory onto the volume.

[root-user ~]$ mount /dev/xvdh /data

After you mount the directory you can check to see command worked by running lsblk again and you should see something similar to below.

[root-user ~]$ lsblk
    xvdf  202:80   0  100G  0 disk  /
    xvdh 202:1    0    80G  0 disk   /data