How to backup and restore a Mongo Database

Introduction

MongoDB is a popular NoSQL database that is used for storing large amounts of data in a flexible and JSON-like format. The Backup and Restore in MongoDB is an important part to handle a database. This utility is important because the valuable data can be removed or crashed or corrupted from the server.

Prerequisite

  1. An SSH user access with sudo privileges
  2. Mongo DB credentials if it is authenticated
  3. The `mongodump` and `mongorestore` command line tools, which are included with the MongoDB installation

Implementation

Step 1: SSH the server and convert the user to the root

$ ssh user@IP
$ sudo su

Step 2: Backing Up a MongoDB Database

$ mongodump –host <hostname> –db <dbname> –out </backup directory path/>

If the Mongo DB is running on the local host, the hostname is not required

$ mongodump –db <dbname> –out <backup directory path>
  • –username and –password: The credentials to use to authenticate to the database

Step 3: Restoring a MongoDB Database

$ mongorestore –db <dbname> </backup directory path/>

Use –drop option delete all data from the target database before restoring it

$ mongorestore –db <dbname> –drop </backup directory path/>

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top