How to use backup and restore all databases from mongodb? - 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

Related

MongoRestore is giving me the system cannot find the file

I am trying to export my local mongodb data to an atlas cluster and i created a dump and now i am using the command,
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin
to try to restore it but is giving me the system cannot find the file specified.
But if i type mongorestore then it works but it doesn't restore to the atlas cloud server whatever.
What am i doing wrong?
Edit: The path i have used is C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg and dump\gfg but it is still not working.
Usage of MongoRestore.exe can be found here.
Standalone MongoDB
When you use mongorestore, you need to provide the location of the file/dump you need to restore
mongorestore --host=mongodb1.example.net --port=27017 --username=user --authenticationDatabase=admin /opt/backup/mongodump-2011-10-24
last portion of the command is missing that defines the location of the data that needs to be restored.
ReplicaSet
To Restore MongoDB on a Replica, You have to stop the mongod and replace the files for mongodb. Please read here for restoring database on replica set.
For restore from standalone to replica,
mongorestore --host myReplSet/mongo0.example.com:27020,mongo1.example.com:27012 --db <dbname> <folder_location>
If this still does not work, check path and ensure its properly escaped (spaces in name) or quoted.
Your code with quotes and path:
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin "C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg"

MongoDB - dump and restore across different host, db with oplog

Is it possible to take a mongodump and mongorestore it to a different hosts, with different DB names, with oplog enabled?
From: mongodb://user:password#source-hostname:source-port/db1
To: mongodb://user:password#dest-hostname:dest-port/db5
When I do a mongodump with oplog on the source MongoDB, it takes a dump of the entire DB.
mongodump --oplog --host <source-hostname> -u <user> -p <password> --port <source-port> --authenticationDatabase admin
Now for the restore, I want to restore to a different hostname, and the db-name is also different. Is there way to restore the data to this db, with the oplogReplay?
mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay --db <db5> <path-to-dump>/dump
If I use oplogReplay, I am getting the following error
Can only replay oplog on full restore
I do not want to do a full restore, as it will create the db-name as db1, whereas I want to make use of db5. Also, there are already multiple DBs on this destination host and I do not want to bombard with another new database.
Any suggestions on this issue?
You can't use two options --oplogReplay and --db at the same time.
If you don't want to restore the full DB, simply go to dump/ folder and delete files for DBs other than db5. Then retry mongorestore without --db:
mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay <path-to-dump>/dump
If this doesn't not work for you, you may need to import oplog collection to a temporary db, and manipulate it to remove all except records for db5.

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.

Mongodb's "mongodump" command, javascript execution error

Perhaps I have a complete misunderstanding of how mongodump is supposed to work, but I can't seem to get it to do anything besides returning a JavaScript execution failed: SyntaxError: Unexpected identifier error.
Here's what I'm doing:
Mongod is running
I want to backup a database called "mydb"
I'm inside the mongo shell
I tried the command mongodump --db mydb and get the above error
I've tried both mongodump and mongoexport, both have the same issue
What am I doing wrong here?
Try the following it will work
i.Open the terminal
ii. Enter mongodump --collection collectionname --db dbname (Don't go inside mongo shell);
iii.If default port is different(other than 27017) then go for the following command
mongodump --host mongodb1.example.net --port 37017 --username user --password pass --out /opt/backup/mongodump-2011-10-24
mongodump,mongorestore is not commands of mongodb shell. It is separate mongodb utlity. You can find it under mongodb bin folder.
Usually you will need to add all mongodb utilities to the system Path variable and after this easy backup/restore databases from any place in the command line or in the terminal.
Your command looks mongodump --db mydb good if your databases in on default port(27017).
I faced the problem in taking mongo dump and I also wanted to store the dump to S3. Finally I ended up with a bash script to take mongo dump and store it to S3. I used mongodump to take backup.
mongodump -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
Where $MONGO_HOST,$MONGO_PORT and $MONGO_DATABASE are bash variables for host, port and database-name respectively.
You can also use --username user --password pass option for mongodump command if you have username and password setup on the database.
Here is the script to take mongodb dump and store it to S3 with a cron.