Let’s see how to backup the MongoDB database with DigitalOcean in this article. At Bobcares, with our DigitalOcean Managed Services, we can handle your DigitalOcean – MongoDB issues.
Backup MongoDB Database with DigitalOcean
In terms of NoSQL database engines, MongoDB is one of the most well-liked. Scalability, reliability, toughness, and usability are its most renowned strengths. We will backup and restore a sample MongoDB database in this article.
Prerequisites
Ubuntu 18.04 Droplet, MongoDB installed and configured, Sample MongoDB database imported.
Backup Process
We can use the
mongodump
Copy Code
command to create a data backup. The --db
Copy Code
argument, which specifies the name of the database we want to back up, is a crucial part of the mongodump
Copy Code
command. The mongodump
Copy Code
command backs up all the databases if we don’t specify a database name. The --out
Copy Code
argument, which specifies the directory into which the data will be written, is the second crucial argument.
Let’s back up the
new
Copy Code
database, for instance, and store the backup file in the /var/backups/mongobackups
Copy Code
directory. Our backups will store in the directory with the current date, such as /var/backups/mongobackups/MM-DD-YY
Copy Code
.
- Firstly, create that directory.
$ sudo mkdir /var/backups/mongobackups
- Now run
.mongodump
Copy Code$ sudo mongodump --db new --out /var/backups/mongobackups/`date +"%m-%d-%y"`
- The result will look like this:
2020-10-29T19:22:36.886+0000 writing new.restaurants to 2020-10-29T19:22:36.969+0000 done dumping new.restaurants (25359 documents)
Copy CodeNote that we used
to obtain the current date in the directory path mentioned above. As a result, we will be able to store backups in a directory likedate +"%m-%d-%y"
Copy Code
. This is especially practical when we automate the backups./var/backups/date/
Copy CodeThe
database is currently fully backed up and can be found in the locationnew
Copy Code
. Everything needed to successfully restore the/var/backups/mongobackups/date/new/
Copy Code
database is present in this backup.new
Copy CodeGenerally speaking, we ought to perform regular backups, ideally when the server is the least busy. As a result, we can configure the
command as a cron job to execute regularly.mongodump
Copy Code - To do this, launch the cron editor and open crontab.
$ sudo crontab -e
- Enter the
command as follows inside the crontab prompt:mongodump
Copy Code3 3 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`
Copy Code
This will have all of the databases backed up.
How to delete old Backups?
Our Tech team always advises cleaning old backups as they may take up a lot of disk space. Use the bash command below to remove all backups older than seven days:
$ find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;
We can also perform this cleaning process as a cron job so that the removal of old backups will be done before the next backup. In order to do so:
- Open the crontab and type the below command:
$ sudo crontab -e
- Then enter the line:
3 1 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;
Copy Code - Finally, save and close the file.
The MongoDB databases will have a reliable backup solution if we do all the actions in this step.
[Looking for a solution to another query? We are available 24/7.]
Conclusion
In this article, we have provided the simple steps from our Support team to create a proper backup of the MongoDB database with DigitalOcean.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments