MongoDB 🌱 - Try to backup and restore the database with --gzip option


1 min read

Table of contents

When you backup the database, you should choose collection output with Gzip, not .bson or .json for efficient storage.

Here is an example using abc DB at localhost with output directory is dump/local_abc_2024_05_05_13_19:

mongodump --uri="mongodb://localhost:27017/abc" --db abc -o dump/local_abc_2024_05_05_13_19 --gzip

You can use mongodump --help for more information:

Export the content of a running server into .bson files.

Specify a database with -d and a collection with -c to only dump that database or collection.

Connection strings must begin with mongodb:// or mongodb+srv://.

See for more information.

namespace options:
  -d, --db=<database-name>                                  database to use
  -c, --collection=<collection-name>                        collection to use

uri options:
      --uri=mongodb-uri                                     mongodb uri connection string

output options:
  -o, --out=<directory-path>                                output directory, or '-' for stdout (default: 'dump')
      --gzip                                                compress archive or collection output with Gzip

Expected output is the following text:

2024-05-05T11:22:33.802+0700    done dumping <database-name>.<collection-name>

When running mongorestore, you should specify the option --gzip to restore from compressed files or data stream created by mongodump --gzip.

mongorestore --uri="mongodb://localhost:27017/abc_2024_05_05_13_19" --db abc_2024_05_05_13_19 dump/abc_2024_05_05_13_19/abc  --gzip