Dump complete MongoDB data to device or upload to AWS S3
Project description
mongodb-backup-s3
A Python package for backing up MongoDB databases to local storage and uploading backups to AWS S3. This package provides both a command-line interface and a Python API for automating MongoDB backup workflows.
Features
- MongoDB Backup: Export MongoDB collections to JSON files with efficient batch processing
- S3 Upload: Upload backup files to AWS S3 buckets
- Flexible Configuration: Support for environment variables and command-line arguments
- Memory Efficient: Uses cursor-based pagination for large collections
- CLI Tool: Easy-to-use command-line interface
- Python API: Programmatic access for integration into your applications
Installation
Install the package using pip:
pip install mongodb-backup-s3
Requirements
- Python 3.8 or higher
- MongoDB database (local or remote)
- AWS S3 bucket and credentials (for S3 upload functionality)
Quick Start
1. Using the Command-Line Interface
After installation, you can use the mongodb-backup-s3 command from anywhere in your terminal.
Basic Usage
Backup and upload to S3:
mongodb-backup-s3 --mongo-uri mongodb://localhost:27017/ --db-name my_database \
--aws-access-key YOUR_ACCESS_KEY --aws-secret-key YOUR_SECRET_KEY \
--s3-bucket my-backup-bucket
Backup only (save to local directory):
mongodb-backup-s3 --action backup --mongo-uri mongodb://localhost:27017/ \
--db-name my_database --output-dir ./backups
Upload existing backup to S3:
mongodb-backup-s3 --action upload --aws-access-key YOUR_ACCESS_KEY \
--aws-secret-key YOUR_SECRET_KEY --s3-bucket my-backup-bucket \
--output-dir ./backups
Using Environment Variables
Create a .env file in your project directory:
# MongoDB Configuration
MONGO_URI=mongodb://localhost:27017/
DB_NAME=my_database
OUTPUT_DIR=backups/mongodb_backup
# AWS Configuration
AWS_ACCESS_KEY=your_access_key_here
AWS_SECRET_KEY=your_secret_key_here
S3_BUCKET=my-backup-bucket
Then run the command without specifying credentials:
mongodb-backup-s3 --action both
Command-Line Arguments
| Argument | Description | Default | Required |
|---|---|---|---|
--mongo-uri |
MongoDB connection URI | From .env or environment |
For backup |
--db-name |
Name of the MongoDB database | From .env or environment |
For backup |
--output-dir |
Directory to store backup files | backups/mongodb_backup |
No |
--aws-access-key |
AWS Access Key ID | From .env or environment |
For upload |
--aws-secret-key |
AWS Secret Access Key | From .env or environment |
For upload |
--s3-bucket |
S3 bucket name | From .env or environment |
For upload |
--action |
Action to perform: backup, upload, or both |
both |
No |
2. Using the Python API
You can also use this package programmatically in your Python code:
from mongodb_backup_s3 import backup_mongodb, upload_to_s3
# Backup MongoDB database
backup_mongodb(
mongo_uri="mongodb://localhost:27017/",
db_name="my_database",
output_dir="./backups",
batch_size=1000 # Optional: documents per batch
)
# Upload backup to S3
upload_to_s3(
aws_access_key="YOUR_ACCESS_KEY",
aws_secret_key="YOUR_SECRET_KEY",
s3_bucket="my-backup-bucket",
backup_folder="./backups"
)
API Reference
backup_mongodb(mongo_uri, db_name, output_dir, batch_size=1000)
Backs up a MongoDB database to JSON files.
Parameters:
mongo_uri(str): MongoDB connection URI (e.g.,mongodb://localhost:27017/)db_name(str): Name of the database to backupoutput_dir(str): Directory path where backup files will be savedbatch_size(int, optional): Number of documents to process per batch. Default: 1000
Returns: None
Raises: Prints error messages if connection or backup fails
Example:
from mongodb_backup_s3 import backup_mongodb
backup_mongodb(
mongo_uri="mongodb://user:password@host:27017/",
db_name="production_db",
output_dir="/path/to/backups",
batch_size=5000
)
upload_to_s3(aws_access_key, aws_secret_key, s3_bucket, backup_folder)
Uploads backup files from a local directory to an AWS S3 bucket.
Parameters:
aws_access_key(str): AWS Access Key IDaws_secret_key(str): AWS Secret Access Keys3_bucket(str): Name of the S3 bucketbackup_folder(str): Path to the directory containing backup files
Returns: None
Raises: Prints error messages if credentials are invalid or upload fails
Example:
from mongodb_backup_s3 import upload_to_s3
upload_to_s3(
aws_access_key="AKIAIOSFODNN7EXAMPLE",
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
s3_bucket="my-backup-bucket",
backup_folder="/path/to/backups"
)
How It Works
Backup Process
- Connection: Connects to MongoDB using the provided URI
- Collection Discovery: Lists all collections in the specified database
- Batch Processing: For each collection:
- Counts total documents
- Processes documents in batches (default: 1000 per batch)
- Writes documents as JSON arrays to
{collection_name}.jsonfiles
- Output: Creates JSON files in the specified output directory, one file per collection
Upload Process
- S3 Client: Initializes AWS S3 client with provided credentials
- File Discovery: Walks through the backup directory
- Upload: Uploads each JSON file to S3, preserving the directory structure
- Verification: Prints confirmation for each successfully uploaded file
Output Format
Each MongoDB collection is backed up as a JSON file containing an array of documents:
[
{"_id": {"$oid": "..."}, "field1": "value1", ...},
{"_id": {"$oid": "..."}, "field1": "value2", ...},
...
]
The files use MongoDB's extended JSON format (with $oid, $date, etc.) to preserve data types.
Examples
Example 1: Complete Backup Workflow
from mongodb_backup_s3 import backup_mongodb, upload_to_s3
import os
from dotenv import load_dotenv
load_dotenv()
# Backup
backup_mongodb(
mongo_uri=os.getenv("MONGO_URI"),
db_name=os.getenv("DB_NAME"),
output_dir="./backups"
)
# Upload to S3
upload_to_s3(
aws_access_key=os.getenv("AWS_ACCESS_KEY"),
aws_secret_key=os.getenv("AWS_SECRET_KEY"),
s3_bucket=os.getenv("S3_BUCKET"),
backup_folder="./backups"
)
Example 2: Scheduled Backups
import schedule
import time
from mongodb_backup_s3 import backup_database, upload_to_s3
def daily_backup():
output_dir = f"./backups/{time.strftime('%Y%m%d')}"
backup_mongodb(
mongo_uri="mongodb://localhost:27017/",
db_name="my_database",
output_dir=output_dir
)
upload_to_s3(
aws_access_key="YOUR_KEY",
aws_secret_key="YOUR_SECRET",
s3_bucket="my-backup-bucket",
backup_folder=output_dir
)
# Schedule daily backup at 2 AM
schedule.every().day.at("02:00").do(daily_backup)
while True:
schedule.run_pending()
time.sleep(60)
Example 3: CLI with Environment Variables
# Set environment variables
export MONGO_URI="mongodb://localhost:27017/"
export DB_NAME="my_database"
export AWS_ACCESS_KEY="your_key"
export AWS_SECRET_KEY="your_secret"
export S3_BUCKET="my-bucket"
# Run backup and upload
mongodb-backup-s3 --action both
Error Handling
The package includes error handling for common scenarios:
- MongoDB Connection Errors: Displays connection error messages
- Missing Credentials: Validates required parameters before execution
- File System Errors: Handles missing directories and file access issues
- AWS Credential Errors: Detects and reports invalid AWS credentials
- S3 Upload Errors: Provides detailed error messages for failed uploads
Best Practices
- Use Environment Variables: Store sensitive credentials in
.envfiles (not in version control) - Regular Backups: Schedule regular backups for production databases
- Test Restores: Periodically test restoring from backup files
- Monitor S3 Costs: Be aware of S3 storage and transfer costs
- Secure Credentials: Use IAM roles when possible instead of access keys
- Batch Size: Adjust
batch_sizebased on document size and available memory
Troubleshooting
Connection Issues
If you encounter MongoDB connection errors:
- Verify the MongoDB URI format
- Check network connectivity
- Ensure MongoDB is running and accessible
- Verify authentication credentials if required
S3 Upload Issues
If S3 uploads fail:
- Verify AWS credentials are correct
- Check S3 bucket permissions
- Ensure the bucket exists and is accessible
- Verify IAM policies allow PutObject operations
Memory Issues
For very large collections:
- Reduce
batch_sizeparameter - Ensure sufficient disk space for backup files
- Monitor system resources during backup
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
Support
For issues, questions, or contributions, please visit the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongodb_backup_s3-0.1.1.tar.gz.
File metadata
- Download URL: mongodb_backup_s3-0.1.1.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9798cb11d5450fe1ee5c8712bd7f08bf56f1f25f917237618dd0bbeb1fd687b3
|
|
| MD5 |
bf2f9f7980d67c21396305c3149aba3d
|
|
| BLAKE2b-256 |
a641ca77adccbe4f6098847586d6ffb0964588c3e7a542251dcfdf8e4645fb75
|
File details
Details for the file mongodb_backup_s3-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mongodb_backup_s3-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02fb32cf05704a042c00eb3486776605a8acc1f6ef304045b2091cc9be88bbd8
|
|
| MD5 |
1b7e3a484ee7272abbdee05ccb124f8c
|
|
| BLAKE2b-256 |
2cbe92cb8240ac83ae5b2bcd063ad2fdb9c265f984e7677efc4252947844cd2b
|