Backing up and restoring MongoDB databases using PHP
💻 coding

Backing up and restoring MongoDB databases using PHP

2 min read 340 words
2 min read
ShareWhatsAppPost on X
  • 1The 'mongodump' utility creates a binary export of a MongoDB database, which can be restored using 'mongorestore'.
  • 2A PHP class called MongoDumper simplifies the backup process for MongoDB databases, allowing for ZIP compression of dumps.
  • 3Restoration requires unzipping BSON and JSON files to a designated folder and using the 'mongorestore' command to restore the database.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The 'mongodump' utility creates a binary export of a MongoDB database, which can be restored using 'mongorestore'."

Backing up and restoring MongoDB databases using PHP

Dumping a MongoDB database is done using the “mongodump” utility in the command prompt. This dump creates a binary export of the database. This export can be restored using the “mongorestore” utility.

A basic dump of a local MongoDB database can be code as follows:

mongodump --db mymongodatabase

To dump a specific collection, simply specify the collection:

mongodump --collection collection --db mymongodatabase

Also, you can dump the database to a specific server location:

mongodump --db mymongodatabase --dbpath /var/mypath/mongodumps

We wanted to create a utility that would allow our client to backup their WordPress-integrated MongoDB database using an admin utility. To do this, we built a PHP class that allows us to enter the database name, the dump location, and go. This tool also ZIPs the binary files for us, allowing us to conserve disk space and contain each dump in one file. You can access this class here: https://github.com/KerryRitter/MongoDumper

A basic usage of this class is done as follows:

$dumper = new MongoDumper("/var/mypath/mongodumps");

$dumper->run("mydb", true); // 'true' shows debug info

This will dump the local ‘mydb’ database to /var/mypath/mongodumps/mydb_[timestamp].zip and display the shell output and various debug info. To remove the debug info, remove the “true” parameter in the “run” function call.

To restore, unzip the BSON and JSON files and upload to a folder on the server with the name of the database you wish to restore to. For example, if you wish to restore “mydatabase”, you will need to put the files in a path such as /var/mypath/mongodumps/mydatabase. To restore the database, use the following command in your command prompt:

mongorestore /var/mypath/mongodumps/mydatabase

There are a number of options available for restoration that you can find in the documentation: http://docs.mongodb.org/manual/tutorial/backup-with-mongodump/

Please note that the published MongoDumper class is the simple core; there are security concerns with using shell_exec that need to be addressed before you use it on a publicly accessible site. If there is an error given by the dump or the dump did not work, make sure to check the MongoDumper.php file and the backup folder’s permissions.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 23 March 2019 · 2 min read · 340 words

Part of AskGif Blog · coding

You might also like

Backing up and restoring MongoDB databases using PHP | AskGif Blog