Equivalent to PyMongo / Mongoengine mongodump

Is there an equivalent function in PyMongo or mongoengine for MongoDB mongodump ? I can't seem to find anything in the docs.

Use case: I need to periodically backup the remote mongo database. The local machine is a production server on which mongo is not installed, and I do not have administrator rights, so I can not use subprocess to call mongodump . I can install the mongo client locally on virtualenv, but I would prefer an API call.

Thank you very much:-).

+6
source share
1 answer

For my relatively small small database, I ended up using the following solution. This is not very suitable for large or complex databases, but it is enough for my case. It deletes all documents as json in the backup directory. It is clumsy, but it does not depend on other things than pymongo.

 from os.path import join import pymongo from bson.json_utils import dumps def backup_db(backup_db_dir): client = pymongo.MongoClient(host=<host>, port=<port>) database = client[<db_name>] authenticated = database.authenticate(<uname>,<pwd>) assert authenticated, "Could not authenticate to database!" collections = database.collection_names() for i, collection_name in enumerate(collections): col = getattr(database,collections[i]) collection = col.find() jsonpath = collection_name + ".json" jsonpath = join(backup_db_dir, jsonpath) with open(jsonpath, 'wb') as jsonfile: jsonfile.write(dumps(collection)) 
+5
source

Source: https://habr.com/ru/post/971865/


All Articles