Export all databases with mongoexport - mongodb

In my app, I'm creating a new database for every user. So I have many databases currently. I need to back up them. But I couldn't find a way to export all databases and collections at the same time with mongoexport
Do anyone have solution?

You can do it using root user and password.
./mongodump --uri="mongodb://rootUser:rootPassword#localhost:27017/?authSource=admin"
This will create a dump folder.
For restoring ensure that your current directory contains that dump folder then.
./mongorestore -h localhost -p 27017 -u rootUser:rootPassword -p rootPassword

Related

Dumping mongodbdatabase using meteor locally

I need to know how can dump the mongo db database locally using meteor.The database 'test' resides the folder name 'reaction' is dumping to another folder in same drive.How it is possible?Please help.Thanks in advance
In order to dump the database use the command
mongodump -h 127.0.0.1 --port 3001 -d meteor
After issuing this command a new folder "dump" will be created with your data.

Mongorestore don't know what to do with file "db/collection.bson", skipping

I want to migrate my mongodb from 2.0 to 3.0. So I followed the official doc to use mongodump to backup my dbs and use mongorestore to restore the dbs to mongodb 3.0.
But when I use mongorestore, it tells me "don't know what to do with file "db/collection.bson", skipping...".
Nothing to do. How could I migrate my dbs?
Thanks.
EDIT: Here is my steps.
Use mongodump in mongodb 2.0
mongodump
tree dump
db
├── collection-1.bson
├── collection-2.bson
├── collection-3.bson
├── ...
Copy db directory to mongodb 3.0 server.
On the mongodb 3.0 server calls mongorestore db
But I get this error:
mongorestore db
2015-03-10T09:36:26.237+0800 building a list of dbs and collections to restore from db dir
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-1.bson", skipping...
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-2.bson", skipping...
2015-03-10T09:36:26.237+0800 don't know what to do with file "db/collection-3.bson", skipping...
...
2015-03-10T09:36:26.237+0800 done
It seems one must also specify -d in 3.0 like this:
mongorestore -d db db
This answer isn't directly related to your issue case, but the same error output you will receive trying to restore mongo data, dumped with --archive option, available since MongoDB 3.2 version. To resolve this you need to specify --gzip parameter in your mongorestore command. Example:
mongorestore -d destination_db --gzip /path/to/source/db
If you have lost your mongoDb just try running this command:
mongorestore -d destination-db --gzip source-db
Where:
destination-db is the name of the destination database
source-db is the name of the source database.
Following command worked for me in mongoDB 6.0 -
mongorestore --host localhost --port 27017 --db DB-NAME DB-BACKUP-PATH
Please replace DB-NAME and DB-BACKUP-PATH with your actual values. For ex.
mongorestore --host localhost --port 27017 --db demo_db /home/ubuntu/db-backup
Where "/home/ubuntu/db-backup" contains all collections in bson and json format.
Try this one.
mongorestore -db dataBaseName dataBasePath
In order to import data to your mongodb ,You need to specify --db configuration in mongorestore function with name and path, this works for me for bson and json files as well:
mongorestore --db <dbName> <dbPath>
check the data was imported properly using mongo cli
mongo
show databases
use <dbName>
show collections
With mongorestore, the path of those dump files is a required parameter; you've got that right so far, by indicating db.
It is also a good idea as Peter has said to indicate a database to restore the dump files into collections with a /d switch.
Something I didn't realize while struggling with this is that a mongod must be running to consume the restoration. If you have more than 1 mongod running, you should definitely indicate a port with the --port switch. The code that worked for me was:
mongod --dbpath config --port 27019 --logpath log.config --logappend
And in another CLI:
mongorestore --port 27019 /d config config
followed by
mongo localhost:27019/config
to verify that the collections were filled properly.
You want to run mongorestore on the dump directory, not the db/ directory.
mongorestore dump/
Please Follow these intstructions it worked for me
First go to command prompt .
run cd C:\Program Files\MongoDB\Server\3.0\bin .
now ,For suppose your data is in a folder called trading and it contains json files .
and the path for that might be c:\Users\documents\trading.
(mongorestore -d dbName dbPath).
now run mongorestore -d trading c:\Users\documents\trading.
your files will be restored.
Not directly related to your situation but someone could stumble upon this post with similar error for this reason
In my case, the data was provided to me and had been dumped using
mongodump --db ${DB_NAME} --gzip -o ${BACKUP_FILE_GZ}
To restore, you must use following format:
mongorestore --gzip --db "${DB_NAME_RESTORE}" ${BACKUP_FILE_GZ}/${DB_NAME}
mongorestore --uri "mongodb+srv://<user>:<password>#<host>" -d <dbname> <dump dir. address ./something/like/this>
This work fine for me
I used docker to start my mongo db instance & I had default username and password
auth set for database.
Below is how my restore looked like.
mongorestore --authenticationDatabase admin -u root -p example -d test-db /data/test-db
So in-short something of this sort should work.
mongorestore --authenticationDatabase admin -u {USERNAME} -p {PASSWORD} -d {DB_NAME} {/DUMP_PATH}

Copying MongoDB Database into Local Machine

I have a MongoDB database that resides on a remote server machine whose IP address is 192.168.1.20 on a local network. For development and testing purposes, and since I am not allowed to modify or delete the database on the server for security purposes, I want to copy the database on my local machine for my personal use.
Can anyone please tell me, how do I achieve this?
I do this by creating a dump of the remote db to my local machine, which I then restore:
Make sure you have a mongo instance up and running (eg. run mongod.exe from your bin folder in a terminal window. On my windows computer that's C:\mongodb\bin)
Make a dump from remote db: Open a new terminal window, move to the bin folder again, run:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass
(Change the parameters to suit your own situation.)
Restore the dumped database: Once the dump has been made, run the following command so that you have a local db:
mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB
(replace nameOfRemoteDB with the name of the remote db, the same as in previous command, and replace theNameYouWantForYourLocalDB with the name that you want your new local db to have)
There is copy database command which I guess should be good fit for your need.
db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018");
Alternatively, you can just stop MongoDb, copy the database files to another server and run an instance of MongoDb there.
EDIT 2020-04-25
Quote from MongoDB documentation
MongoDB 4.0 deprecates the copydb and the clone commands and their mongo shell helpers db.copyDatabase() and db.cloneDatabase().
As alternatives, users can use mongodump and mongorestore (with the mongorestore options --nsFrom and --nsTo) or write a script using the drivers.
Reference here
This should be a comment to the answer of #malla, but I don't have enough reputation to comment so I'm posting it here for other's reference.
In step 2, When you are trying to dump file from a remote server, remember to add out option so that you can restore locally later: (in my first try, I didn't add it and it failed, saying dump\db_name was not found).I'm not sure whether my way efficient or not. But it worked for me.
Step 2:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass --out <path_you_want_to_dump>
Step 3:
mongorestore -d theNameYouWantForYourLocalDB \<path_you_want_to_dump> + nameOfRemoteDB
The mongoexport command:
http://docs.mongodb.org/manual/core/import-export/
Or, mongodump command:
http://docs.mongodb.org/manual/reference/program/mongodump/
mongodb has commandline tools for importing and exporting. Take a look at mongodump --collection collection --db test and mongorestore --collection people --db accounts dump/accounts/
http://docs.mongodb.org/v2.2/reference/mongodump/
http://docs.mongodb.org/v2.2/reference/mongorestore/
this even works over the network
You can use the mongoexport command to copy the database to your local machine.

Copying localhost MongoDB data to meteor servers

So I have been playing around with meteor and mongodb and have a working version set up on my localhost. Unfortunately, when I do meteor deploy xxx.meteor.com it doesn't deploy my database as well.
How do I do this?
Meter deploy only deploys a fresh database. To copy over your data you have to use mongorestore with your local mongodb dump, which you can make with mongodump (docs)
So first dump your database somewhere
mongodump --host localhost:3002
Get your mongodb`s credentials by running (in your project dir):
meteor mongo myapp.meteor.com --url
This will give you your database details in the form:
mongodb://username:password#host:port/databasename
Then you can plug these into mongorestore (docs) and restore your local database over
mongorestore -u username -p password -h host:port -d databasename ~/desktop/location_of_your_mongodb_dump

How to use backup and restore all databases from mongodb?

If i want to do a generic backup for all databases in mongodb, is it that all i have to do is:
$ mongodump
And if i want to restore the latest dump i've created, all i need to do is:
$ mongorestore
Where are the backups from the mongodump stored?
How to i specify a specific dump for all databases to be restored?
The backups are stored in the directory that you have specified with the --out option in command line. If you do not specify any output dir the backup will be placed to ./dump directory. With the mongorestore you have to specify the directory where you have dumped before as a command line argument.
On sharded environment the backup will be flattened if you use mongodump through a mongos. After restore you will have to re-shard the collections. so restoring not always effortless. See documentation:
http://docs.mongodb.org/manual/tutorial/backup-small-sharded-cluster-with-mongodump/
You can dump directly the db folders too, check the cli options.
For sharded clusters you can check the possibilities here: http://docs.mongodb.org/manual/administration/backups/#sharded-cluster-backups
restore all
mongorestore --host= --port= --username= --authenticationDatabase= --nsInclude "."
backup all
mongodump --ssl --host --port -p --authenticationDatabase -u -p --out
If you want to backup databases use:
mongodump --ssl --host --port -p --authenticationDatabase -u -p --out
if the database has ssl enabled include the --ssl flag
if you don't include the --out mongodump will create a "/dump" directory.
inside the dump or specified backup directory you'll find directories with the names of your databases, inside each of them, you'll find the backups files, for each collection you'll find a ".bson" and a ".metadata.json"
To restore all the databases use:
mongorestore --ssl --host= --port= --username= --authenticationDatabase= --nsInclude "."
Again if the database has ssl enabled include the --ssl flag if not just remove it.
the "--nsInclude" flag tell mongorestore which databases or collections you want to restore.
Examples:
--nsInclude=test.users (this will backup the users collection of the database test, and therefore will fail if the path to the dump is anything but the path to the users.bson of that particula database)
To include all the database and all the collections use --nsInclude=. or --nsInclude .
then define the path to all the collection directories of your backup