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.
Backup and restore the MongoDB database is important when you want to migrate your database to a different server or switch the MongoDB version. MongoDB comes with mongodump and mongorestore utility to backup and restore the databases. It is a very simple and powerful utility to perform the backup and restoration process on the live servers very effectively.
The mongodump is a backup tool to create a binary export of the contents of a database. It can export the data from standalone, replica set, and sharded cluster deployments.
The 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.
In this tutorial, we will show you how to backup and restore the MongoDB database.
# Requirements
- A server running Linux operating system with MongoDB installed.
- A root password is set up on your server.
# 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 a single and multiple databases on the local and remote server.
# Backup a Single Database
You can use mongodump command to take a single database back up 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/
# Backup 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 have 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
In the above guide, you learned how to backup and restore MongoDB database with mongodump and mongorestore command line utility. I hope these tools will help you to perform your day-to-day process.
If you don't want to manage this check out our MongoDB Backup Solution