MongoDb: How to import dump data from .gz file?
Asked Answered
C

4

36

I want to import dump data from my .gz file.

Location of file is home/Alex/Documents/Abc/dump.gz and the name of db is "Alex".

I have tried mongorestore --gzip --db "Alex" /home/Alex/Documents/Abc/dump.gz

But it shows error:

 2018-10-31T12:54:58.359+0530   the --db and --collection args should 
 only be used when restoring from a BSON file. Other uses are 
 deprecated and will not exist in the future; use --nsInclude instead
 2018-10-31T12:54:58.359+0530   Failed: file 
 /home/Alex/Documents/Abc/dump.gz does not have .bson extension.

How can I import it?

Confrere answered 31/10, 2018 at 7:43 Comment(0)
B
81

Dump command:

mongodump --host localhost:27017 --gzip --db Alex --out ./testSO

Restore Command:

mongorestore --host localhost:27017 --gzip --db Alex ./testSO/Alex

Works perfectly!


While using archive:

Dump command:

mongodump --host localhost:27017 --archive=dump.gz --gzip --db Alex

Restore Command:

mongorestore --host localhost:27017 --gzip --archive=dump.gz --db Alex

Note:- While using archive you need to stick with the database name.

Different database name or collection name is not supported. For more info.

Buck answered 31/10, 2018 at 8:44 Comment(6)
"the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future;"Infringement
I have json and bson files from dump. --gzip without --db failed for me.Historiographer
mongorestore --host localhost:27017 --gzip --archive=dump.gz --db Alex this command does not work for me. I am getting an error same as @SantiagoBendavid.Nowhere
Just in case you're asking (like myself) 27017 is the default MongoDB port: docs.mongodb.com/manual/reference/default-mongodb-port/…Rosalbarosalee
mongorestore --gzip --archive=/path/to/file.gz --db works for meKaohsiung
different db names are supported with nsTo and nsFrom parameter fields of mongorestore utilityRashid
U
3

This is what worked for me in the latest versions (100.5.1) of mongodump.

mongorestore --uri=<CONNECTION_URI> --gzip --archive=<ARCHIVE_NAME> --nsFrom "<SOURCE_DB_NAME>.*" --nsTo "<DEST_DB_NAME>.*"
Uncommunicative answered 8/11, 2021 at 11:28 Comment(0)
S
1

Unpack .tgz files and restore the DB

tar zxvf fileNameHere.tgz

mongorestore --port 27017 -u="username" -p="password" --authenticationDatabase admin /bacup_path

Synergetic answered 25/2, 2022 at 10:10 Comment(0)
G
0

mongorestore doesn't find the BSON files inside the gzip file because the mongodump was made with different paths than the restore one.

To solve the problem, the fastest and safest way is to extract the gzip file and go to the upper folder containing the json and bson files for run the mongorestore.

For example, the dump.gz file was made in such a way that the backup are saved within the data/backup/mongo/dump/ path folders

Extracting the dump.gz file with command tar -xvf dump.gz you will find a folder named data with the subfolders data/backup/mongo/dump/ inside (inside the dump folder are present all backup file with json and bson extension, these files represent databases and collections, etc.)

Go to the higher folder, that containing the dump folder eg. cd data/backup/mongo/

Now you can run the restore command

mongorestore --authenticationDatabase admin dump/

Where dump/ is the folder that containing the backup files.

You may need to use the arguments -h to point the server host (eg. localhost) and -u followed by the username enabled to make the restore operations (eg. root)

Giliana answered 12/7, 2022 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.