EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Mongodb backup and restore
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB JSON
    • MongoDB Careers
    • MongoDB Migration
    • MongoDB Export to Excel
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
    • MongoDB schema
    • MongoDB find by id
    • Mongodb max document size
    • Mongodb not equal
    • MongoDB keyfile
    • MongoDB Hosting
    • MongoDB Kafka Connector
    • MongoDB exists
    • MongoDB express
    • MongoDB Performance
    • MongoDB Connection String
    • MongoDB BI Connector
    • Mongodb aggregation pipeline
    • Mongodb backup and restore
    • MongoDB Drop Collection
    • MongoDB Text Search
    • MongoDB URI
    • MongoDB Full Text Search
    • MongoDB List Databases
    • MongoDB Use Cases
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Mongodb backup and restore

Mongodb backup and restore

Introduction to Mongodb backup and restore

Mongo DB backup and restore is the process of migrating and transferring all the data from the source database to the target database which indeed is going to be a Mongo DB database. While migrating and transferring the data from the source it is very necessary to consider a factor that is whether the source database is online or offline. By the term online here we mean that is there any of the updates or changes taking place in the source database when we are trying to migrate or not. If the source database is changing and evolving, we will have to do online migration else we can go for offline migration. In this topic, we are going to learn about Mongodb backup and restore.

In case of data failure or unavoidable circumstances that lead to data loss, if we have backup then we can restore it and get back to the original state with previous data.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In both cases of migration, we need to follow two steps of creating a backup and restoring the same. In this article, we will study the offline migration of the dataset in Mongo DB. We will discuss each of the steps of backup creation till restoration in detail.

Steps for Migration

The process of migration in the offline database is very simple and straightforward. It involves the two steps mentioned below –

  • Existing data is used and a zip file is created for consideration of backup.
  • A new database or target database is selected and the dumping of the backup also called as the restoration process is carried out.

Now, we will look at each of these steps in detail.

Backup creation

For creating a backup zip file, there is a utility program that exists in Mongo DB which is mongodump. We can use this command for creating a zip file of the existing source database. The syntax of the mongodump command is as shown below –

Mongodump –host=”name of host : port number” – username = “name of the user” –password “associated password for the user” – authenticationDatabase “admin”  --db = “name of source database” –collection = “Name of the collection” –query = “json” –forceTableScan -v –gzip –out ./dump

In the above syntax, the terminologies used here are described one by one –

–host: This is the address of the host that includes the name of the host and the port of the socket where the connection needs to be established. By default, the value of the hostname and port is considered as localhost:27017. In case, if you are trying to specify a string of connections then you can make the use of uri option shown below –

Uri = “mongodb:// name of user : associated password @ host address[: port number] …”

–name of the user: This will help you to specify the user’s name which will have the access to the database and which will be authenticated by the Mongo DB database whose we are creating a backup.

— password: This option helps you to specify the password associated with the specified user name which will be considered for authentication purposes to verify if the user is a valid user or not.

–authenticationDatabase: The database which is authenticated and where the specified user with the name of the user is created. If we will not specify the authentication database option here or if the export target database is not specified then the dump command will assume that there is the presence of the user’s credentials inside the admin database.

–db: This option helps in specifying the source database which needs to be considered for exporting or creating a dump file. When the name of the database is not mentioned here then all the databases which are present in the current instance are considered for dump file creation. We can also specify the database name directly by using the connection string containing URI.

–collection: This helps in specifying the name of the collection whose backup is to be taken. By default, if you do not specify this option and its value then all the collections of the database are dumped into the dump zip file.

–query: This is the JSON document containing a query that can be specified to restrict and limit the documents to be dumped in the zip file. It is necessary to enclose a query in single quotes ‘{…}’ to make sure that it doesn’t get miscommunicated with the other environmental parameters.

–forceTableScan: Usually, the dump file is created by scanning all the documents and saving them which occur inside the index of _id field. When we specify this option all the data is directly scanned to the data store.

–gzip: The output of the command is compressed due to this option. The individual files are also compressed and the files have the suffix mentioned as .gz.

–out: The directory or folder where the dump file is written in BSON files is specified here. By default, the directory where the output files are stored is inside the current directory inside which a new directory with dump name is created.

Restoration

The restoration can be done by using the following command –

Mongorestore --uri = “ mongodb:// name of user : associated password @ name of host : port address ? AuthSource = admin” –drop –noIndexRestore

The terminologies have the same description as mentioned for backup creation.

Example of Mongodb backup and restore

We have a database whose name is EDUCBA having Articles collection in it. To create a backup zip file which will be referred to as a dump file, we can fire the following command –

sudo mongodump --db EDUCBA --out /var/backups/educbaBackups/`Date +"%m-%d-%y"`

The output of the execution of the above command is as shown in the below image –

A zip file will be created which can be used as a dump file for restoration of this data in any of the other target databases. Now, let us look at the command used for restoring this data which is as shown below –

sudo mongorestore --db EDUCBA --drop /var/backups/educbaBackups/01-20-16/EDUCBA/

The output of the execution of the above command is as shown below –

Conclusion – Mongodb backup and restore

We can migrate the data in Mongo DB by exporting it to create a dump file and then importing the same zip file created from exporting the backup to restore the data in the target database.

Recommended Articles

This is a guide to Mongodb backup and restore. Here we discuss the offline migration of the dataset in Mongo DB and the steps of backup creation. You may also have a look at the following articles to learn more –

  1. MongoDB Triggers
  2. Mongodb show collections
  3. Mongodb unwind
  4. MongoDB findOne()
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more