mongodump ignore some specified collections
Asked Answered
C

5

58

I was trying to backup my mongo database on the product sever.and then restore then back to the staging server. and here comes some problem, there are a lot of collections in db, I want to igonre some collections that I don't want to restore on staging server.

I can approach this by dumpping the staging db, dumpping the producting db, and then restore the prodct to staging useing --drop option. and restore the specified collections in staging db. uh..it's really bad.

1. dump producting db

mongodump --host product-server-host --username abcd --password bcda -d db -o pruduct-dump-dir

2. dump staging db

mongodump --host staging-server-host --username abcd --password bcda -d db -o staging -dump-dir

3. restore all collection, then restore the collection back restore pruduct-dump-dir to staging server

mongorestore --host staging-server-host --username abcd --password bcda --drop pruduct-dump-dir

mongorestore --host staging-server-host --username abcd --password bcda --drop --collection coll pruducting-dump-dir

Is there any option like ignore-collection when I'm dumpping? any suggestion will be appreciated :3

Clamshell answered 12/4, 2013 at 3:22 Comment(0)
B
111

Now available from version 3.0.0

--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>

Repeat to exclude more than 1

Checkout the documentation

Brendanbrenden answered 10/3, 2015 at 23:32 Comment(3)
Said documentation seems to give absolutely no indication of what it means by "array of strings". There isn't really a unified idea of arrays as parameters in the shell, so that could mean anything. Is it one string with comma separators? Is it any number of strings until a new flag is encountered? Are we supposed to put ()s or [] around it? What about repeating the flag with a different collection? All of these are used as replacements for arrays, it'd be nice if someone could clarify what they actually want.Gombosi
@Thor84no you are right, no indication what it means. But to exclude more than one collection you can repeat the excludeCollection parameter (I think this is what they mean with array). Example: mongodump --excludeCollection=users --excludeCollection=jobs -d mydatabaseCaddie
Finally I'm able to use the --excludeCollection! Thanks @DanielPérezRadaSaloma
Y
14
mongodump --db test --excludeCollection=users --excludeCollection=salaries
Yasukoyataghan answered 2/10, 2017 at 16:13 Comment(1)
would it be possible to add description to answer. I am not sure code only answers are good and stackoveflow standard.Prosaism
D
9

You can add --collection COLLECTION_NAME to dump the collection you need. By default, if you do not specify a collection to dump from a database, MongoDump will dump all collections in that database.

Discordance answered 12/4, 2013 at 9:18 Comment(3)
what if I have 20 collections need to dump except 4 collectionsClamshell
You'll need to dump each collection individually.Discordance
yeah something like --exclude_collection col1 col2 col3 would be amazing. No mention of it in docs.mongodb.org/manual/reference/program/mongodump . There appears to be a feature request in the MongoDB Jira: jira.mongodb.org/browse/SERVER-2459 you should vote for the issueSessoms
B
8

As of Mongo 3.4, you can now specify an --nsExclude <namespace pattern> option when restoring from a Mongo database dump, which will exclude the specified namespaces from the restore operation. This is particularly useful when the mongodump operation has already occurred.

Official documentation here: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude

You can exclude multiple collections with wildcards:

mongorestore --db test --nsExclude 'test.*_tmp'

Or alternatively, specifying multiple --nsExclude options work as well:

mongorestore --db test --nsExclude 'test.collection1' --nsExclude 'test.collection2'
Barong answered 14/11, 2017 at 23:2 Comment(1)
You can use --nsInclude as well instead of --db mongorestore --nsInclude test --nsExclude 'test.collection1' --nsExclude 'test.collection2'Sextuplicate
U
0

I had to do the same while backing up a mongo db. If you use python (or any other language), you can use similar approach as well. After doing the mongodump, you simple have to remove the unwanted collection's bson & the metadata.json files.

    import os
    EXCLUDE_COLLECTIONS = ['collection_1', 'collection_2']
    db_dump_path = "/Path/to/mongodump"
    db_name = "name_of_db"
    for collection_name in EXCLUDE_COLLECTIONS:
        bson_file_path = os.path.join(db_dump_path, db_name, '{}.bson'.format(collection_name)
        meta_file_path = os.path.join(db_dump_path, db_name, '{}.metadata.json'.format(collection_name)
        if os.path.exists(bson_file_path) and os.path.exists(meta_file_path):
            os.remove(bson_file_path)
            os.remove(meta_file_path)
Underarm answered 19/2, 2019 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.