drop whole database within a single command of restoring the dump of mongodb - 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.

Related

mongorestore of specific database from full dump - safe?

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.

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

Restoring the data from pg_dump doesn't overwrite the data but it appends the data to the original database

I am taking the dump of postgres database using "pg_dump database_name > backup.sql". Later on I am doing some modifications in the original database(database_name) and then I am restoring the data from the backup file(backup.sql). But the result is that, the database doesn't gets restored to the original state, instead it adds the original data to the modified data(modified + original).I just want it to restore to the original state, shall i delete all the data from the database before restoring it from the backup file, since it gives the original state of the database. Or is there any other way to do this?
The default format fo pg_dump is plain, so it creates a COPY statement. Hence when you psql backup.sql you just run those copy over existing data. To rewrite data, you should either drop tables first or pg_dump -F c and pg_restore -c.
Warning - in both cases it will destroy old data (this seems what you want though)
-c
--clean Clean (drop) database objects before recreating them. (Unless --if-exists is used, this might generate some harmless error messages, if any objects were not present in the destination database.)
As #Craig Ringer suggests, drop/recreate from backup would be much easier and cleaner. To drop database you run DROP DATABASE au - note that there should be no connected users to success. Then you have to create db: CREATE DATABASE au and run psql -f backup.sql -d au
Take the dump with -c option: pg_dump -c database_name > backup.sql. See pg_dump docs.

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