~/mongo/mongodb-backup-restore-remote.md

How to take Mongodb Backup and Restore

2 min readAugust 09, 2019by Gorav Singal#mongodb
// TL;DR

Use mongodump with --uri for remote backups, mongorestore to import BSON dumps, and mongoimport for JSON/CSV data. Works across local and remote MongoDB instances.

How to take Mongodb Backup and Restore

Pre-requisite

Assuming you have a mongodb database, and you want to take backup and restore it somewhere. Lets see how we can take backup and then restore it. And, you must have installed mongodb tools like

  • mongodump
  • mongorestore
  • mongoimport

Commands to take Mongodb Backup

mongodump --db <source database name> --authenticationDatabase <source database name> -u <username of sourcedb> -p <password of source db>

It assumes that you have a running mongodb instance on port 27017.

Mongodb backup when database is on remote server or port is different on localhost

mongodump --host <hostname>:<port> --db <source database name> --authenticationDatabase <source database name> -u <username> -p <password>

where the dump is saved

By default, the backup dump is stored in a new directory named dump under current directory.

If you want to change the directory where the backup dump should be saved, use -o option

mongodump --db <source database name> -o <my directory path>

# for remote
mongodump --host <hostname>:<port> --db <source database name> --authenticationDatabase <source database name> -u <username> -p <password> -o <my directory path>

Backup selected collection

Above commands will take backup of complete database. If you want to take backup of only few collections, use following command:

mongodump --db <source database> --collection <name of collection>

Commands to restore mongodb database

mongorestore --db <target database name> -h <hostname>:<port> -u <username of target database> -p <password of target database> --authenticationDatabase <target database> <path of my database e.g. dump/mydb>

Restore only selected collection

Above command restore complete database. To restore only selected collection, use following:

mongorestore --db <target database> --collection <name of collection> <path of bson file under dump directory>

When you take backups, it creates binary json (bson) files in dump directory. Restore single collection command takes path of that bson file.

Restore from json files

Sometimes, you have have database dump in json format. Use following command:

mongoimport --db <source database> --collection <name of collection> --file <path of json file> --jsonArray

Restore from a csv file

mongoimport --db <database name>  --collection <collection name> --type csv --headerline --file <path of csv file>

Restore without restoring index

There are lot of command options while restoring. For example you don’t want to restore indexes. Use following command:

mongorestore --db <target database> --noIndexRestore -h <host>:<port> -u <username> -p <password> --authenticationDatabase <database name> <path of dump database>

$ 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 take Backup from MongoDB and Restore to MongoDB

How to take Backup from MongoDB and Restore to MongoDB

This will take backup of your passed database name, to the passed folder. It…

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

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…