Python MongoDB Backup & Restore
Python Library to Backup and Restore MongoDB
GitHub Repo: https://github.com/sannjayy/py-mongo-backup-restore
Installaion
Do the following in your virtualenv:
pip install py-mongo-backup-restore
Import:
from py_mongo_backup_restore import PyMongoBackupRestore
Configuration:
from py_mongo_backup_restore import PyMongoBackupRestore
# Database Configuration:
config = {
'scheme': 'mongodb',
'host': '37.108.158.64:27017',
'username': 'username',
'password': 'password',
'extra_options': '?authSource=admin', # Optional
'database_name': 'test', # Optional
}
# (OR) Connection with URI
config = {
'connection_string': 'mongodb+srv://username:password@host.gp2xb.mongodb.net/database?retryWrites=true&w=majority'
}
# Creating Instance
mongo_handler = PyMongoBackupRestore(**config)
print('URI -> ', mongo_handler.get_uri()) # Returns the Mongo Host Uri
To check if mongodump and mongorestore commands are working
This script checks the version of mongodump and mongorestore commands using the --version flag. If the commands are working, it prints a success message; otherwise, it prints an error message.
PyMongoBackupRestore(**config).check_mongodump_mongorestore()
Backup Database:
# Backup Full Database
mongo_handler.backup(
database_name = "DATABASE_NAME", # Optional if a database_name is provided in the config.
backup_folder = "BACKUP_FOLDER",
compression="gzip" # (Optional)
)
# (OR) Backup a Collection
mongo_handler.backup(
database_name = "DATABASE_NAME", # Optional if a database_name is provided in the config.
collection_name = "COLLECTION_NAME",
backup_folder = "BACKUP_FOLDER",
compression="gzip" # (Optional)
)
Restore Database:
# Restore Full Database
mongo_handler.restore(
database_name = "DATABASE_NAME", # Target Database Name
backup_folder = "BACKUP_FOLDER/BACKUP_NAME",
)
# (OR) Restore a Collection
mongo_handler.restore_collection(
database_name = "DATABASE_NAME", # Target Database Name
collection_source = "BACKUP_FOLDER/BACKUP_NAME/file.bson",
collection_name = "COLLECTION_NAME",
)
Metadata
Release files for py-mongo-backup-restore 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| py_mongo_backup_restore-2.1.0.tar.gz | 4.5 kB | Without provenance | Details |
Release files / py_mongo_backup_restore-2.1.0.tar.gz
| Download URL | py_mongo_backup_restore-2.1.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8bd69df91c01215d62fb7046b41baddf011ccfcf69f7f1cc28df860f6231d568
|
|
BLAKE2b-256 checksum How to use checksums |
5f7a608aa4c7016171428bc11bbd6d2f652c7b3853ada8748e342b9cbdd78a7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.2
|
Security for py-mongo-backup-restore 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.