How to back up, restore, and migrate a MongoDB database on Ubuntu 20.04?

Overview

MongoDB is widely recognized as one of the most used NoSQL database engines, acclaimed for its scalability, resilience, dependability, and user-friendly nature. This tutorial demonstrates the processes of backing up, restoring, and migrating a sample MongoDB database.

Prerequisites

There are certain prerequisites that need to be met before you begin:

Get Started

Step 1: Create a backup of the MongoDB Database using mongodump

  • To begin, let's create a directory called /var/backups/mongobackups, where we will store the backup of the newdb database. It is advisable to organize the backups by including the current date in the directory name, such as /var/backups/mongobackups/06-09-23.

sudo mkdir /var/backups/mongobackups
  • Execute the mongodump command to initiate the backup process.

sudo mongodump --db newdb --out /var/backups/mongobackups/$(date +'%m-%d-%y')

To ensure regular backups, it is recommended to schedule the mongodump command as a cron job during periods of low server load. For instance, you can configure the cron job to execute daily at 03:03 AM.

  • To achieve this, access the following crontab command:

sudo crontab -e
  • Within the crontab prompt, add the following command:

3 3 * * * mongodump --out /var/backups/mongobackups/$(date +'\%m-\%d-\%y')

If your MongoDB database backups start consuming excessive disk space due to their size, it is advisable to manage them efficiently. Regularly cleaning or compressing old backups is recommended to free up space.

  • To illustrate this, you can execute the following bash command to delete backups that are older than seven days:

find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Just like the previous mongodump command, you can schedule the deletion of old backups as a cron job. It is recommended to set it to run just before the next backup process starts. For instance, if the backup job is scheduled at 03:03 AM, the deletion job should run at 03:01 AM.

  • To open the crontab again and make the necessary changes, use the following command:

sudo crontab -e
  • Add the following command:

1 3 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Save your file and exit by pressing Ctrl+O and Ctrl+X respectively.

Step 2: Restore and Migrate a MongoDB database using mongorestore

To restore a MongoDB database, we will execute the mongorestore command, which is compatible with the binary backups generated by mongodump.

  • After obtaining a backup with a timestamp, you can restore it using the following command.

Note: Make sure to modify the date to match your backup's timestamp.

sudo mongorestore --db newdb --drop /var/backups/mongobackups/06-09-23/newdb/

In the scenario described above, if you intend to migrate the data to a different server using the same approach, you will need to copy the backup directory (/var/backups/mongobackups/06-09-23/newdb/ in this example) to the other server.

Conclusion

We have successfully demonstrated how to back up, restore and migrate a MongoDB database on ubuntu 20.04.

Last updated