How to Use mongodump
Backing up and restoring the MongoDB database is important when you want to migrate your database to a different server or switch to MongoDB. MongoDB comes with two commands, mongodump
and mongorestore
, to back up and restore databases. These are simple yet powerful tools for performing the backup and restoration process on live servers effectively.
mongodump
is a backup tool that creates a binary export of the contents of a database. It can export data from standalone, replica sets, and shared cluster deployments.
mongorestore
is a database restoration tool that loads data from either a binary database dump created by mongodump
or the standard input into a mongod
or mongos
instance.
This tutorial will show you how to back up and restore the MongoDB database.
Requirements
To follow this guide, you need:
- A server running a Linux operating system with MongoDB installed.
- A root password set up on your server.
Basic Syntax
The basic syntax of the 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 provided below:
- host: Specifies the hostname or IP address of the database server.
- username: Specifies the database username.
- password: Specifies the password of the database user.
- port: Specifies the port number of the MongoDB instance.
- db: Specifies the name of the database to be backed up.
- out: Specifies the location of the backup path.
The basic syntax of the mongorestore command is:
mongorestore --host [host-name] --username [username] --password [password] --port [port-number] --db [database-name] --drop [backup-location]
Where:
- drop is used to remove the database if it already exists.
How to Backup a MongoDB Database
This section provides steps for backing up single and multiple databases on both local and remote servers in MongoDB.
Backing Up a Single Database
To backup a single database, you can use the mongodump command. For instance, to backup a database named “testdb” and save the backup in the /opt directory on a 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 the database name inside the /opt directory.
Backup a Single Collection
You can also backup a single collection from a database by specifying the –collection option with the mongodump command. For example, to take a backup of a single collection named “test collection” from the testdb database, run the following command:
mongodump --collection testcollection --db testdb --out /opt/
mongodump for All Databases
To back up all MongoDB databases, you don’t need to specify any database name with the mongodump command.
You can use the following command to back up all MongoDB databases and save the backup inside the /mnt directory.
mongodump --out /mnt
This command will backup all MongoDB databases and save them inside the /mnt directory.
Backup a Remote Database
To take a database backup from a remote MongoDB server, you need to specify its IP address and port. Additionally, you must configure MongoDB to allow for remote connections.
For example, to take a backup of a single database named “remotedb” from the remote MongoDB server at IP address 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:
The IP address of the remote MongoDB server is 192.168.0.101, and the MongoDB port number is 27017. Use admin as the database username and yourpassword as the password for the admin user. The name of the database you want to backup is remotedb.
How to Restore a MongoDB Database
This section explains how to restore single and multiple databases on local and remote servers.
Restore a Single Database
To restore a single database using the backup created earlier, use the mongorestore command as follows:
mongorestore --db testdb /path/to/testdb/
This command restores the testdb database from the specified path.
mongorestore --db testdb --drop /opt/testdb
This command will restore a testdb database from the /opt/testdb directory.
Restoring All Databases
To restore all MongoDB databases, you do not need to specify the –db option. Instead, you can simply run the following command:
mongorestore --drop /opt
This command will restore all MongoDB databases from the backup located within the /opt directory.
Restoring a Remote MongoDB Database
To restore a MongoDB database, use the mongorestore command with the –host and –port parameters. For example, to restore a backup of a single database named “remotedb” on the remote MongoDB server at IP address 192.168.0.101, execute the following command:
mongorestore --host 192.168.0.101 --port 27017 --username admin --password yourpassword --db remotedb --drop /opt/remotedb
Automating MongoDB Database Backups with Cron
Regularly backing up your MongoDB database is a good practice for any system administrator. You can create a backup script and schedule it using a Cron job. To create a backup script, execute the following command:
nano /opt/mongobackup.sh
Add the following lines to the script:
#!/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 script daily at 10 AM:
nano /etc/crontab
Add the following line to the file:
0 10 * * * root /opt/mongobackup.sh
Save and close the file.
Conclusion
This guide has shown you how to backup and restore a MongoDB database using the mongodump and mongorestore utilities. I hope these tools will help you with your day-to-day processes.