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
command to create a data backup. The --db
argument, which specifies the name of the database we want to back up, is a crucial part of the mongodump
command. The mongodump
command backs up all the databases if we don’t specify a database name. The --out
argument, which specifies the directory into which the data will be written, is the second crucial argument.
Let’s back up the new
database, for instance, and store the backup file in the /var/backups/mongobackups
directory. Our backups will store in the directory with the current date, such as /var/backups/mongobackups/MM-DD-YY
.
- Firstly, create that directory.
$ sudo mkdir /var/backups/mongobackups
- Now run
mongodump
.$ 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)
Note that we used
date +"%m-%d-%y"
to obtain the current date in the directory path mentioned above. As a result, we will be able to store backups in a directory like/var/backups/date/
. This is especially practical when we automate the backups.The
new
database is currently fully backed up and can be found in the location/var/backups/mongobackups/date/new/
. Everything needed to successfully restore thenew
database is present in this backup.Generally speaking, we ought to perform regular backups, ideally when the server is the least busy. As a result, we can configure the
mongodump
command as a cron job to execute regularly. - To do this, launch the cron editor and open crontab.
$ sudo crontab -e
- Enter the
mongodump
command as follows inside the crontab prompt:3 3 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`
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 {} \;
- 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