A versatile Python-based MongoDB backup solution that offers both a graphical user interface (GUI) and command-line interface (CLI) for backing up MongoDB databases to JSON files. This tool exports collections from specified MongoDB databases into readable JSON format using mongoexport
.
- Command-line interface: Allows you to specify MongoDB URI, databases to back up, and other configurations.
- Database Export: Exports collections from specified MongoDB databases into
.json
files. - Logging: Logs the process to track successes and failures.
- Error Handling: Provides robust error handling during MongoDB connection and export operations.
- User-friendly graphical interface
- Real-time backup progress tracking
- Multi-database selection
- Connection testing capability
- Interactive file/directory selection
- Live logging window
- Multi-threaded operations to prevent UI freezing
- Command-line interface for automation and scripting
- Support for multiple database backups
- Detailed logging
- Configurable backup paths
- Error handling and reporting
- Python 3.6+
- MongoDB tools (including
mongoexport
) - Dependencies:
pymongo
tkinter
(for GUI version)
-
Clone the repository:
git clone https://github.com/yourusername/mongo-backup-tool.git cd mongo-backup-tool
-
Install dependencies:
pip install -r requirements.txt
The scripts will automatically install required dependencies if not present.
-
MongoDB Tools: Ensure MongoDB tools (including
mongoexport
) are installed on your system.
-
Launch the GUI:
python mongo_backup_gui.py
-
Using the GUI:
- Enter your MongoDB URI
- Click "Test Connection" to verify connectivity
- Select databases to backup from the list
- Choose MongoDB tools path and backup directory
- Click "Start Backup" to begin the process
- Monitor progress in real-time through the progress bar and log window
Run the script with the following command:
python mongo_backup.py --uri "<MongoDB_URI>" --databases "<Database_1>,<Database_2>" --backup-dir "<Backup_Directory>" --tools-path "<MongoDB_Tools_Path>"
-
--uri
: MongoDB URI (required) Example:mongodb+srv://username:password@cluster0.mongodb.net
-
--databases
: Comma-separated list of databases to back up (required) Example:appointment,cdn
-
--backup-dir
: Backup directory path (default:./database_backup
) -
--tools-path
: MongoDB tools path (default:./mongodb-database-tools/bin
)
python mongo_backup.py --uri "mongodb+srv://username:password@cluster0.mongodb.net" --databases "appointment,cdn" --backup-dir "./backups" --tools-path "./mongodb-database-tools/bin"
- Connection: The tool connects to MongoDB using the provided URI
- Database Selection:
- GUI: Select databases from the interactive list
- CLI: Specify databases via command-line arguments
- Export Process:
- Uses
mongoexport
to extract collections - Converts data to formatted JSON files
- Organizes backups in the specified directory
- Uses
- Progress Tracking:
- GUI: Real-time progress bar and log window
- CLI: Terminal-based logging
backup_directory/
├── database1/
│ ├── collection1.json
│ ├── collection2.json
│ └── collection3.json
└── database2/
├── collection1.json
└── collection2.json
- Connection Failed: Verify MongoDB URI and network connectivity
- Export Error: Ensure
mongoexport
is properly installed and path is correct - Permission Error: Check write permissions for backup directory
- GUI Not Loading: Verify tkinter is installed (
pip install tk
)
- Use the "Test Connection" button to verify MongoDB connectivity
- Check the log window for detailed error messages
- Ensure all paths are correctly set before starting backup
- Review command-line arguments for accuracy
- Check console output for error messages
- Verify database names are correctly comma-separated
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Vimukthi Indunil
GitHub