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

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…