Exemplo n.º 1
0
def query_dump_database(hostname, port, dbname, username, password, authdb,
                        authusername, authpassword, output_path):

    # connect to the db
    mongo = pymongo.MongoClient(hostname, int(port))
    if username and password:
        mongo[authdb].authenticate(authusername, authpassword)

    db = mongo[dbname]

    # Get a list of relevant collections from the database
    db_collection_names = db.collection_names()
    db_collection_names.sort()
    db_collection_names.sort(key=len)

    if len(db_collection_names) == 0:
        print("Database for {} is empty".format(dbname))
        logging.info("Database for {} is empty".format(dbname))
        return

    #Create dump folder
    dump_folder_path = output_path + dbname if output_path[
        -1:] == '/' else output_path + '/' + dbname
    if not os.path.exists(dump_folder_path):
        os.makedirs(dump_folder_path)
    else:
        print("Dump folder for {} already exists".format(dbname))
        logging.info("Dump folder for {} already exists".format(dbname))
        return

    print("Dumping database {}".format(dbname))
    logging.info("Dumping {} database".format(dbname))

    #Use MongoCollection with dump_to_bson to dump each collection in the database
    for collection_name in db_collection_names:
        collection_dump_path = dump_folder_path + collection_name + '.json' if dump_folder_path[
            -1:] == '/' else dump_folder_path + '/' + collection_name + '.json'
        collection_to_dump = MongoCollection(hostname, port, username,
                                             password, dbname, collection_name)
        collection_to_dump.dump_to_json(collection_dump_path)
Exemplo n.º 2
0
def query_dump_database(hostname, port, dbname, username, password, authdb, authusername, authpassword, output_path):

    # connect to the db
    mongo = pymongo.MongoClient(hostname, int(port))
    if username and password:
        mongo[authdb].authenticate(authusername, authpassword)

    db = mongo[dbname]

    # Get a list of relevant collections from the database
    db_collection_names = db.collection_names()
    db_collection_names.sort()
    db_collection_names.sort(key=len)

    if len(db_collection_names) == 0:
        print("Database for {} is empty".format(dbname))
        logging.info("Database for {} is empty".format(dbname))
        return

    #Create dump folder
    dump_folder_path = output_path + dbname if output_path[-1:] == '/' else output_path + '/' + dbname
    if not os.path.exists(dump_folder_path):
        os.makedirs(dump_folder_path)
    else:
       print("Dump folder for {} already exists".format(dbname))
       logging.info("Dump folder for {} already exists".format(dbname))
       return 

    print("Dumping database {}".format(dbname))
    logging.info("Dumping {} database".format(dbname))

    #Use MongoCollection with dump_to_bson to dump each collection in the database
    for collection_name in db_collection_names:
        collection_dump_path = dump_folder_path + collection_name + '.json' if dump_folder_path[-1:] == '/' else dump_folder_path + '/' + collection_name + '.json'
        collection_to_dump = MongoCollection(hostname, port, username, password, dbname, collection_name)
        collection_to_dump.dump_to_json(collection_dump_path)