Introduction to MongoDB
MongoDB is one of the most renowned NoSQL Database Engines. It is widely regarded as a Database that is highly powerful, scalable, and reliable to use. MongoDB leverages JSON-like documents with optional schemas to function efficiently. MongoDB supports a unique Document-based Data Model that helps developers map data and code. MongoDB further empowers application development by providing a unified and robust API for querying.
MongoDB lets you optimize, scale, and deploy your applications seamlessly with built-in sharding, replication, performance tools, and indexing to help you run confidently in production. MongoDB ensures that your performance SLAs are met in any conducive environment irrespective of whether it’s 450 million users around the world or your first customer.
Understanding the steps to install MongoDB on Ubuntu
You can follow these steps to install MongoDB on Ubuntu with the help of the apt package manager:
- Import the Public Key Leveraged by the Package Management System: Open up your terminal and enter the following command to start importing the public GPG key for MongoDB from MongoDB Static Site:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Once this operation returns OK, you can move on to the next step.
- Create a List File for MongoDB: Next, create a list file /etc/apt/sources.list.d/mongodb-org-5.0.list for your Ubuntu Version (Version 16.04 or higher). If you are oblivious to the Ubuntu version your host is running, open a shell or terminal on your host server/device and run this command:
lsb_release -dc
For more details, refer to the MongoDB Installation Guide.
- Reload the Local Package Database: Next, issue the following command to reload the local package database:
sudo apt-get update
- Download the MongoDB Packages: For this step, you can either choose to install the latest version of MongoDB or a particular version of MongoDB. If you wish to install the latest stable version, issue the following command:
sudo apt-get install -y mongodb-org
Understanding the steps to restore and backup MongoDB on Ubuntu
Restore and Backup are integral components of any database system. They play a pivotal role when you lose your valuable data owing to a server crash, corruption, or any of the other viable reasons. Here are the steps you can follow to Restore and Backup the MongoDB database by leveraging the utilities provided by MongoDB itself.
Step 1: Creating a backup directory
- You can begin by creating a backup directory where you can store all the backups for ease of access. It also serves as a more organized repository for your backups. Here’s how you can create a backup directory:
sudo mkdir /var/backups/mongobackups
Step 2: Creating a backup by leveraging mongodump
- mongodump is the go-to utility that you leverage to export data from the database. This utility will retrieve the –db argument that will cite the name of your desired database to create a backup for:
sudo mongodump --db dbyour --out /var/backups/mongobackups/
- In the above command, you are creating a backup for the database named dbyour. You use the –out argument to set the directory where you wish to create the backup. Once you’ve executed the given command you get the following prompt:
2022-01-04T19:30:24.230+0000 writing dbyour.coach to 2022-01-04T19:30:24.230+0000 done dumping dbyour.coach (2100 documents)
- In case you forget to add your database name via the –db argument, mongodump will backup and store up all your databases by default.
Step 3: Automating the backups
- In the previous step, you created the dump through the manual method. To prevent data loss you need to take backup on a regular basis at a specified interval of time. Say, you want to take a daily backup at morning 2 AM, and to attain this you can follow along with this snippet:
sudo mongodump --db dbyour --out /var/backups/mongobackups/`date +"%m-%d-%y"`
sudo crontab -e
- Next, set the mongodump command inside crontab to execute the query at 2:00 AM:
* 2 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`
- This cron job creates the dump of your database daily, however, if your database size is too large you might soon run out of disk space with too many backups to keep track of. Therefore, to prevent this issue from rearing its head, you need to delete the old backups regularly. For instance, if you want to delete all the backups older than 5 days, you can use this bash command:
find /var/backups/mongobackups/ -mtime +5 -exec rm -rf {} \;
- Similar to daily backups, set a cron job to delete backups which will run daily before creating new backups at say 1:56 AM. Enter the following command to set the cron job for 1:56 AM daily:
56 1 * * * find /var/backups/mongobackups/ -mtime +5 -exec rm -rf {} \;
Step 4: Restoring a backup by leveraging mongostore
- You can leverage mongostore to restore the database from a backup to extract an exact copy of your database for a given/specific time, along with data types and indexes. Here’s the command for the same:
sudo mongorestore --db dbyour --drop /var/backups/mongobackups/01-05-22/dbyour/
Conclusion
This blog talks about the steps you can follow to backup and restore MongoDB on Ubuntu seamlessly. It also gives a brief overview of MongoDB before exploring the steps involved in installing, restoring, and backing up on Ubuntu.
YouTube: MongoDB in 18 Minutes – Introduction (Caleb Curry)
Photo credit: The feature image has been done by Thitiphat.