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))
source share