How To Automate The Process Of Backing Up The Data Of Mongo Db And Restoring It On Demand?

## Prerequisites
  •  Node.js version 14 or higher
  • MongoDB installed locally or on a remote server

## Installation

To install the module, run the following command in your Node.js project:

 [mongod-backup](https://www.npmjs.com/package/mongod-backup) installed this npm package

 `npm install mongod-backup` 
## Usage
### backup(dbUri: string): Promise<string>

This function creates a backup of a MongoDB database using the `mongodump` command. It accepts a database URI as a parameter and returns a promise that resolves with the path of the archive file that was created.

const { backup } = require("mongod-backup");

const dbUri = "mongodb://localhost:27017/mydatabase";

backup(dbUri)
  .then((backupPath) => {
    console.log(`Backup successful. Archive file path: ${backupPath}`);
  })
  .catch((error) => {
    console.log("Backup failed.", error);
  });

### restore(dbName: string, archivePath: string): Promise<string>

This function restores a MongoDB database from an archive file using the `mongorestore` command. It accepts the name of the database to restore and the path of the archive file as parameters, and returns a promise that resolves with a success message when the restore is complete.

const { restore } = require("mongod-backup");

restore("mydatabase", "/path/to/archive/file")
  .then((message) => {
    console.log(message);
  })
  .catch((error) => {
    console.error("Restore failed:", error);
  });

### backupFromLocal(dbName: string): Promise<string>

This function creates a backup of a local MongoDB database using the `mongodump` command. It accepts the name of the database as a parameter and returns a promise that resolves with the path of the archive file that was created.

const { backupFromLocal } = require("mongod-backup");

backupFromLocal("mydatabase")
  .then((archivePath) => {
    console.log(`Backup successful: ${archivePath}`);
  })
  .catch((error) => {
    console.error("Backup failed:", error);
  });

To make a zip of all the data from MongoDB, you can modify the `dbUri` parameter by removing the database name. For the `restore` function, you can pass an empty string as the `dbName` parameter and provide the path to the zip file.

Please note that you will need to have a MongoDB instance running locally or on a remote server for the backup and restore operations to work properly.

I hope this information is helpful! Let me know if you have any further questions. At ICS, We have built an automated solution to manage the latest backup of all db at a secure place and restore the backup at any point of time.

This is very helpful in different scenarios like when your server is attacked and the hackers flush out all the data or data has been flushed due to some server crash or cloud provider issue.

Please reach out to us for more info on building such a complete system: here


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *