Introduction to Backup in MongoDB
Backup is essential and useful in every database to recover data from the loss. We have already taken the backup of the database to apply each database’s backup strategy, so only data can be recovered. Basically, there are two types of backups available in MongoDB, i.e. cp or rsync and mongodump backup.
How to Create Backup in MongoDB?
Below are the types of backup available in MongoDB.
- Hot backup (Mongodump / Mongorestore)
- Cold backup (cp and rsync)
We have to use mongodump command to take hot backup in MongoDB. Using mongodump, we can take a single database as well as single collection backup. We can use mongodump command to take the whole database server backup and take a backup from the remote server. We can also take the MongoDB server’s backup using system tools; this is called a system-level backup. Mongodump and mongorestore backup utility is work with BSON data dumps, and it is useful for creating backups of small collections of databases.
1. Hot backup (Mongodump / Mongorestore)
- We can take a backup of the collection or database at the time of MongoDB instance running. Mongodump database utility takes database backup by connecting to the mongodb instance.
- We can take backup for an entire server, single database, single collection, or part of collection using mongodump utility in MongoDB.
- We can also take a backup from another host to connecting to the host by using –host.
- Mongodump will write output into BSON files which held the data accessible from the mongod instance.
- Below are the syntax and example of mongodump command.
1. Take whole database server backup using mongodump
- I suppose we have to take backup in the same data directory then we have using the below command.
- In the below example we have taken the backup in the same directory on which we have issue command.
In the below example, we have taken a whole database backup into /home directory. For taking backup into the different directory, we have used –our a flag with mongodump.
2. Take a single database backup using mongodump
We can take a single database backup in the same partition or different partition. Below are the syntax and example of taking a single database backup using mongodump command.
mongodump --db=test_bak –out=(partition mount point on which we have taken backup)
mongodump --db=test_bak –out=/home/
3. Take a single collection backup using mongodump
We can take a single collection backup in the same partition or different partition. Below are the syntax and example of taking single collection backup using mongodump command.
mongodump --db=database_name –collection=collection_name
mongodump --db=test_bak –collection=collection_name –out=(partition mount point on which we have taken backup)
mongodump --db=test_bak --collection=test_bak2
mongodump --db=test_bak --collection=test_bak2 --out=/home/
2. Cold backup (Using cp and sync)
- We have taken cold backup using cp and rsync command in MongoDB. For taking cold backup, we need to stop the service of MongoDB.
- A cold backup is a full backup of the database. We can’t restore a single collection or database from a full database dump.
- We need to stop the service of MongoDB; if we cannot stop the service of MongoDB, then our backup will be inconsistent.
- We can copy the direct file of the database using cp and rsync command in MongoDB. Hot backup is a consistent backup of the database server.
- Using a cold backup point in time, recovery is not possible for replica sets in MongoDB. We can’t set up a point in time recovery using the cold backup.
- Below is the method to take cold backup.
1. Using cp command
The below example shows to take backup using cp command in MongoDB.
cp –aRp (path of data directory files) (Backup copied location)
systemctl stop mongod
cp -aRp /var/lib/mongo/ /home/
2. Using rsync command
Below is an example is shown to take backup using rsync command.
rsync (path of data directory files) (Backup copied location)
systemctl stop mongod
rsync -avzh /var/lib/mongo/ /home/
This is a guide to Backup in MongoDB. Here we discuss an introduction to Backup in MongoDB and how to create a backup with examples. You can also go through our other related articles to learn more –