From 55ee8207860bfc2eeed6babcdf484aa99be59809 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 5 Nov 2022 15:33:18 -0500 Subject: [PATCH] Add Dump And Restore With A Single gzip File as a MongoDB TIL --- README.md | 3 +- ...ump-and-restore-with-a-single-gzip-file.md | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 mongodb/dump-and-restore-with-a-single-gzip-file.md diff --git a/README.md b/README.md index 8c53022..a832c0a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1263 TILs and counting..._ +_1264 TILs and counting..._ --- @@ -521,6 +521,7 @@ _1263 TILs and counting..._ - [Determine The Database Version](mongodb/determine-the-database-version.md) - [Dump A Remote Database](mongodb/dump-a-remote-database.md) +- [Dump And Restore With A Single gzip File](mongodb/dump-and-restore-with-a-single-gzip-file.md) - [Get Size Stats For A Collection](mongodb/get-size-stats-for-a-collection.md) - [List Size Stats For All Collections](mongodb/list-size-stats-for-all-collections.md) diff --git a/mongodb/dump-and-restore-with-a-single-gzip-file.md b/mongodb/dump-and-restore-with-a-single-gzip-file.md new file mode 100644 index 0000000..6ce6d5e --- /dev/null +++ b/mongodb/dump-and-restore-with-a-single-gzip-file.md @@ -0,0 +1,40 @@ +# Dump And Restore With A Single gzip File + +The `mongodump` and `mongorestore` utilities provide a way for grabbing all the +data from one database and putting it into another database. These commands are +useful for transitioning production data to a database instance with more +computing resources. + +The `--archive` and `--gzip` flags, supported by both commands, are what allow +us to do the whole process with a single file. Without flags, `mongodump` will +output multiple `.bson` files. + +Here is what the `mongodump` command might look like pointed at a remote URI: + +```bash +mongodump \ + --uri="mongodb+srv://:@" \ + --archive="myapp-dump.20221105.gz" \ + --gzip +``` + +This will take a little while to run based on the size of the database. The +result will be a file in your current directory with the name +`myapp-dump.20221105.gz`. Because it is gzip'd, it will be a few times smaller +than the standing database. + +To then load all the data into your new Mongo database cluster, you'll use +`mongorestore` with all the same flags, making sure to swap out the destination +URI details with those of the new instance. + +```bash +mongorestore \ + --uri="mongodb+srv://:@" \ + --archive="myapp-dump.20221105.gz" \ + --gzip +``` + +For more details, see [Output an Archive +File](https://www.mongodb.com/docs/database-tools/mongodump/#output-to-an-archive-file) +and [Compress the +Output](https://www.mongodb.com/docs/database-tools/mongodump/#compress-the-output).