Bobcares

Backup MongoDB Database DigitalOcean | How To?

by | Sep 16, 2022

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.

backup mongodb database digitalocean

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.

  1. Firstly, create that directory.
    $ sudo mkdir /var/backups/mongobackups
  2. Now runmongodump.
    $ sudo mongodump --db new --out /var/backups/mongobackups/`date +"%m-%d-%y"`
  3. 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 the new  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.

  4. To do this, launch the cron editor and open crontab.
    $ sudo crontab -e
  5. 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:

  1. Open the crontab and type the below command:
    $ sudo crontab -e
  2. Then enter the line:
    3 1 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;
  3. 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.

GET STARTED

0 Comments

Submit a Comment

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

Speed issues driving customers away?
We’ve got your back!

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF