Back-Up, Restoration, and Migration of MongoDB Database on Ubuntu

In the ever-changing enterprise database administration spectrum, MongoDB is one of the most popular choices in the NoSQL DBMS platforms. It is known for its advanced features of being highly scalable, more powerful than the competitors, reliability, and easiness to handle. Even though MongoDB is so friendly to the non-tech administration, DBAs must know a few essentials when it comes to data backup, restoration, and migration of MongoDB databases.

The import and export of databases deal more with tangible data in human-readable formats, which is compatible with all popular software products. Operations related to the backup and restoration of data while using or newly creating MongoDB binary data will preserve not only the integrity and consistency but also various MongoDB specific attributes. When it comes to data migration, it is preferable to use data backup and restoration as long as the data source, and the target systems are compatible.

In this article, we will be discussing further the standard backup, restoration, and migration of MongoDB on the latest Ubuntu version. Let us first explore some prerequisites to facilitate it.

Prerequisites for MongoDB data manipulations

The following prerequisites should be met before you proceed further with data backup or migration for MongoDB:

  • The version of Ubuntu 14.04 Droplet or above.
  • Non-root Sudo user (you may check the Ubuntu 14.04 server setup to know how)
  • Completing MongoDB installation and configuration.
  • A sample MongoDB database to be imported

While attempting the methods in this tutorial, all the given commands requiring the root privileges must be run as non-root Sudo-privileged users.

Know the basics

The DBA’s need to know some basic things before attempting the MongoDB manipulations. Even if you were working with relational databases like MySQL or MS SQL, you could find some similarities with MongoDB while you work on it.

The primary thing you should keep in mind that MongoDB is using JSON and binary JSON (bson) to store info. JSON is a human-readable language format, which can be ideally used for exporting and importing the data. You may further manipulate the data exported using any suitable tool supporting JSON. Even a plain text editor can be used for this purpose.

A sample JSON document may look like below:

For all those who handle various kinds of databases, JSON is a very comfortable language to work with; however, one drawback is that it may not support all types of data in the bson. So, you may experience the fidelity loss in terms of information on using JSON. So, for operations like backup and restoration, etc., it is always ideal to use binary bson.

Another important thing is that you should not worry about creating a MongoDB database explicitly. If there is no existing database you choose to import, then a database will be created automatically. However, compared to other database engines available, the MongoDB structure is built automatically upon insertion of the very first data row in the table.

While inserting a large volume of data in MongoDB reading or insertion, this may be highly resourced intensive, which may consume a lot of memory and disk space. This is a crucial area of concern when MongoDB used to handle large databases for Big Data applications. The simple and most effective solution here is to run backups and exports during nighttime or other comfortable times when the business is slow.

Another area of concern with MongoDB handling is that the consistency of information handled may be troublesome if there is a busy MongoDB server. You cannot find a simple one-step solution for this issue. However, we will put forth some recommendations further down in this article.

MongoDB database backup

The most important argument to perform mongodump is ‘–db.’ This specifies the database name which you choose to backup. If there is no database name specified, then mongodump runs to backup all existing databases. Another important data argument is ‘—out.’ This specifies the directory to which data gets dumped.

To back up, we need to create the directory like:

Here is the backup command:

An ideal backing up process will give the results.

In the path we had written above, the data format is +”%m-%d-%y,” which will automatically return the current date. It will allow the users to set the backup inside the given directory inside /var/backups/01-20-20/. This is a beneficial approach while automating backup.

By doing this, you will have a complete backup of the database in the directory. This backup will have everything meant to restore the newdb and preserve the DB’s fidelity properly. As a rule of thumb, you should do a regular backup of data, preferably daily, when the server has the least load. You can set mongodump command as a cron job, which can regularly run, for example, 03:00 AM daily.

With many backups’ schedules, there is a chance that you may run out of your disk space available for the MongoDB database. So, it is recommended that you frequently clean the old backups or keep them compressed. As a standard, you may set it as deleting all backups, which are more than 7 days old. The command for this can be written as below.

Restoration and Migration of MongoDB

You can restore your database from the most appropriate previous backups. This same feature is also useful when you are trying to migrate your MongoDB databases. To restore, you can use the ‘mongorestore’ command, which works with the ‘mongodump’ backup procedure.

  • To do this, you may specify the arguments as the first name of the database with ‘—DB’ argument.
  • Next, use the ‘—drop’ argument to ensure that the target database is another DB is dropped so that the new backup is done in a clean database.
  • To finish, specify the directory of the most appropriate last backup, i.e.,

The final command may look like

This way, we are restoring the data on the same server in which the backup was created. For migrating the data to different ad servers, i.e., for migration, you may use the same method, but you have to copy the backup directory to the new server.

In this article, we discussed the basic things about managing the MongoDB data for back up, restoration, and migration of DB to another server. You may follow these steps but need to be careful in restoring data from the most relevant historical backup and taking all precautions to avoid losing data while migrating.