0

I'm using mongodb for saving the data for my application and I want to backup of that database in gzip file I searched for it and I found question posted by the other users

link https://stackoverflow.com/questions/24439068/tar-gzip-mongo-dump-like-mysql

link https://stackoverflow.com/questions/52540104/mongodump-failed-bad-option-can-only-dump-a-single-collection-to-stdout

I used these commands but that will not me the expected output I want the command that will create my database gzip compress file and using extraction I will restore that database folder into the mongodb

currently I'm using this below command mongodump --db Database --gzip --archive=pathDatabase.gz

which will create a compression of .gz while I extract it it will show me nothing.

Can you please give me a command that I will use it or any suggestions will appreciated.

puneet55667788
  • 89
  • 2
  • 10

2 Answers2

4

When you use mongodump --db Database --gzip --archive=pathDatabase.gz You will create 1 archive file (it does not create a folder) for the specified DB and compress it with gzip. Resulting file will be pathDatabase.gz in your current directory.

To restore from such file, you'd do this mongorestore --gzip --archive=pathDatabase.gz

This will restore the db "Database" with all its collection. You can check out these MongoDB documentation pages for more info

Dump: https://docs.mongodb.com/manual/reference/program/mongodump/

Restore: https://docs.mongodb.com/manual/reference/program/mongorestore/

Edit: Removed --db flag from restore command as it is not supported when used with --archive.

JPm
  • 98
  • 6
  • this command is not working `mongorestore --db Database --gzip --archive=pathDatabase.gz` why? – puneet55667788 Feb 04 '19 at 13:26
  • the error is `2019-02-04T18:57:34.348+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 ` – puneet55667788 Feb 04 '19 at 13:28
  • You are right, updated the answer. The --db option is not necessary and not allowed for this use case. The archive file already have the db name in it. – JPm Feb 04 '19 at 14:35
  • in case need to authenticate with user name and password `mongodump --host 127.0.0.1 --port 27017 --username userx --password password01 --authenticationDatabase admin --archive=backup.gz --gzip` – rugby2312 Mar 10 '21 at 13:29
2

mongodump --archive=/path/to/archive.gz --gzip will actually create an archive which interleaves the data from all your collections in a single file. Each block of data is then compressed using gzip.

That file can not be read by any other tool than mongorestore, and you need to use identical flags (i.e. mongorestore --archive=/path/to/archive.gz --gzip), which you can use to restore your dump on another deployment.

The resulting archive can not be extracted using gunzip or tar.

If you need to change the target namespace, then you should use the --nsFrom, --nsTo and --nsInclude options in order to use a different database name.

Charles Sarrazin
  • 801
  • 7
  • 13