MongoDB Backup and Restore Guide
Database backup is one of the most important processes for any database or system administrator. A database backup is the copy of data from the database that helps reuse the database in case any loss events happen.
Backup 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 mongodump and mongorestore commands to backup and restores the databases. It is a very simple and powerful tool to perform the backup and restoration process on live servers 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 sets, and shared 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.
This tutorial will show you how to backup and restore the MongoDB database.
Table Of Contents
- Requirements
- Basic Syntax
- How to Backup MongoDB Database
- Backup a Single Database
- Backup a Single Collection
- mongodump All Databases
- Backup a Remote Database
- How to Restore MongoDB Database
- Restore a Single Database
- Restore All Databases
- Restore a Remote Database
- Automate MongoDB Database Backup with Cron
- Conclusion
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 database name 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 the database if it already exists.
How to Backup MongoDB Database
This section will show how the single and multiple databases on the local and remote servers backup in MongoDB.
Backup a Single Database
You can use the mongodump command to take a single or all database 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 the 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 test collection 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 the /mnt directory.
mongodump --out /mnt
This command will backup all MongoDB databases and save it inside /mnt directory.
Backup a Remote Database
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 is the MongoDB port number, admin is the database username, 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
This section will show you how to restore single and multiple databases on the local and remote servers.
Restore a Single Database
You can easily use the mongorestore command as MongoDB restore command on a single database using the backup 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 databases from the backup inside /opt directory.
Restore a Remote Database
You can restore the MongoDB database by specifying the --host and --port parameters with the 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
Regularly tidying up a MongoDB database is a good practice for any system administrator. For example, 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
The above guide taught you how to backup and restore the MongoDB database with the mongodump and mongorestore command line utility. I hope these tools will help you to perform your day-to-day process.
If you do not want to manage this, check out our MongoDB Backup Solution
Does Mongodump lock the database?
Mongodump does not lock the database. You can read and write the file easily.
Can we take incremental backup in MongoDB?
Yes, you can perform both full and incremental backup with the help of Mongobackup, an external tool of MongoDB. The full backup will be performed by dbPath in a filesystem, while incremental backup will be done by partial oplog dump.
How can you define atomacity in database?
It is called atomicity when multiple operations are grouped into a single logical entity. For example, if you want to update three databases in a single logical action, you can call it atomicity.
Scheduled MongoDB (Single Databases) Backups SnapShooter
Backup a single MongoDB database to your external storage
Learn more about MongoDB (Single Databases) Backups
Get started for freeRelated
Thank you for helping us improve!