MongoDB – Create Backup

Database backup is one of the most important process for any database or system administrator. A database backup is the copy of data from the database that helps to reuse the database in case of any loss events happens.

Basic Syntax

The basic syntax of mongodump command is shown below:

mongodump –host [host-name] –username [username] –password [password] –port [port-number] –db [database-name] –out [backup-directory]

A brief explanation of each option is shown below:

  • –host : Specify the hostname or IP address of the database server.
  • –username : Specify the database username.
  • –password : Specify the password of the database user.
  • –port : Specify the port number of the MongoDB instance.
  • –db : Specify the name of the database that you want to backup.
  • –out : Specify the location of the backup path.

The basic syntax of mongorestore command is shown below:

mongorestore –host [host-name] –username [username] –password [password] –port [port-number] –db [database-name] –drop [backup-location]

Where:

–drop is used to remove database if already exist.

How to Backup MongoDB Database

In this section, we will show you how to back up single and multiple databases on the local and remote servers.

Backup a Single Database

You can use mongodump command to take a single database backup or all databases backup. For example, to backup a single database named testdb and save the backup inside /opt directory on the local server, run the following command:

mongodump –db testdb –out /opt/

The above command will take a backup of the testdb database and create a directory with database name inside /opt directory.

Backup a Single Collection

You can also backup a single collection from a database by specifying the option –collection with mongodump command.

For example, to take a backup of a single collection named testcollection from the testdb database, run the following command:

mongodump –collection testcollection –db testdb –out /opt/

mongodump All Databases

To back up all MongoDB databases, you don’t need to specify any database name with mongodump command.

You can run the following command to backup all MongoDB databases and save the backup inside /mnt directory.

mongodump –out /mnt

This command will backup all MongoDB databases and save it inside /mnt directory.

Backup a Remote Database

In order to take a database backup from the remote MongoDB server. You will need to specify the IP address and port of the remote MongoDB server. You will also need to configure MongoDB to allow for remote connection.

For example, to take a backup of a single database named remotedb from the remote MongoDB server (192.168.0.101), run the following command:

mongodump –host 192.168.0.101 –port 27017 –username admin –password yourpassword –db remotedb –out /opt/

Where:

192.168.0.101 is the IP address of the remote MongoDB server, 27017 the MongoDB port number, admin is the username of the database, yourpassword is the password of the admin user and remotedb is the name of the database that you want to backup.

How to Restore MongoDB Database

In this section, we will show you how to restore single and multiple databases on the local and remote server.

Restore a Single Database

You can use the mongorestore command to restore a single database easily using the backup that we created earlier.

For example, to restore a single database backup named testdb , run the following command:

mongorestore –db testdb –drop /opt/testdb

This command will restore a testdb database from the /opt/testdb directory.

Restore All Databases

To restore all MongoDB databases, you don’t need to specify –db option. You can restore all databases by just running the following command:

mongorestore –drop /opt

This will restore all MongoDB database from the backup located inside /opt directory.

Restore a Remote Database

You can restore the MongoDB database by specifying the –host and –port parameters with mongorestore command.

For example, to restore a backup from the single database named remotedb on the remote MongoDB server (192.168.0.101), run the following command:

mongorestore –host 192.168.0.101 –port 27017 –username admin –password yourpassword –db remotedb –drop /opt/remotedb

Automate MongoDB Database Backup with Cron

Backing up a MongoDB database regularly is a good practice for any system administrator. You can create a database backup script and schedule it using the Cron job.

You can create a backup script with the following command:

nano /opt/mongobackup.sh

Add the following lines:

#!/bin/bash TODAY=`date +%d%b%Y` BACKUPDIR=/backup/mongo mkdir -p ${BACKUPDIR}/${TODAY} mongodump –host localhost –db testdb –out ${BACKUPDIR}/${TODAY}/

Save and close the file then create a cron job to run the above script daily at 10 AM.

nano /etc/crontab

Add the following lines:

0 10 * * * root /opt/mongobackup.sh

Save and close the file when you are finished.

Conclusion

You learnt how to use the command line tools mongodump and mongorestore to backup and restore a MongoDB database in the above instruction. I’m hoping these tools will make it easier for you to carry out your regular tasks.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *