How to Replicate a MongoDB Database From Remote to Local

• 1 min read

Here is a simple script to backup a MongoDB remote database and restore it locally.

#!/bin/bash

HOST="myhost.com"
PORT="1337"
REMOTE_DB="myremote"
LOCAL_DB="mylocal"
USER="giraffe"
PASS="7hIs15MyPa5s"

## DUMP THE REMOTE DB
echo "Dumping '$HOST:$PORT/$REMOTE_DB'..."
mongodump --host $HOST:$PORT --db $REMOTE_DB -u $USER -p $PASS

## RESTORE DUMP DIRECTORY
echo "Restoring to '$LOCAL_DB'..."
mongorestore --db $LOCAL_DB --drop dump/$REMOTE_DB

## REMOVE DUMP FILES
echo "Removing dump files..."
rm -r dump

echo "Done."

And that’s it. Dumping and restoring MongoDB databases really is that easy. If you have any questions leave them in the comments below and I’ll get back to you as soon as possible.

If you enjoyed this tutorial, please consider sponsoring my work on GitHub 🤗

Be the first to cheers
Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit

×

Recommended Posts ✍🏻

See All »
• 3 min read
✨ HTML Share Buttons
Read Post »
• 3 min read
🚅 Next Stop, Yaak
Read Post »
• 4 min read
💻 Wait for User to Stop Typing, in JavaScript
Read Post »