An Easy Way to Recover MongoDB on Ubuntu 20.04

MongoDB on Ubuntu

Nowadays, every online platform has its own databases and backup systems that log users’ activity for better business prospects. Whenever we hear about database systems, the first thought that comes to our mind is the backup and restore or recovery process.

Restoring or recovering data at the time of disasters, like server crashes, data loss, etc., is essential. That’s when backed up business-critical data comes to great help. Speaking of database systems, MongoDB is considered to be significantly more secure than others. And a combination of a secure database, MongoDB and a secure operating system, Ubuntu makes a good team.

In this article, we are going to present a quick and easy way to recover MongoDB on Ubuntu v.20.4.

  • Ubuntu 20.04 server with a sudo non-root user and a firewall
  • Fully installed and configured MongoDB
  • Sample MongoDB
Basic Understanding of MongoDB for Backup and Restoring Data:

In MongoDB, reading and inserting data at scale can be resource-intensive, which in turn consumes extensive CPU processing power, memory, and storage space. As MongoDB is widely used for Big Data operations, it is recommended to perform backup and export functions during nights or hours when it is ideal.

In the case of the busy MongoDB server and continuous data alteration during the database backup process, it can be complex to maintain data consistency, which can be solved by applying replication.

For the data export or backup process, it is advisable to use the command – mongodump, and for restoration – mongorestore.

Step 1: Backing up MongoDB
  • Create a backup directory.
$ sudo mkdir /var/backups/mongobackups
  • Create a backup using the mongodump command with –db argument, specifying the database for which you’re creating a database.
$ sudo mongodump --db clouddb --out /var/backups/mongobackups/`date +"%m-%d-%y"`

In the above command, `date +"%m-%d-%y"` the term is used to automatically get the backup-creation data and manage it properly. The clouddb is the name of the database we just created.

  • You should backup data regularly when the server is nearly ideal or with a few jobs. For example, you can schedule regular backups at 2:00 AM every day. To make this happen, follow the below command:
$ sudo crontab -e

Note: You’ll be writing this cron job command for the root users, which will need password verification from the root profiles to execute properly.

  • Insert the mongodump command inside the created crontab prompt:
* 2 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`
  • After some time, MongoDB might get out of disk space. Thus, the old data backups either should be cleaned or compressed regularly, or you can also set the database’s old backups (such as a week older) to get deleted regularly. Let’s know-how:
$ find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;
  • Create a cron job to find a week older data backup to delete them:
$ sudo crontab -e

Now, add the following line to set up cron job execution time at 2:00 AM every day.

* 2 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;
  • At last, Save and Close the file.

Now, your MongoDB database backup solution is all set to proceed.

Step 2: Restore Backed Up MongoDB Database

Now, we have a copy of our mission-critical data. So, we can restore the lost data using backup files and by writing the following command:

$ sudo mongorestore --db clouddb --drop /var/backups/mongobackups/02-08-22/newdb/

Here, the –drop argument will ensure that whenever this database is called, it will be first dropped and then restored from the backups in a clean database.

Summing Up:

Now, you know how to create and restore a backup of MongoDB in Ubuntu 20.04. So, try it and let us know where you get stuck.

Have any disaster recovery solution-based projects for us? Send us your requirements and book your first 60 mins of free disaster recovery solution-based consultation with our expert technical team.

Supportscreen tag