Script Hooks: How to Protect MongoDB with Retrospect
  • 24 Oct 2023
  • 2 Minutes to read
  • Dark
    Light
  • PDF

Script Hooks: How to Protect MongoDB with Retrospect

  • Dark
    Light
  • PDF

Article summary

Hooks

Overview

MongoDB is a popular NoSQL document database on Windows, Mac, and Linux with scalability and flexibility for querying and indexing. It stores data in JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time, and the document model maps to the objects in your application code, making data easy to work with. Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data. MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use. Finally, MongoDB is free and open-source.

With Retrospect, you can protect your live, running MongoDB instance with no downtime. There are two ways to protect MongoDB: backing up its underlying data after quiescing the instance or backing up a data export. We’ll walk through how to do both.

Protect MongoDB via Underlying Data

By default, MongoDB stores data at /data/db on Linux and Mac systems and at C:\data\db on Windows systems. Alternatively, you can specify the location in mongod.conf.

To protect the underlying data while your MongoDB instance is running, we will use Retrospect’s Script Hooks to lock the database from writes before the backup using the StartSource trigger and MongoDB’s db.fsyncLock() and unlock the database after the backup completes using the EndSource trigger and MongoDB’s db.fsyncUnlock(). The database is still readable during this time, but all writes are prevented. You can selectively protect only the MongoDB data store on the system by using a rule/selector to only include the data store path.

Backup Steps

To protect MongoDB, Retrospect or Retrospect Client needs to be running on the same system.

  1. Download Retrospect_for_Windows_External_Scripts.zip and place the MongoDB script hook at the appropriate location:
    • Retrospect for Windows: C:\ProgramData\Retrospect
    • Retrospect for Mac: /Library/Application Support/Retrospect
    • Retrospect Client for Windows: C:\Program Files\Retrospect\Retrospect Client
    • Retrospect Client for Mac: /Library/Application Support/Retrospect Client/retroeventhandler
  2. In the RetroEventHandler file, replace the username and path with the appropriate values for your environment. Below is the relevant section:
  3. function StartSource {
  4. # Datastore Protection
  5. /usr/local/bin/mongo admin --eval "printjson(db.fsyncLock())" &>/dev/null
  6. # Export Protection
  7. # /usr/local/bin/mongodump -o /data/dump &>/dev/null
  8. echo
  9. }
  10. function EndSource {
  11. # Datastore Protection
  12. /usr/local/bin/mongo admin --eval "printjson(db.fsyncUnlock())" &>/dev/null
  13. echo

}

  1. Start a backup of the system where MongoDB is running. You do not need to restart Retrospect for the updated file to be used.

Restore Steps

  1. Stop your MongoDB instance.
  2. Restore the Retrospect backup containing the MongoDB data store.
  3. Start your MongoDB instance. The instance will utilize the restored data.

Protect MongoDB via Data Export

To protect the MongoDB instance via an exported data folder, we will use Retrospect’s Script Hooks to export the data before the backup using the StartSource trigger and MongoDB’s mongodump. Restoring the dump requires MongoDB’s mongorestore. You can selectively protect only the exported MongoDB data store on the system by using a rule/selector to only include the data export path.

Backup Steps

To protect MongoDB, Retrospect or Retrospect Client needs to be running on the same system.

  1. Download Retrospect_for_Windows_External_Scripts.zip and place the MongoDB script hook at the appropriate location:
    • Retrospect for Windows: C:\ProgramData\Retrospect
    • Retrospect for Mac: /Library/Application Support/Retrospect
    • Retrospect Client for Windows: C:\Program Files\Retrospect\Retrospect Client
    • Retrospect Client for Mac: /Library/Application Support/Retrospect Client/retroeventhandler
  2. In the RetroEventHandler file, replace the path with the appropriate values for your environment. Below is the relevant section:
  3. function StartSource {
  4. # Datastore Protection
  5. # /usr/local/bin/mongo admin --eval "printjson(db.fsyncLock())" &>/dev/null
  6. # Export Protection
  7. /usr/local/bin/mongodump -o /data/dump &>/dev/null
  8. echo
  9. }
  10. function EndSource {
  11. # Datastore Protection
  12. # /usr/local/bin/mongo admin --eval "printjson(db.fsyncUnlock())" &>/dev/null
  13. echo

}

  1. Start a backup of the system where MongoDB is running. You do not need to restart Retrospect for the updated file to be used.

Restore Steps

  1. Restore the Retrospect backup containing the MongoDB data export folder.
  2. Use mongorestore to restore the data dump.

Last Update: September 5, 2017


Was this article helpful?