I need to be able to restore a single database, even a single collection from a backup. Since mongodump --oplog only applies to a full instance (Replica Set), I made the following procedure to filter only the entries from the db that I want to restore from the oplog.bson generated by the --oplog option.
Could someone tell me if this is correct or if I'm missing something?
First I restore only the db (the db is test in this example) that I need
mongorestore -d test dump/test
Restore into a random collection the oplog.bson file generated by the --oplog option
mongorestore -d oplog -c oplog dump/oplog.bson
Dump from the restored oplog.bson collection only the documents that refer to the db that I'm restoring
mongodump -d oplog -c oplog -q "{ns:/^test[.]/}" -o oplog
Restore with the --oplogReplay option using the last dump with filtered operations
mongorestore --oplogReplay oplog/oplog
Finally I drop the temporary oplog collection.
mongo --eval "db.getSisterDB('oplog').dropDatabase()"
Thanks in advance!
Basically, that's one way to do it.. Or you can do extra export after dumping specific database.
mongoexport -d local -c oplog.rs --query="{ns:/^test[.]/}" -o oplog.dump
2017-10-10T11:51:50.780+0300 connected to: localhost
2017-10-10T11:51:51.737+0300 local.oplog.rs 0
2017-10-10T11:51:51.938+0300 local.oplog.rs 3
2017-10-10T11:51:51.938+0300 exported 3 records
And you have now direct json dump file what you can read back
Related
I am doing to following to sync mongodb database between two instances as follows but it doesn't seem to work,anyone know what should I do to fix it?
mongodump --uri="mongodb://username1:PASSWORD1#x.y.z.w:27017" --db=databaseName --archive="mongodbdump.bson"
mongorestore --username=username2 --password=password2 --authenticationDatabase=databaseName --host=replicaset1:port1,replicaset2:port2,replicaset3:port3 --archive=mongodbdump.bson
Error:
0 document(s) restored successfully. 41419 document(s) failed to restore
You probably have an unique index conflict with the _id, you need to use the --drop option
Before restoring the collections from the dumped backup, drops the
collections from the target database. --drop does not drop collections
that are not in the backup.
I must restore a single Mongodb database from a full backup of many databases
without touching the other databases:
e.g. $ mongorestore --gzip --drop --db ONEDB dbbackupfile
Will the restore command honour the --db ONEDB and ONLY drop the ONEDB database collection? Or will it drop them all?
[The documentation for mongorestore only says that --drop will drop all collections in the backup - it doesn't say what effect the --db option has on the --drop flag...]
Thank you in advance for your help.
I understand the fear of dropping all your databases,
With that being said the documentation clearly states:
drops the collections from the target database.
i recommend that if your experimenting with this command for the first time and you're not confident with it yet you should test it not on production but on your local first, or at the very least run it with --dryRun flag just to see if your getting the results you want.
When I restore a collection with the following command:
mongorestore --db mydb --drop --collection mycollection --batchSize=100 mycollection.bson
as the original collection is dropped, the indexes are lost. I can see that there is also a mycollection.metadata.json file which contains indexes of this collection, but I cannot find in the documentation how this file can be restored.
All I've found is how to restore an entire database, which restores all collections with metadata from a directory. However, I want to restore only a single collection. How do I do that?
Note: I am using mongo version 3.0.7
You don't need to do something specific to restore metadata.
mongorestore does this for you.
When you restore collection:
mongorestore --collection mycollection --db mydb mycollection.bson
mongorestore checks directory where mycollection.bson exist for mycollection.metadata.json file. Just keep metadata file in the same directory as collection.
I am trying to restore the directory dump of mongodb.
i am doing
mongorestore --db mydb --drop path/to/mydb/dump
But both does not able to restore the state of my dump.
Any new records are visible even after restoring the db.
But no error is shown on console.
I didn't see an answer and I had the same question today.
You can drop the database before with:
use <db>
db.dropDatabase()
Or you can only drop the collection with:
db.<collection>.drop()
The problem with your command could be that something misses, like the database which you authenticate against or the user or maybe another thing.
In my setup this works
mongorestore --username=<user> --db=<database> --authenticationDatabase=<database> --dir=<dumpdir> --drop
If your dump was zipped beforehand, you can add the --gzip flag in the end.
You can find all that in the documentation for dropping a database or in the documentation of dropping a whole database. But please be cautious with it.
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
Now available from version 3.0.0
--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>
Repeat to exclude more than 1
Checkout the documentation
mongodump --db test --excludeCollection=users --excludeCollection=salaries
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.
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'
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)