DEV Community

Mario García
Mario García

Posted on • Updated on

Running Percona Backup for MongoDB with Docker

This blog post is the third part of the series on Percona Backup for MongoDB (PBM) that includes the following articles:

  1. Deploy a MongoDB Cluster With Docker
  2. Object Storage with MinIO
  3. Running Percona Backup for MongoDB With Docker

This blog post will teach you how to configure PBM to back up your databases using MinIO as the storage solution. As in the previous blog posts, we will use Docker for configuring PBM in a development environment.

From the documentation, you can install PBM in any of the following ways:

  • Install from Percona repositories using the package manager of your operating system
  • Run in Docker
  • Deploy in Kubernetes
  • Build from source
  • Manual download

Running PBM in Docker

Create the PBM User

Follow the instructions from the documentation and create a user for PBM.

Log in to your MongoDB primary node

$ docker exec -it mongo1 mongosh
Enter fullscreen mode Exit fullscreen mode

Create the role that allows any action on any resource.

db.getSiblingDB("admin").createRole({ "role": "pbmAnyAction",
      "privileges": [
         { "resource": { "anyResource": true },
           "actions": [ "anyAction" ]
         }
      ],
      "roles": []
   });
Enter fullscreen mode Exit fullscreen mode

Create the user and assign the role you created to it.

db.getSiblingDB("admin").createUser({user: "pbmuser",
       "pwd": "secretpwd",
       "roles" : [
          { "db" : "admin", "role" : "readWrite", "collection": "" },
          { "db" : "admin", "role" : "backup" },
          { "db" : "admin", "role" : "clusterMonitor" },
          { "db" : "admin", "role" : "restore" },
          { "db" : "admin", "role" : "pbmAnyAction" }
       ]
    });
Enter fullscreen mode Exit fullscreen mode

Start the Container

Start the PBM container as follows:

$ docker run --name pbm --network mongodbCluster -e PBM_MONGODB_URI="mongodb://pbmuser:secretpwd@<HOST>:<PORT>" -d percona/percona-backup-mongodb:latest
Enter fullscreen mode Exit fullscreen mode

Where:

  • pbm is the name you want to assign to your container.
  • This container is added to the network previously created, mongodbCluster.
  • PBM_MONGODB-URI is a MongoDB Connection URI string used to connect to the primary node of your cluster.
  • The <HOST> will be the hostname (mongo1) of your primary node, the <PORT> will be 27017.

Set Up PBM

Create a pbm_config.yaml file with the following content:

storage:
  type: s3
  s3:
    endpointUrl: "http://<HOST>:9000"
    region: us-east-1
    bucket: <BUCKET_NAME>
    prefix: data/pbm/test
    credentials:
      access-key-id: accessKey
      secret-access-key: secretKey
Enter fullscreen mode Exit fullscreen mode

Where:

  • <HOST> is the hostname of the container running PBM.
  • <BUCKET_NAME> is the name (mongo-backup) of the bucket you created while configuring MinIO.
  • The values of acces-key-id and secret-access-key can be obtained from the credentials.json file downloaded after creating an access key for the MinIO user.

Then, copy that file to your container:

$ docker cp pbm_config.yaml container_name:/tmp/pbm_config.yaml
Enter fullscreen mode Exit fullscreen mode

container_name is the name (pbm) of the PBM container.

Log in to your container:

$ docker exec -it pbm bash
Enter fullscreen mode Exit fullscreen mode

And run:

$ pbm config --file /tmp/pbm_config.yaml
Enter fullscreen mode Exit fullscreen mode

Set Up pbm-agent

Before doing backups, you must start pbm-agent on every server with the mongod node installed.
For doing this, you must log in to every node in your cluster, install PBM and run pbm-agent:

$ docker exec -it <NODE> bash
Enter fullscreen mode Exit fullscreen mode

Replace NODE with mongo1, mongo2, and mongo3. Run the following commands.

Install all the required packages:

$ apt update && apt install curl cron nano && curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb && apt install -y gnupg2 lsb-release ./percona-release_latest.generic_all.deb && apt update && percona-release enable pbm release && apt update && apt install -y percona-backup-mongodb
Enter fullscreen mode Exit fullscreen mode

The above command will install PBM.

And finally, set the PBM_MONGODB_URI and run pbm-agent.

PBM_MONGODB_URI="mongodb://pbmuser:secretpwd@<HOST>:27017/?authSource=admin" && export PBM_MONGODB_URI="mongodb://pbmuser:secretpwd@<HOST>:27017/?authSource=admin&replSetName=myReplicaSet" && pbm-agent
Enter fullscreen mode Exit fullscreen mode

Where <HOST> is the hostname (mongo1) of your primary node.

Once PBM is running on every node of your cluster, you can do backups.

Start a Backup

You can do a complete backup of the databases and collections in your MongoDB Server by running the following command from the pbm container:

$ docker exec -it pbm bash
Enter fullscreen mode Exit fullscreen mode
pbm backup --type=TYPE
Enter fullscreen mode Exit fullscreen mode

You can specify what type of backup you wish to make: physical or logical.

When physical backup is selected, Percona Backup for MongoDB copies the contents of the dbpath directory (data and metadata files, indexes, journal, and logs) from every shard and config server replica set to the backup storage.

During logical backups, Percona Backup for MongoDB copies the actual data to the backup storage. When no --type flag is passed, Percona Backup for MongoDB makes a logical backup.

To list the backups available in your storage, run the following command:

pbm list
Enter fullscreen mode Exit fullscreen mode

You will get the following output:

Backup snapshots:
  2023-03-10T19:32:35Z <logical> [restore_to_time: 2023-03-10T19:32:40Z]
Enter fullscreen mode Exit fullscreen mode

For more information, check the documentation.

Selective Backup / Restore

When you run pbm backup, you get a complete backup of the databases in your server. But if you want to get a backup of a database or a collection, you may want to try selective backup and selective restore.

For example, if you have some databases in your server, and you need to back up a database or a collection in your database, you can do a selective backup by running any of the following commands:

pbm backup --ns=staff.Payments
Enter fullscreen mode Exit fullscreen mode
pbm backup --ns=Invoices.*
Enter fullscreen mode Exit fullscreen mode

With the first command, you will get a backup of the Payments collection in the staff database.

With the second command, you will get a backup of the Invoices database and all the collections it includes.

If you run pbm list, you will get the following output:

Backup snapshots:
  2023-03-10T19:49:42Z <logical, selective> [restore_to_time: 2023-03-10T19:49:46Z]
Enter fullscreen mode Exit fullscreen mode

To restore a specific database or a collection, run the pbm restore command:

pbm restore <backup_name> --ns <database.collection>
Enter fullscreen mode Exit fullscreen mode

Where <backup_name> is the name of your backup (i.e. 2023-03-10T19:49:42Z) and <database.collection> is the name of the database and collection you want to restore.

For more information about selective backup and selective restore, check this page in the documentation.

Conclusion

After configuring a three-node MongoDB cluster with replication enabled, MinIO and Percona Backup for MongoDB, you're ready to try this tools in your local development environment.


Support me on Buy Me A Coffee

Top comments (1)

Collapse
 
phuongtran280 profile image
Phuong Tran

That's great, I appreciate it!

I understand that the PBM backup currently supports logical types. To enable physical or incremental backups, we'll need to add a PSMDB on the replica set.

Could you please provide instructions on how to set up and configure scheduled backups?