16

I'm trying to backup/restore mongodb database to/from .gz files as sample script here

#01 create .gz backup - ok for r3.2.9 and r3.4.10 
mongodump --db ${DB_NAME} --gzip --archive=${BACKUP_FILE_GZ}

#02 restore from .gz file - NOT ok for r3.2.9
mongorestore --gzip --archive=${BACKUP_FILE_GZ} --nsFrom "${DB_NAME}.*" --nsTo "${DB_NAME_RESTORE}.*"

Step 01 i.e. back up is good for both mongodb version r3.2.9 and r3.4.10; though step 02 NOT works for r3.2.9

How can I get mongorestore version r3.2.9 to restore from .gz file and be able to rename the database?

p.s.

We have the solution here but that requires the backup to be a folder; my backup files are huge i.e. 1Gb-2Gb so the extraction is too much time-consuming.

Nam G VU
  • 289
  • 1
  • 3
  • 14

6 Answers6

25

Nothing works for me but this.

mongorestore --gzip --archive=/path/to/file.gz --db db_name
sirajalam049
  • 353
  • 4
  • 7
  • The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} – Siyavash Hamdi Feb 15 '23 at 13:23
20

With 3.2.x you cannot use --nsFrom or --nsTo parameters. This pair of commands should work in all versions:

mongodump --db ${DB_NAME} --gzip -o ${BACKUP_FILE_GZ}

mongorestore --gzip --db "${DB_NAME_RESTORE}" ${BACKUP_FILE_GZ}/${DB_NAME}

Now you get a directory with gzipped files and you can restore all (or just one) collections to a different database.

mustaccio
  • 25,896
  • 22
  • 57
  • 72
JJussi
  • 5,703
  • 1
  • 15
  • 19
3

That is because your mongodump script has an --archive flag.

Then you have to use it when doing mongorestore.

Glorfindel
  • 2,201
  • 5
  • 17
  • 26
  • 2
    Normally, answers here require a bit more than a one-liner. Perhaps you might point to OP to some documentation about this issue or similar? p.s. welcome to the forum! :-) – Vérace Jun 02 '19 at 11:59
2

This worked for me:

mongorestore --gzip --db {DB_NAME} --collection {COLLECTION_NAME} ./{FILENAME}.bson.gz
Andrea Girardi
  • 143
  • 1
  • 7
0

For version 6 You only need to run: (Suppose that your current directory has a backup file named backup_db.gzip and you want to create a database named blog from its content)

 mongorestore --gzip --archive=./backup_db.gzip  blog
Zrelli Majdi
  • 101
  • 1
0

If you want to restore more than one collection at once, use this command below:

for file in *.gzip; do mongorestore --gzip --archive=$file <database_name>; done

Replace the <database_name> with your database name. This works for me.

You can use, also, the --verbose option to see all logs

mustaccio
  • 25,896
  • 22
  • 57
  • 72