Skip to main content
Version: 2.7

Back up MongoDB on Kubernetes

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=mongo for this example.
  • If MongoDB is using oplog, you will 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 will 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:

    • Rule name: add a name for your backup rule

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

    • Container: ignore this field

    • Action: add the following action

      mongo --eval "printjson(db.fsyncLock())"
    • Add the second action:

      If you are using Oplog, this will dump Oplog and can be used 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 will administratively reduce 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=mongo

    • Container: ignore this field

    • Action: add the following action

      mongo --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:

  • Once you have populated all the fields of Create Backup window, click Create.

Demo

Watch this short demo of the above information.