mongorestore of specific database from full dump - safe? - mongodb

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.

Related

Why isn't mongodump and mongorestore working?

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.

mongodump --oplog and per database restore

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

Move a collection from one DB to another DB within the same MongoDB instance

I have many collections in database A. I want to copy some of them to database B in the same MongoDB.
I tried to copy whole database using db.copyDatabase('A', 'B'), but it has more than 300 GB data. It will take ages to copy and I just want to copy a few collections from database A to database B.
Does anyone know how can I do it?
You can try it using mongo shell. you can copy collection from one db to another db using renameCollection. the renameCollection can run from admin database so first need to switch to admin db.
so ca follow bellow steps in mongo shell:
step-1: run this comment use admin
step-2: run bellow comment
db.runCommand({renameCollection:"sourcedb.sourceCollection",to:"targetdb.tragetCollection"})
for example:
use admin
db.runCommand({renameCollection:"funnel.countries",to:"test.countries"})
copied countries collection from funnel db to test db.
In background MongoDB will create dump for source collection and restore the dump automatically to target db collection
Use mongodump to dump the collections:
mongodump --db A --collection coll --out yourbackupdir
and then import the collections using mongorestore:
mongorestore --db B --collection coll yourbackupdir/

Taking a Mongo DB dump

I've seen the mongodump command used to export a specific database, but how can I find the name of the available databases before choosing one?
To confirm, is this correct?
mongodump -d <database_name> -o <directory_backup>
Also, what effect will the command alone, without any flags, have if run on the database? Thanks!
You can show the databases available with
show dbs
If you do not specify a database it will dump all databases.
https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption--db

drop whole database within a single command of restoring the dump of mongodb

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.