MongoDB Backup

This section describes the strategy for capturing backups in case of data loss events, when deploying MongoDB in production.

Backup with file system Snapshots

Backup of a MongoDB deployment can be created by making a copy of MongoDB’s underlying data files.

File system snapshots are an operating system volume manager feature and are not specific to MongoDB. With file system snapshots, the operating system takes a snapshot of the volume to use as a baseline for data backup. The mechanics of snapshots depend on the underlying storage system.

This method uses system level tools to create copies of the device that holds MongoDB’s data files. It completes quickly and works reliably but requires additional system configuration outside of MongoDB.

Backup with Lua script

The entire data in the MongoDB repository can be cloned and copied to another location and then used as a backup. Using Lua, this process can be carried out in AspenTech Inmation using an object that can execute Lua scripts. The Lua functionality is provided by an internal Lua library called syslib.Backup that must be called at the beginning of the script using the require function. The MONGODB function is a method in this library and can be called using the Lua : notation. The method will stop the MongoDB service, make a copy of the data to a valid path destination. After the copy is complete, it restarts the MongoDB services. The arguments that must be provided for the function are:

  • service_name - the name of the MongoDB service (as displayed in Windows services)

  • source - the path location of the source MongoDB data store (using \\ notation for the path)

  • destination - valid path to a destination directory (using \\ notation for the path)

An example of the Lua script:

local backup = require('syslib.Backup')
local a,b = backup:MONGODB("MongoDB", "D:\\MongoDB\\Server\\4.0\\data", "D:\\del\\backup\\new")
return tostring(a) .. " : " .. tostring(b)

The function will return the message backup successful if the entire cloning was successful. If the process was not completed successfully, an error code and error message will be returned. Possible errors are:

  • stopping service failed

  • source folder doesn’t exist

  • could not delete old files

  • copying files failed

  • starting service failed

This will help you find the source of any problems.

Copying the MongoDB data can be a long process, and it is difficult to predict how long the process will take. Therefore, it is preferred to execute the MongoDB Backup functionality in Lua from an object (ActionItem or GenItem) rather than a Lua Console. This way, the object can be allocated a dedicated thread to complete the cloning process without affecting other running processes.