How to restore dump backup in mongodb - mongodb

I have restore-63bbdc996664dc2524cce24d.tar file, I am restoring it in the MongoDB database
tried these commands
mongodump --db test --gzip --archive=restore-63bbdc996664dc2524cce24d.tar
mongodump --db test --archive=restore-63bbdc996664dc2524cce24d.tar
mongodump --host localhost --port 27017 --db test --gzip --archive=restore-63bbdc996664dc2524cce24d.tar
mongorestore --drop C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz -v
error => Failed: file C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz does not have
.bson extension
I also tried to convert this file to bson by the command
bsondump --outFile=C:\Users\rustam\restore-63bbdc996664dc2524cce24d.tar.gz coll.bson
Can anyone please tell me what should I do to restore the database?
Thanks in advance for any comment or answer.

Extract the archive file using tar -xvf restore-63bbdc996664dc2524cce24d.tar
Use mongorestore --db test --drop <path/to/extracted/bson/files> to restore the dump.
Keep in mind that this command will drop any existing collections in that database before restoring, so make sure you have taken necessary backups.

After spending a lot of time on it finally I got the solution
1. Extract the tar file
2. Run the below command
mongod --port your_port --dbpath your_extracted_tar_folder
note: port is optional, it will run 27027 default port
3. Go and connect on the same port that you pass

Related

Export the whole database on mongodb

Is there a way to export the whole database in mongodb instead of exporting a collection this way?
{"_id":{"$oid":"5d3de201b128f8eccc1979a5"},"user":"myuser","password":"$2y$10$euzVCeHJ4XAT0xQuQzUotenktVGCQ5darCSWWQtfYE80IqLovDNfi","widgets":[{"name":"w1","color":"blue"},{"name":"w2","color":"green"}]}
mongodump is a way to do this.
See the documentation
yes please use mongodump
Some parameters are as follow
--db <database>, -d <database>
If you do not specify a database, mongodump copies all databases in this instance into the dump files.
--collection <collection>, -c <collection>
If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.
so with your condition you can do it like this
mongodump --host mongodb1.example.net --port 37017 --username user --password "pass" --db yourdatabasename --out /opt/backup/mongodumpdir
the default host port is localhost & 27017 .
If you haven't changed the default, you can ignore this

Mongodump doesn't work as expected

I am trying to host my database online and based on what I have found online, I need to use mongodump to export my database first.
The way to use mongodump if am not wrong is:
mongodump -d <db-name> -o <directory>
when I use the above command, I get the following error in the terminal:
Failed: error connecting to db server: no reachable servers
I tried to add --host=127.0.0.1 after mongodump as follows:
mongodump --host=127.0.0.1 -d <db-name> -o <directory>
But I still get the same result. What am I doing wrong here?
Update:
I managed to overcome the error by starting the MongoDB service with the following command:
brew services start mongodb
Now when I run mongodump, it seems to be working but I can't find it when I navigate to the directory where it supposed to be located!
NOTE: I am using Meteor technology, and I am accessing my database with meteor mongo command
If you are doing mongodump from a remote server it can happen that versions are incompatible. This results in no documents being dump without any warnings. (At least from my tests, mongodump 2.6.10 won't be able to dump from mongod 3.2.13)
Also, make sure bash special characters are not breaking up your query.
Example:
mongodump --db DB_name --collection colname --query "{$or: [something1, something]}"
The previous query won't work as you need to escape the $ with \.
mongodump --db DB_name --collection colname --query "{\$or: [something1, something]}"
You could try:
Run mongodump --db <database> --port 3001 from the directory you want the output files to be created (it will create a dump dir with the files)
The port is 3001, as it seems meteor doesn't use Mongodb default port
Also, if nothing appears, try running with the -v flag for verbose mode, this will help you to find out why your files are not being created. Also, be sure yout database name is correct.

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}

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.

How to use mongodump for 1 collection

How can I use mongodump to move a single collection from one database to another?
How should I use the command and its options?
I think it's just:
mongodump --db=<old_db_name> --collection=<collection_name> --out=data/
mongorestore --db=<new_db_name> --collection=<collection_name> data/<db_name>/<collection_name>.bson
Also see docs here and here.
Btw, the other way to move the collection from one database to another is to use renameCollection:
db.runCommand({renameCollection:"<old_db_name>.<collection_name>",to:"<new_db_name>.<collection_name>"})
Here's some related SO threads:
How to copy a collection from one database to another in MongoDB
How to use the dumped data by mongodump?
Taking database (document) dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
Taking collection dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --collection <collection-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
mongodump documentation
Very basic commands for dump mongodb.
Dump all collection
mongodump
Dump specific database only
mongodump --db=DB_NAME
Dump database with username & password
mongodump -u=USERNAME -p=PASSWORD --db=DB_NAME
Dump from another host
mongodump --host HOST_NAME/HOST_IP --port HOST_PORT --out {YOUR_DIRECTOTY_PATH} --db=DB_NAME
Only able to dump from another host when they allow it.
If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri
mongodump --uri "mongodb://user:password#mongo-en-1.example.io:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin" --collection $collectionname
Then restore it the usual way.
Here is an example of how you can export a single collection with mongodump.exe on Windows 10:
"D:\Program Files\MongoDB\Server\4.0\bin\mongodump.exe" -h localhost --port 27017 -d meteor --collection users -o meteor_users
The exported collection is users, the database is meteor, the host localhost, the port is 27017.
The output will be stored in directory meteor_users.
Restoring should use a command like this one:
"D:\Program Files\MongoDB\Server\4.0\bin\mongorestore.exe" -d meteor -c users users.bson
None of them works for me while doing dump for MongoDB atlas. Here is the minor change in the host that work for me
Dump
mongodump --uri mongodb+srv://<USERNAME>:<PASSWORD>#host.abcd.mongodb.net/db_name --collection "user_collection" --gzip --out db_backup_folder
Restore
mongorestore --uri mongodb+srv://<USERNAME>:<PASSWORD>#dbhost.abcd.mongodb.net -d db_name --gzip db_backup_folder
atlas-database-tools-backup-restore