EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial Backup in MongoDB
 

Backup in MongoDB

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

Backup in MongoDB

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

Code:

mongodump

Output:

Hot backup

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.

Code:

mongodump --out=/home/

Output:

Backup in MongoDB - 2

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.

Syntax

mongodump --db=database_name
mongodump --db=test_bak –out=(partition mount point on which we have taken backup)

Code:

mongodump --db=test_bak
mongodump --db=test_bak –out=/home/

Output:

using mongodump

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.

Syntax

mongodump --db=database_name –collection=collection_name
mongodump --db=test_bak –collection=collection_name –out=(partition mount point on which we have taken backup)

Code:

mongodump --db=test_bak --collection=test_bak2
mongodump --db=test_bak --collection=test_bak2 --out=/home/

Output:

Take single

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.

Syntax

cp –aRp (path of data directory files) (Backup copied location)

Code:

systemctl stop mongod
cp -aRp /var/lib/mongo/ /home/
echo $?

Output:

Backup in MongoDB - 5

2. Using rsync command

Below is an example is shown to take backup using rsync command.

Syntax 

rsync (path of data directory files) (Backup copied location)

Code:

systemctl stop mongod
rsync -avzh /var/lib/mongo/ /home/

Output:

Backup in MongoDB - 6

Recommended Articles

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 –

  1. MongoDB Administration
  2. MongoDB Collection
  3. MongoDB create Index
  4. MongoDB Alternatives

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW