mongo1 Min Read

How to take Backup from MongoDB and Restore to MongoDB

Gorav Singal

March 15, 2018

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!

Share

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…

MongoDB with Mongoose — Patterns and Pitfalls

MongoDB with Mongoose — Patterns and Pitfalls

Schema Design Philosophy MongoDB schema design is fundamentally different from…

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…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…