Backup And Restore MongoDB On Ubuntu

Prerequisites

Before following this article, please make sure you complete the following prerequisites:

Ubuntu 18.04 server with sudo non-root user and a firewall.

MongoDB installed and configured using the article Install MongoDB Community Edition on Ubuntu

Sample MongoDB

Step 1 - Create Backup Directory

First, we create a backup directory in which we are going to store all the backup for easy access and in a more organized way.

mongo

> show dbs

sudo mkdir /var/www/backups

Step 2 - Create Backup using mongodump

mongodump is the utility to export data from the database. This utility accepts --db argument which specifies the name of a specific database for which you want to create a backup

sudo mongodump --db yourdb --out /var/backups/mongobackups

Here in the above command, we are creating a backup for the database name yourdb, and using --out argument you can set the directory where we want our backup created. After executing the above command you will get the below output

2021-03-29T20:30:36.230+0000 writing yourdb.school to

2021-03-29T20:30:36.230+0000 done dumping yourdb.school (2300 documents)

If you don't specify the name of the database using --db argument then mongodump backs up all of your databases.

Step 3 - Automate The Backups

As per step 2, we are able to create the dump of our selected database but using manual mode. To prevent data loss we must take backup on regular basis at a certain interval of time. Let say we want to take daily backup at morning 4 AM and to achieve this you need to follow the below steps

sudo mongodump --db yourdb --out /var/backups/mongobackups/`date +"%m-%d-%y"`

In the above command, we used date +"%m-%d-%y" which automatically gets the current date and it will help us to access our backup copies in a more convenient way.

For Example : /var/backups/mongobackups/03-20-21

Now to create an automatic backup at 4:00 AM on a daily basis, we are going to set a cron job

sudo crontab -e

Now you have to set mongodump command inside crontab to execute at 4:00 AM

4 mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`

The above cron job will cerate dump of our database on daily basis and if your database size is large then you may soon run out of disk space with too many backups. To prevent this issue we are going to delete old backups regularly.

For example, to delete all the backups older than seven days, you can use the following bash command:

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

Now similar to daily backups, for deleting old backups we need to set cron job which will run on a daily basis before creating new backups at 3:55 AM

sudo crontab -e

Now enter the below command to set cron job for 3:55 AM daily basis

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

Using step 3, now you are able to create a proper solution for creating a backup of MongoDB.

Step 4 - Restore a backup using mongorestore

When you restore the database from a backup using mongorestore you will get an exact copy of the database taken at a particular time, including all the indexes and data types.

Let’s take an example of our previous step where we have created a backup and using the below command we are going to restore the database yourdb.

sudo mongorestore --db yourdb --drop /var/backups/mongobackups/03-20-21/yourdb/

Here, in --db argument you can pass your database name for which you want to restore the database, and --drop will make sure that the targeted database will first dropped so that the backup is restored in a clean database.

6 Time Viewed

Posted: ৩ জুন ২০২২, বিকাল ৬:৩৪ সময়