Percona ClusterSync for MongoDB is a tool for replicating data from a source MongoDB cluster to a target MongoDB cluster. It supports cloning data, replicating changes, and managing collections and indexes.
- Clone: Instantly transfer existing data from a source MongoDB to a target MongoDB.
- Real-Time Replication: Tail the oplog to keep your target cluster up to date.
- Namespace Filtering: Specify which databases and collections to include or exclude.
- Automatic Index Management: Ensure necessary indexes are created on the target.
- HTTP API: Start, finalize, pause, resume, and check replication status via REST endpoints.
- Go 1.24 or later
- MongoDB 6.0 or later
- Python 3.13 or later (for testing)
- Poetry (for managing Python dependencies)
-
Clone the repository:
git clone https://github.com/percona/percona-clustersync-mongodb.git cd percona-clustersync-mongodb -
Build the project using the Makefile:
make build
Alternatively, you can install PCSM from the cloned repo using
go install:go install .This will install
pcsminto yourGOBINdirectory. IfGOBINis included in yourPATH, you can run Percona ClusterSync for MongoDB by typingpcsmin your terminal. -
Run the server:
bin/pcsm --source <source-mongodb-uri> --target <target-mongodb-uri>
Alternatively, you can use environment variables:
export PCSM_SOURCE_URI=<source-mongodb-uri> export PCSM_TARGET_URI=<target-mongodb-uri> bin/pcsm
To start the replication process, you can either use the command-line interface or send a POST request to the /start endpoint with the desired options:
bin/pcsm startcurl -X POST http://localhost:2242/start -d '{
"includeNamespaces": ["db1.collection1", "db2.collection2"],
"excludeNamespaces": ["db3.collection3", "db4.*"]
}'To finalize the replication process, you can either use the command-line interface or send a POST request to the /finalize endpoint:
bin/pcsm finalizecurl -X POST http://localhost:2242/finalizeTo pause the replication process, you can either use the command-line interface or send a POST request to the /pause endpoint:
bin/pcsm pausecurl -X POST http://localhost:2242/pauseTo resume the replication process, you can either use the command-line interface or send a POST request to the /resume endpoint:
bin/pcsm resumecurl -X POST http://localhost:2242/resumeTo check the current status of the replication process, you can either use the command-line interface or send a GET request to the /status endpoint:
bin/pcsm statuscurl http://localhost:2242/statusWhen starting the PCSM server, you can use the following options:
--port: The port on which the server will listen (default: 2242)--source: The MongoDB connection string for the source cluster--target: The MongoDB connection string for the target cluster--log-level: The log level (default: "info")--log-json: Output log in JSON format with disabled color--no-color: Disable log ASCI color
Example:
bin/pcsm \
--source <source-mongodb-uri> \
--target <target-mongodb-uri> \
--port 2242 \
--log-level debug \
--log-jsonPCSM_SOURCE_URI: MongoDB connection string for the source cluster.PCSM_TARGET_URI: MongoDB connection string for the target cluster.PCSM_MONGODB_CLI_OPERATION_TIMEOUT: Timeout for MongoDB client operations; accepts Go durations like30s,2m,1h(default:5m).
When using the --log-json option, the logs will be output in JSON format with the following fields:
time: Unix time when the log entry was created.level: Log level (e.g., "debug", "info", "warn", "error").message: Log message, if any.error: Error message, if any.s: Scope of the log entry.ns: Namespace (database.collection format).elapsed_secs: The duration in seconds for the specific operation to complete.
Example:
{ "level": "info",
"s": "clone",
"ns": "db_1.coll_1",
"elapsed_secs": 0,
"time": "2025-02-23 11:26:03.758",
"message": "Cloned db_1.coll_1" }
{ "level": "info",
"s": "pcsm",
"elapsed_secs": 0,
"time": "2025-02-23 11:26:03.857",
"message": "Change replication stopped at 1740335163.1740335163 source cluster time" }Starts the replication process.
includeNamespaces(optional): List of namespaces to include in the replication.excludeNamespaces(optional): List of namespaces to exclude from the replication.
Example:
{
"includeNamespaces": ["dbName.*", "anotherDB.collName1", "anotherDB.collName2"],
"excludeNamespaces": ["dbName.collName"]
}ok: Boolean indicating if the operation was successful.error(optional): Error message if the operation failed.
Example:
{ "ok": true }Finalizes the replication process.
ok: Boolean indicating if the operation was successful.error(optional): Error message if the operation failed.
Example:
{ "ok": true }Pauses the replication process.
ok: Boolean indicating if the operation was successful.error(optional): Error message if the operation failed.
Example:
{ "ok": true }Resumes the replication process.
fromFailure(optional): Allows PCSM to resume from failed state
Example:
{
"fromFailure": true
}ok: Boolean indicating if the operation was successful.error(optional): Error message if the operation failed.
Example:
{ "ok": true }The /status endpoint provides the current state of the PCSM replication process, including its progress, lag, and event processing details.
-
ok: indicates if the operation was successful. -
state: the current state of the replication. -
info: provides additional information about the current state. -
error(optional): the error message if the operation failed. -
lagTime: the current lag time in logical seconds between source and target clusters. -
eventsProcessed: the number of events processed. -
lastReplicatedOpTime: the last replicated operation time. -
initialSync.completed: indicates if the initial sync is completed. -
initialSync.lagTime: the lag time in logical seconds until the initial sync completed. -
initialSync.cloneCompleted: indicates if the cloning process is completed. -
initialSync.estimatedCloneSize: the estimated total size of the clone. -
initialSync.clonedSize: the size of the data that has been cloned.
Example:
{
"ok": true,
"state": "running",
"info": "Initial Sync",
"lagTime": 22,
"eventsProcessed": 5000,
"lastReplicatedOpTime": "1740335200.5",
"initialSync": {
"completed": false,
"lagTime": 5,
"cloneCompleted": false,
"estimatedCloneSize": 5000000000,
"clonedSize": 2500000000
}
}-
Install Poetry:
curl -sSL https://install.python-poetry.org | python3 - -
Install the required Python packages:
poetry install
To build the project for testing, use the following command:
make test-buildTo run the tests, use the following command:
poetry run pytest \
--source-uri <source-mongodb-uri> \
--target-uri <target-mongodb-uri> \
--pcsm_url http://localhost:2242 \
--pcsm-bin bin/pcsm_testAlternatively, you can use environment variables:
export TEST_SOURCE_URI=<source-mongodb-uri>
export TEST_TARGET_URI=<target-mongodb-uri>
export TEST_PCSM_URL=http://localhost:2242
export TEST_PCSM_BIN=bin/pcsm_test
poetry run pytestThe
--pcsm-binflag orTEST_PCSM_BINenvironment variable specifies the path to the PCSM binary. This allows the test suite to manage the PCSM process, ensuring it starts and stops as needed during the tests. If neither the flag nor the environment variable is provided, you must run PCSM externally before running the tests.
Contributions are welcome. Please open a JIRA issue describing the proposed change, then submit a pull request on GitHub.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.