Databases 7 min read

How to Run Percona MongoDB HotBackup with a Simple PHP Script

This guide explains why the community edition of MongoDB lacks native hot backup, how Percona MongoDB adds online backup support, the underlying backup and restore principles, and provides a step‑by‑step PHP script with environment setup, configuration, execution, and scheduling instructions.

dbaplus Community
dbaplus Community
dbaplus Community
How to Run Percona MongoDB HotBackup with a Simple PHP Script

Background

The official MongoDB Community edition does not support hot (online) backups; users must rely on logical backup tools such as mongodump and mongorestore, which are similar to MySQL's mysqldump. When backing up a replica set, the --oplog option must be used to capture incremental data, analogous to mysqldump --single-transaction --master-data=2.

Percona MongoDB HotBackup

Starting with Percona MongoDB 3.2, the WiredTiger storage engine provides built‑in online hot‑backup capability, eliminating the need for logical dumps.

Backup Principle

A background process continuously monitors the MongoDB oplog. When new entries appear, they are written to WiredTiger.backup (you can view changes with strings WiredTiger.backup).

The process copies the database files and index files from the dbpath to a user‑specified backup directory.

Restore Principle

The recorded WiredTiger.backup log is replayed, applying oplog changes to the WiredTiger engine to produce a consistent snapshot.

The backed‑up data files are copied back into the original dbpath, and MongoDB is started, automatically rejoining the replica set.

Important Considerations

For small to medium databases the process is straightforward, but TB‑scale data can become painful.

If the oplog size is too small, it may be overwritten during backup/restore, making it impossible to re‑join the replica set.

The tool must run on the current dbpath of a member (typically a secondary). Run createBackup on the admin database with administrator rights and specify the backup directory.

Remote backup is not supported; the script should be deployed on the secondary node. Use NFS or similar if you need to store backups on another server.

PHP Script for Automated HotBackup

The author provides a ready‑made PHP script ( pmongo_bak.php) that can be executed directly from the shell.

1. Environment Preparation

Install required packages:

yum install -y php-pear php-devel php gcc openssl openssl-devel cyrus-sasl cyrus-sasl-devel

2. Install PHP Mongo Driver

pecl install mongo

Then add extension=mongo.so to the end of /etc/php.ini.

3. Create MongoDB Super‑User for Backup

db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})

4. Configure pmongo_bak.php

// Modify the following configuration values
$user = "admin"; // root user
$pwd = "123456";
$host = "192.168.180.26"; // secondary node for hot backup
$port = "27017";
$authdb = "admin"; // authentication database
$BAKDIR = "/data/bak/" . date('Y_m_d_H_i_s');

// Do not modify the code below
$m = new MongoBak($user,$pwd,$host,$port,$authdb,$BAKDIR);
// ... (rest of the script) ...

5. Run the Script

Execute with root privileges:

php pmongo_bak.php

6. Schedule with Cron

00 01 * * * /usr/bin/php /root/php_mongodb/pmongo_bak.php > /root/php_mongodb/bak_status.log 2>&1

7. Deployment Note

The script must reside on the secondary server. To back up to a remote location, mount the destination via NFS or a similar network file system.

Download

The pmongo_bak.php script and related files can be downloaded from the Baidu Cloud link https://pan.baidu.com/s/1axM2-joZnO7dCFHb-e33zA (access code may be required). The tool is offered free of charge to the community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

OpsPHPPerconaDatabase BackupMongoDBHotBackup
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.