mongo|March 15, 2018|1 min read

How to take Backup from MongoDB and Restore to MongoDB

TL;DR

Use mongodump to export your database to BSON files, then mongorestore to import them into another MongoDB instance. Works for local and remote databases.

How to take Backup from MongoDB and Restore to MongoDB

Problem Statement

In this post, we will learn how to take backup from MongoDB instance, and restore that to another MongoDB instance.

Tools Required

The tool you require are:
  • mongodump - To take backup
  • mongorestore - To restore

Taking Backup from a Running MongoDB instance

Run following command to take backup of a specific database ``` mongodump --db YOUR_DB_NAME --out YOUR_TARGET_FOLDER ```

This will take backup of your passed database name, to the passed folder. It will create a new folder under the path passed, and its name will be the name of your DB.

If your MongoDB instance runs on another port

By default, MongoDB instance runs on port 27017. Let us assume, it is running on port: 27020, and hostname is localhost.

Run following command:

mongodump --host localhost:27020 --db YOUR_DB_NAME --out YOUR_TARGET_FOLDER

To restore backup to a running instance of MongoDB

Run following command, if your instance is running on default port i.e. 27017:
mongorestore --db YOUR_DB_NAME YOUR_TARGET_FOLDER/YOUR_DB_NAME

If your MongoDB instance runs on another port

``` mongorestore --uri mongodb://localhost:27020 --db YOUR_DB_NAME YOUR_TARGET_FOLDER/YOUR_DB_NAME ```

Enjoy!

Related Posts

How to connect Php docker container with Mongo DB docker container

How to connect Php docker container with Mongo DB docker container

Goto your command terminal. Type: This will expose port: 27017 by default. You…

How to take Mongodb Backup and Restore

How to take Mongodb Backup and Restore

Pre-requisite Assuming you have a mongodb database, and you want to take backup…

How to install Mongo DB Driver for Php 7.x

How to install Mongo DB Driver for Php 7.x

The simplest way to install driver for php is using pecl. When I tried to run…

Mongoose - Using CRUD operations in mongodb in nodejs

Mongoose - Using CRUD operations in mongodb in nodejs

MongoDB CRUD Operations Mongoose provides a simple schema based solution to…

How to sync Mongodb data to ElasticSearch by using MongoConnector

How to sync Mongodb data to ElasticSearch by using MongoConnector

Introduction This post is about syncing your mongodo database data to…

How to run MongoDB replica set on Docker

How to run MongoDB replica set on Docker

Introduction This post is about hosting MongoDB replica set cluster with…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…