An Overview of Percona Backup for MongoDB

Agus Syafaat

The known and popular backup method for MongoDB is mongodump. It is a logical backup method, similar to mysqldump in MySQL or pg_dump in PostgreSQL database. There is another backup tool called Percona Backup for MongoDB. It has support for replica sets and shard clusters, as well as more advanced features like point in time recovery. 

It is important to note that it performs a consistent backup for your mongodb sharded cluster, and also supports S3 compatible object storage to store the backups. In this blog, we will discuss architecture, installation and usage of Percona Backup for MongoDB.

Architecture

Percona Backup for MongoDB consists of two components, the first one is a process utility that needs to be installed on each MongoDB node, called pbm-agent. The pbm-agent acts to coordinate between the database nodes, running the backup and restore process. It also checks if the node is the correct node to take the backup. The pbm-agent requires a specific user with some role privileges; eg: readWrite, backup, clusterMonitor, and restore. It also needs to create a new role for pbm with action type anyAction and resource type anyResource. The user must exist on each node in the replica set and also in the config server if you use sharded cluster architecture. Percona Backup for MongoDB uses a MongoDB URI connection string method to connect to the database, which is why it requires credential access at the first time.

The other component is the command line interface called pbm. pbm utility triggers the backup-related actions, e.g., execute backup, restore, list backup, delete, and so on. Before you work with pbm, you would need to configure backup options, restore options, and point in time recovery options. 

The config file itself is stored in a YAML file, and pbm config command is used to load the configuration file. Some of commands for the pbm utility are shown below:

  • pbm config, the command used to configure the backup option before executed.
  • pbm backup,  used to take a backup of MongoDB. It supports some compression methods such as gzip, pgzip, lz4, snappy. 
  • pbm restore, the command used for restoring a backup to a node.
  • pbm list,  listing of the current backup files.
  • pbm cancel-backup, used to cancel the running backup process.
  • pbm delete-backup, used to delete backup files. There are two options; you can specify the file name of backup to delete or delete backup files that are older than a certain age.

Installation of Percona Backup for MongoDB

There are two ways you can install Percona Backup for MongoDB, you can use the package manager from the operating system and use the official Percona Repository for installing the software, or you can build from source code.

As prerequisites before installing the pbm through the yum/apt install, you need to configure the Percona Repository, and then after that, enable pbm repository:

[[email protected] ~]# percona-release enable pbm release

* Enabling the Percona Backup MongoDB repository

<*> All done!

Then, install Percona Backup for MongoDB. In this case, I am using the CentOS based operating system so we’ll do yum install:

[[email protected] ~]# yum install percona-backup-mongodb

Loaded plugins: fastestmirror

Repository percona-release-noarch is listed more than once in the configuration

Repository percona-release is listed more than once in the configuration

Repository percona-release-noarch is listed more than once in the configuration

Repository percona-release-source is listed more than once in the configuration

(1/10): extras/7/x86_64/primary_db                                                                                                         | 222 kB  00:00:00

(2/10): pbm-release-x86_64/7/primary_db                                                                                                    | 4.2 kB  00:00:02

(3/10): percona-tools-release/7/x86_64/primary_db                                                                                          |  84 kB  00:00:00

(4/10): tools-release-x86_64/7/primary_db                                                                                                  |  84 kB  00:00:00

(5/10): percona-release-x86_64/7/primary_db                                                                                                | 1.1 MB  00:00:06

(6/10): percona-release/7/x86_64/primary_db                                                                                                | 1.1 MB  00:00:08

(7/10): base/7/x86_64/primary_db                                                                                                           | 6.1 MB  00:00:11

(8/10): updates/7/x86_64/primary_db                                                                                                        | 2.5 MB  00:00:08

(9/10): epel/x86_64/updateinfo                                                                                                             | 1.0 MB  00:00:13

(10/10): epel/x86_64/primary_db                                                                                                            | 6.9 MB  00:00:07

Loading mirror speeds from cached hostfile

 * base: mirror.telkomuniversity.ac.id

 * epel: ftp.jaist.ac.jp

 * extras: mirror.telkomuniversity.ac.id

 * updates: mirror.telkomuniversity.ac.id

Resolving Dependencies

--> Running transaction check

---> Package percona-backup-mongodb.x86_64 0:1.3.3-1.el7 will be installed

--> Finished Dependency Resolution



Dependencies Resolved



==================================================================================================================================================================

 Package                                       Arch                          Version                              Repository                                 Size

==================================================================================================================================================================

Installing:

 percona-backup-mongodb                        x86_64                        1.3.3-1.el7                          pbm-release-x86_64                         16 M



Transaction Summary

==================================================================================================================================================================

Install  1 Package



Total download size: 16 M

Installed size: 61 M

Is this ok [y/d/N]: y

Downloading packages:

percona-backup-mongodb-1.3.3-1.el7.x86_64.rpm                                                                                              |  16 MB  00:00:55

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : percona-backup-mongodb-1.3.3-1.el7.x86_64                                                                                                      1/1

  Verifying  : percona-backup-mongodb-1.3.3-1.el7.x86_64                                                                                                      1/1



Installed:

  percona-backup-mongodb.x86_64 0:1.3.3-1.el7



Complete!

After it is complete, you can configure the pbm-agent as a background process and play around with the pbm command line interface:

[[email protected] ~]# pbm

usage: pbm [<flags>] <command> [<args> ...]



Percona Backup for MongoDB



Flags:

  --help                     Show context-sensitive help (also try

                             --help-long and --help-man).

  --mongodb-uri=MONGODB-URI  MongoDB connection string (Default =

                             PBM_MONGODB_URI environment variable)

  --compression=s2           Compression type

                             <none>/<gzip>/<snappy>/<lz4>/<s2>/<pgzip>



Commands:

  help [<command>...]

    Show help.



  config [<flags>] [<key>]

    Set, change or list the config



  backup

    Make backup



  restore [<flags>] [<backup_name>]

    Restore backup



  cancel-backup

    Restore backup



  list [<flags>]

    Backup list



  delete-backup [<flags>] [<name>]

    Delete a backup



  version [<flags>]

    PBM version info

Backup in Action

Before taking a backup of MongoDB, ensure the pbm-agent is running on each node and the backup config has been set as shown below, set the path of the backup:

[[email protected] ~]# pbm config --file=/root/config.yaml --mongodb-uri "mongodb://pbmuser:***@localhost:27017/"

[Config set]

------

pitr:

  enabled: false

storage:

  type: filesystem

  filesystem:

    path: /data/backups

And test run the backup on one of the secondary nodes:

[[email protected] ~]# pbm backup --mongodb-uri "mongodb://pbmuser:*****@localhost:27017/" --compression=pgzip

Starting backup '2020-11-13T15:28:49Z'...................

Backup '2020-11-13T15:28:49Z' to remote store '/data/backups' has started

That’s all for now. Coming soon, ClusterControl 1.8.1 will allow you to schedule and manage your MongoDB clusters using Percona Backup for MongoDB.

ClusterControl
The only management system you’ll ever need to take control of your open source database infrastructure.