Table of Contents
In this article, we will see how to install mongodump and mongorestore on Mac. If you are looking to safeguard your MongoDB databases from unforeseen events like corruption, accidental deletion, system failure or from any other such disasters then it is always recommended to take regular backups so that it can later be restored to avoid any critical or production database losses. This task can be easily achieved by using two open source tools - mongodump
and mongorestore
. As understood from its name, mongodump is used for taking mongodb backups and mongorestore is used to restore data from that dump into database.
Now you may ask why mongodump and mongorestore? It is because of several good reasons. With mongodump, you don't necessarily have to take backup of entire databases, you can only take backup of a specific database or specific collections depending on your requirements. This helps save lot of space and reduces the overhead of taking complete backup everytime. Not only that, with mongodump you can actually take backup while database is running. You don't have to shut it down or bring it down. Another great benefit is that since mongodump exports data in BSON format, it can be easily restored in any version of MongoDB.
Similarly, mongorestore can restore selective backups(such as specific database or collection) from dump created by mongodump tool. mongorestore is very frequently used for restoring production database dump into lower environments such as UAT or DEV environment, allowing developers and testers to work with latest data available. Now that it is all understood, lets dive into the steps to install and use these tools on macOS System.
How to Install mongodump and mongorestore on macOS
Also Read: How to Fix "MacBook screen flickering issue" [Easy Solutions]
Step 1: Prerequisites
a) You should have a running macOS
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have Homebrew
installed in your System.
d) Minimum hardware requirements:-
- CPU: Latest model dual-core processor (Intel or Apple Silicon) would be enough.
- RAM: Minimum of 2 GB but 4 GB or more is recommended for handling larger databases.
- Storage: It would depend upon the size of data that needs to be backed up.
Step 2: Update Your Server
It is always a good practice to keep your formulae updated by running brew upgrade
command. This ensures that latest security patches and bugfixes are installed in system which keeps system secure and stable.
cyberithub@macos1066 % brew upgrade
Step 3: Download MongoDB Homebrew Formulae
You have to visit official website and download MongoDB Homebrew formulae for MongoDB and the Database Tools using brew tap mongodb/brew
command as shown below.
cyberithub@macos1066 % brew tap mongodb/brew
==> Tapping mongodb/brew
Cloning into '/usr/local/Homebrew/Library/Taps/mongodb/homebrew-brew'...
remote: Enumerating objects: 1444, done.
remote: Counting objects: 100% (493/493), done.
remote: Compressing objects: 100% (133/133), done.
remote: Total 1444 (delta 415), reused 382 (delta 360), pack-reused 951 (from 1)
Receiving objects: 100% (1444/1444), 313.94 KiB | 555.00 KiB/s, done.
Resolving deltas: 100% (853/853), done.
Tapped 17 formulae (35 files, 401.4KB).
Step 4: Install MongoDB Database Tools
To install mongodb-database-tools on your system, run brew install mongodb-database-tools
command as shown below. This will install MongoDB utilities like mongodump, mongorestore, mongoexport, mongoimport, bsondump and mongostat.
cyberithub@macos1066 % brew install mongodb-database-tools
==> Fetching mongodb/brew/mongodb-database-tools
==> Downloading https://fastdl.mongodb.org/tools/db/mongodb-database-tools-macos-x86_64-100.9.5.zip
########################################################################################################################################## 100.0%
==> Installing mongodb-database-tools from mongodb/brew
/usr/local/Cellar/mongodb-database-tools/100.9.5: 15 files, 120.9MB, built in 9 seconds
==> Running `brew cleanup mongodb-database-tools`...
Step 5: Check mongodump version
Now that mongodb-database-tools is installed, you can check the availability of installed version of mongodump utility by running mongodump --version
command as shown below.
cyberithub@macos1066 % mongodump --version mongodump version: 100.9.5 git version: 90481484c1783826fe26ca18bbdcd30e933f3b88 Go version: go1.21.11 os: darwin arch: amd64 compiler: gc
Step 6: Check mongorestore version
Similarly, you can also check the installed version of mongorestore using mongorestore --version
command as shown below.
cyberithub@macos1066 % mongorestore --version mongodump version: 100.9.5 git version: 90481484c1783826fe26ca18bbdcd30e933f3b88 Go version: go1.21.11 os: darwin arch: amd64 compiler: gc
Step 7: Create Database dump
Now that mongodump tool is installed, lets run through an example to take backup of a collection from MongoDB. In my case, I am having a collection called example
in cyberithub-db
database hosted on MongoDB server cyberithub-db-server
, listening on Port 27017
. To take backup of this collection in local system, I am using below mongodump command. This command will create a backup in cyberithub-db-backup
directory.
cyberithub@macos1066 % mongodump --ssl --host="cyberithub-db-server:27017" --collection=example --db=cyberithub-db --out=cyberithub-db-backup --username=cyberithub --password=pass123 --sslCAFile bundle.pem --tlsInsecure
2024-09-26T22:09:21.837+0230 writing cyberithub-db.example to cyberithub-db-backup/cyberithub-db/example.bson
2024-09-26T22:09:23.253+0230 [...........................] cyberithub-db.example 0/26 (0.0%)
2024-09-26T22:09:24.148+0230 [#####......................] cyberithub-db.example 5/26 (19.2%)
2024-09-26T22:09:24.762+0230 [##########.................] cyberithub-db.example 10/26 (38.4%)
2024-09-26T22:09:25.120+0230 [##############.............] cyberithub-db.example 13/26 (50.0%)
2024-09-26T22:09:26.367+0230 [##################.........] cyberithub-db.example 18/26 (69.2%)
2024-09-26T22:09:27.245+0230 [#######################....] cyberithub-db.example 22/26 (84.6%)
2024-09-26T22:09:27.210+0230 [###########################] cyberithub-db.example 26/26 (100.0%)
Below are the explanation of above command:-
- --ssl: this option is used to enable SSL connections during backup
- --host: this option is used to provide the hostname of database server
- --collection: this option is used to provide the collection name which needs to be backed up
- --out: to provide directory name to store backup
- --username: to provide user name to connect to database
- --password: to provide password of user name to authenticate to database server
- --sslCAFile: to provide CA file for SSL
- --tlsInsecure: to skip certificate validation
Step 8: Restore from dump
Once dump is successfully taken, you can easily restore this dump into any of the MongoDB database server you need. So to restore dump in a MongoDB Server, use below mongorestore command.
cyberithub@macos1066 % mongorestore --ssl --host="cyberithub-restore-server:27017" --collection=example --db=cyberithub-db --username=cyberithub --password=pass123 --sslCAFile bundle.pem --tlsInsecure --drop cyberithub-db-backup/cyberithub-db/example.bson
2024-09-26T23:08:21.837+0230 checking for collection data in cyberithub-db-backup/cyberithub-db/example.bson
2024-09-26T23:08:30.671+0230 reading metadata for cyberithub-db.example from cyberithub-db-backup/cyberithub-db/example.bson
2024-09-26T23:08:43.201+0230 dropping collection cyberithub-db.example before restoring
2024-09-26T23:08:57.148+0230 restoring cyberithub-db.example from cyberithub-db-backup/cyberithub-db/example.bson
2024-09-26T23:09:13.456+0230 [#######....................] cyberithub-db.example 2MB/10MB (20.0%)
2024-09-26T23:09:25.389+0230 [#################..........] cyberithub-db.example 6MB/10MB (60.0%)
2024-09-26T23:09:46.578+0230 [###########################] cyberithub-db.example 10MB/10MB (100.0%)
2024-09-26T23:10:45.276+0230 finished restoring cyberithub-db.example (26 documents, 0 failures)
2024-09-26T23:10:48.219+0230 restoring indexes for collection cyberithub-db.example from metadata
.........................................................
Below are the explanation of above command:-
- --ssl: this option is used to enable SSL connections during backup
- --host: this option is used to provide the hostname of database server where backup needs to be restored
- --collection: this option is used to provide the collection name which needs to be restored
- --db: to provide database name
- --username: to provide user name to connect to database
- --password: to provide password of user name to authenticate to database server
- --sslCAFile: to provide CA file for SSL
- --tlsInsecure: to skip certificate validation
- --drop: to drop current collection before restoring
- cyberithub-db-backup/cyberithub-db/example.bson: It is path to .bson backup file which is to be restored on database server.