12

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

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

Thanks a lot :-).

Gx1sptDTDa
  • 1,558
  • 2
  • 11
  • 23
  • Pymongo and therefore MongoEngine only connect to the `mongod` process. However, a way around your problem might be to simply run `mongodump` on a remote machine, because you can connect to any remote database with `mongodump/restore` etc. You don't need to run `mondodump` on the machine that is hosting the db. Make sense? – MFB Jul 08 '14 at 06:31
  • 1
    Meh. I guess it's easier to just extract all documents and then build a BSON file. – Gx1sptDTDa Jul 10 '14 at 08:56

2 Answers2

14

For my relatively small small database, I eventually used the following solution. It's not really suitable for big or complex databases, but it suffices for my case. It dumps all documents as a json to the backup directory. It's clunky, but it does not rely on other stuff 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))
Gx1sptDTDa
  • 1,558
  • 2
  • 11
  • 23
  • 1
    thanks for posting this! Does this differ in any way from cli `mongodump`? Will the resulting json file be usable with `mongorestore` ? – Cmag Jun 16 '15 at 18:07
  • 1
    You could consider iterating over the result of `col.find()` if you don't intend to hold all the documents in memory at once. You _did_ mention "relatively small small database". – farthVader Nov 04 '15 at 20:44
  • can we store as bson? – Kishan Mehta Feb 09 '17 at 06:47
  • 3
    This should be from bson.json_util import dumps instead of utils.!!!!!! – Kishan Mehta Feb 09 '17 at 07:30
  • I found this solution helpful, but i also want backup in single file. Any suggestions how can i archive in single file as export and import that file to restore database? – Krunal Sonparate Oct 01 '19 at 07:04
  • 2
    This is good, but will not create `.bson` e `.metadata.json` files. So I think restore using `mongorestore` will not be able. – igorkf May 27 '20 at 15:09
5

The accepted answer is not working anymore. Here is a revised code:

from os.path import join
import pymongo
from bson.json_util import dumps

def backup_db(backup_db_dir):
    client = pymongo.MongoClient(host=..., port=..., username=..., password=...)
    database = client[<db_name>]
    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).encode())


backup_db('.')
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124