~/mongo/53-how-take-backup-mongodb-and-restore-mongodb.md

How to take Backup from MongoDB and Restore to MongoDB

1 min readMarch 15, 2018by Gorav Singal#mongodb-backup#mongodb#mongo
// 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!

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

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

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…