Python Library to Backup and Restore MongoDB
Project description
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 = {
'protocol': 'mongodb',
'host': '3.108.158.64:27017',
'username': 'admin',
'password': 'XXXXXXXXXXXXXXXX',
'params': '?authSource=admin', # Optional
}
# (OR) Connection with URI
config = {
'host_uri': '', # If MongoDB URI (Optional)
'params': '?authSource=admin', # Optional
}
# 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",
backup_folder = "BACKUP_FOLDER",
compression="gzip" # (Optional)
)
# (OR) Backup a Collection
mongo_handler.backup(
database_name = "DATABASE_NAME",
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",
)
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
Close
Hashes for py_mongo_backup_restore-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | befe01ec86722bd09d96c5d39a060635189250591c3c17c5c0f9a41ce899a4e5 |
|
MD5 | 03bd2612447123bec46a53713b3db6e8 |
|
BLAKE2b-256 | 441ec9bed07ed9d75311bd3060acf337bddc6976a5d8a98ec6ec96accafecd43 |