Skip to main content
Version: 3.0

Back up MongoDB on Kubernetes

Applicable to both Classic and Federated modes

You can use the instructions on this page to create pre and post backup rules with Portworx Backup, which take application-consistent backups for MongoDB on Kubernetes in production.

MongoDB stores data files in a location called dbPath. dbPath is most often configured to use /var/lib/mongo. Some container images may also configure this path differently, so it is important to pay attention to dbPath.

When deployed with more than a single node, MongoDB has an additional component called Oplog that should be considered during backup. The MongoDB Oplog is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases. The Oplog can also be used for database recovery. Because the Oplog recovery can take a very long time, MongoDB usually is combined with regular database snapshots.

Installation

Prerequisites

note
  • MongoDB pods should use the label app=mongodb for this example.
  • If MongoDB is using oplog, you need a persistent location to dump oplog. The location /data/oplog is used in this example.

Create rules for MongoDB

Create rules for MongoDB that run both before and after the backup operation runs:

Create a pre-exec backup rule for MongoDB

Portworx Backup should flush writes to disk and lock the database to ensure a consistent backup. This is done using the db.fsyncLock() method.

  1. From the home page, navigate to Settings >Rules > Add New.

  2. In the Add Rule window, provide the following details:

    1. Rule name: add a name for your backup rule

    2. Pod Selector: add the following app label app=mongodb

    3. Container: This field is mandatory if you're using mTLS with the Linkerd service mesh, provide the container name. Otherwise, leave this field blank.

    4. Action: add the following action based on MongoDB version:

    • If you are on MongoDB version below 7.0:

      mongo --eval "printjson(db.fsyncLock())"
    • If you are on MongoDB version 7.0 and above:

      mongosh admin --authenticationDatabase admin -u <mongodb-root-username> -p <mongodb-root-password> --eval "printjson(db.fsyncLock());"
    1. Click on + icon (that is inline with the Action field) to add the second action if you are using Oplog:

      Add the following command in the Action field to dump Oplog for alternate database recovery.

      mongodump -d local -c oplog.rs -o /data/oplog

Create a post-exec backup rule for MongoDB

Create a post-backup action that administratively releases the lock on the database following your pre-backup db.fsyncLock() operation.

  1. From the home page, navigate to Settings > Rules > Add New.

  2. In the Add Rule window, provide the following details:

    • Rule name: add a name for your backup rule

    • Pod Selector: add the following app label app=mongodb

    • Container: This field is mandatory if you're using mTLS with the Linkerd service mesh, provide the container name. Otherwise, leave this field blank.

    • Action: add the following action based on your MongoDB version:

      • If you are on MongoDB version below 7.0:

        mongo --eval "printjson(db.fsyncUnlock())"
      • If you are on MongoDB version 7.0 and above:

        mongosh admin --authenticationDatabase admin -u <mongodb-root-username> -p <mongodb-root-password> --eval "printjson(db.fsyncUnlock());"

Use the rules during backup of MongoDB

  • During the backup creation process, select the rules in the pre-exec and post-exec drop-down lists:

  • After you populate all the fields of the Create Backup window, click Create.