Don't Lose Your Data! A Step by step guide:- How to Backup and Restore Your MongoDB Database ?

blog best suited for Ubuntu 20.04.5 LTS. For different Linux distributions, some commands may vary. Users need to check commands for other Linux distributions.

In this guide we focus on using Utilities provided by MongoDB mongodump and mongorestore. These tools helps in creating backups and restoring data using command line.

Some Consideration before using MongoDB Utility :-

The mongodump and mongorestore utilities work with BSON data dumps.These utilities are useful for creating backup & restoring of small Database. These tools will works fine with small and non-prod envs. ( eg:- i have taken backup of 150GB of data and tools works fine above that some complications of time, memory and & data loss occurs )

Some performance Impacts Because mongodump & mongorestore operate by interacting with a running mongod instance, they can impact the performance of your running database. Not only do the tools create traffic for a running database instance, they also force the database to read all data through memory.

Use an alternative backup strategy such as Filesystem Snapshots or Cloud Backups in MongoDB Atlas if the performance impact of mongodump & mongorestore is unacceptable for your use case.

Installation of MongoDB utility Tools :-

The mongodump tool is part of the MongoDB Database Tools package

You can download MongoDB Database Tools from the link :- https://www.mongodb.com/try/download/database-tools

Select your Platform and package. ( my platform is Ubuntu 20.04 and debian package will work for me. )

once downloaded navigate to the directory containing the downloaded .deb package, then run the following command to install the Database Tools using the apt package manager:

// install the downloaded package
sudo apt install ./mongodb-database-tools-*-100.9.0.deb

after successfully installation of utility. You can check using command :- mongodump --help and output will be something.

Backup a Database with mongodump :-

way 1 :- Backup a Remote MongoDB Instance.

mongodump --uri="mongodb://<host URL/IP>:<Port>" [additional options]

// can be used in both combination.

mongodump --host=10.10.10.59 --port=27017 --db=pardali_gateway --username="mongoadmin" --password="chga4125rfgd" --out /opt/MongoDB-Backup/test

way 2 :- Backup a Secure MongoDB Instance.


mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" --username="barryadmin" --password="testpassword" --out /opt/MongoDB-Backup/test

way 3 :- Create an compress & Archive File.

mongodump --username=mongoadmin --password=chga4125rfgd --gzip --archive=/mongobackuppetro/ &

// can be used in both combination.

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" --username="barryadmin" --password="testpassword" --gzip --archive=/mongobackuppetro/MongoDB-Backup/

Restore a Database with mongorestore :-

way 1 :- Restoring data into a remote MongoDB instance

mongorestore [additional options] --uri="mongodb://<host URL/IP>:<Port>" [restore directory/file]
mongorestore [additional options] --host="<host URL/IP>:<Port>"  [restore directory/file]
mongorestore [additional options] --host="<host URL/IP>" --port=<Port>  [restore directory/file]

// can be used in both combination.

mongorestore --verbose --host="10.10.10.59" --port=27017 --username="mongoadmin" --password="chga4125rfgd" ./opt/MongoDB-Backup/dump/

way 2 :- Restoring a Secure MongoDB Instance.

mongorestore [additional options] --authenticationDatabase=<Database> -u=<Username> -p=<Password> [restore directory/file]

// can be used in both combination.

mongorestore --verbose --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" --username="barryadmin" --password="testpassword" ./opt/MongoDB-Backup/dump/

way 3 :- Restoring an compress & Archive File.

mongodump --username=mongoadmin --password=chga4125rfgd --gzip --archive=/mongobackuppetro/ &

// can be used in both combination.
--archive=db.archive
mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" --username="barryadmin" --password="testpassword" --archive=/mongobackuppetro/MongoDB-Backup/

Alternatives for Data Backup in MongoDB:-

MongoDB has other built-in options for data backup in the case of data loss events. The three alternatives to mongodump for data backup are described below.

MongoDB Atlas:-

MongoDB Atlas is the hosted MongoDB built-in backup option that supports two fully-managed methods for data backup: cloud backup and legacy backups.

MongoDB Cloud Manager & Ops Manager:-

MongoDB Cloud Manager serves as a hosted backup, monitoring, and automation service designed for MongoDB. It facilitates the backup and restoration of MongoDB replica sets and sharded clusters through an intuitive graphical user interface.

File system backups:-

To back up a MongoDB deployment, you can duplicate MongoDB’s underlying data files. such as Linux’s Logical Volume Manager (LVM) or Amazon’s EBS for EC2.

Conclusion

Hence, Above steps will help you in backup and restore of MongoDB Data. thanks!!

feel free to ask queries related to this topic. I will be happy to help you.

connect with me:- utkarshsri0701@gmail.com / serv-ar-tistry Studio

0
Subscribe to my newsletter

Read articles from utkarsh srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

utkarsh srivastava
utkarsh srivastava

A seasoned IT professional with over 10 years of experience, crafting innovative solutions at serv-ar-tistry Studio. I am a passionate leader with a proven track record of success in the cloud, infrastructure, and DevOps space. After gaining extensive experience working for various multinational corporations across diverse industries, I took the leap to pursue my entrepreneurial dream and founded serv-ar-tistry Studio. At serv-ar-tistry Studio, we are dedicated to empowering businesses with cutting-edge cloud infrastructure and DevOps solutions. Our team of skilled professionals leverages their expertise to deliver: Scalable and secure cloud solutions: We help businesses migrate and optimize their infrastructure on leading cloud platforms, ensuring efficient resource utilization and cost-effectiveness. Robust infrastructure management: We design, implement, and manage robust infrastructure solutions that are reliable, secure, and adaptable to evolving business needs. Streamlined DevOps workflows: We automate and optimize development and deployment processes, enabling businesses to deliver software faster and with higher quality.