Skip to content

Commit 55ee820

Browse files
committed
Add Dump And Restore With A Single gzip File as a MongoDB TIL
1 parent 06b0db1 commit 55ee820

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1263 TILs and counting..._
13+
_1264 TILs and counting..._
1414

1515
---
1616

@@ -521,6 +521,7 @@ _1263 TILs and counting..._
521521

522522
- [Determine The Database Version](mongodb/determine-the-database-version.md)
523523
- [Dump A Remote Database](mongodb/dump-a-remote-database.md)
524+
- [Dump And Restore With A Single gzip File](mongodb/dump-and-restore-with-a-single-gzip-file.md)
524525
- [Get Size Stats For A Collection](mongodb/get-size-stats-for-a-collection.md)
525526
- [List Size Stats For All Collections](mongodb/list-size-stats-for-all-collections.md)
526527

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Dump And Restore With A Single gzip File
2+
3+
The `mongodump` and `mongorestore` utilities provide a way for grabbing all the
4+
data from one database and putting it into another database. These commands are
5+
useful for transitioning production data to a database instance with more
6+
computing resources.
7+
8+
The `--archive` and `--gzip` flags, supported by both commands, are what allow
9+
us to do the whole process with a single file. Without flags, `mongodump` will
10+
output multiple `.bson` files.
11+
12+
Here is what the `mongodump` command might look like pointed at a remote URI:
13+
14+
```bash
15+
mongodump \
16+
--uri="mongodb+srv://<USER>:<PASSWORD>@<CLUSTER-HOST-URL>" \
17+
--archive="myapp-dump.20221105.gz" \
18+
--gzip
19+
```
20+
21+
This will take a little while to run based on the size of the database. The
22+
result will be a file in your current directory with the name
23+
`myapp-dump.20221105.gz`. Because it is gzip'd, it will be a few times smaller
24+
than the standing database.
25+
26+
To then load all the data into your new Mongo database cluster, you'll use
27+
`mongorestore` with all the same flags, making sure to swap out the destination
28+
URI details with those of the new instance.
29+
30+
```bash
31+
mongorestore \
32+
--uri="mongodb+srv://<USER>:<PASSWORD>@<NEW-CLUSTER-HOST-URL>" \
33+
--archive="myapp-dump.20221105.gz" \
34+
--gzip
35+
```
36+
37+
For more details, see [Output an Archive
38+
File](https://www.mongodb.com/docs/database-tools/mongodump/#output-to-an-archive-file)
39+
and [Compress the
40+
Output](https://www.mongodb.com/docs/database-tools/mongodump/#compress-the-output).

0 commit comments

Comments
 (0)