This script is designed to manage MongoDB replication settings across different platforms including Ubuntu/Linux, macOS, and Windows. It allows you to enable or disable replication by modifying the MongoDB configuration file and restarting the MongoDB service as required.
- Cross-platform support: Works on Ubuntu/Linux, macOS, and Windows.
- Automatically loads predefined environment variables from a
.env
file. - Makes a backup of the original MongoDB configuration file before making any changes.
- Restarts the MongoDB service to apply replication changes.
- Initiates the MongoDB replica set when enabling replication.
- MongoDB installed and configured on your system.
- Git Bash (for Windows users), WSL, or Cygwin to run the script.
- A
.env
file with the required variables.
Create a .env
file in the same directory as the script with the following content:
MONGO_DB_CONFIG_FILE_PATH="C:/path/to/your/mongod.conf" # Windows path format example
REPL_SET_NAME="yourReplicaSetName"
Note: For Unix-based systems (Linux/macOS), provide paths in Unix format.
# For enabling replication
./mongo-replication-manager.sh enable
# For disabling replication
./mongo-replication-manager.sh disable
cd /c/Users/YourUsername/path/to/your/script/
./script.sh enable
- The script loads the required environment variables (
MONGO_DB_CONFIG_FILE_PATH
andREPL_SET_NAME
) from the.env
file. - Depending on the argument passed (
enable
ordisable
), the script:- Enables replication: Adds the
replication
section to the MongoDB configuration file and sets the replica set name. - Disables replication: Removes the
replication
section from the configuration file.
- Enables replication: Adds the
- Restart the MongoDB service to apply changes.
- If replication is enabled, initiate the replica set using MongoDB commands.
- The script handles file path differences between Unix-based systems and Windows.
- Commands for restarting MongoDB services and managing file backups are designed to work on Linux, macOS, and Windows (using Git Bash, WSL, or Cygwin).
- Ensure that MongoDB is installed and accessible on your machine.
- Ensure that you have a Unix-like terminal environment set up if running on Windows (such as Git Bash, WSL, or Cygwin).
- Administrator Privileges are required to modify system-level services (like MongoDB) and configuration files.
- Permission Denied: Make sure you are running the script with administrative privileges.
- File Path Issues: Ensure paths in the
.env
file are in the correct format for your environment (Unix vs. Windows paths). - MongoDB Service Not Restarting: Check that your MongoDB service is named
mongod
and is configured correctly in your OS’s service manager.
This script is distributed under the MIT License. See LICENSE
for more information.
Developed by [Tushar]