Mainly for development purposes I was in need to easily transfer
selected databases from production server to my development
machine. This can be done with
mongorestore commands. The
mongodump by default dumps selected database with
-d parameter to dump directory. Then this folder can
be compressed, transferred to local host, extracted and then
finally restored. This tasks might be quite time consuming and
cumber-stone. After reading MongoDB documentation i turned out
that the MongoDB dump/restore commands can cooperate with each
other allowing simultaneous exporting database from remote host
and loading it to local machine. And it will also compress
transferred data with gzip additionally speeding up operation. In
short, the database can be piped through interwebs!
TLDR; Bash Script
To make task easy, I've made bash script for transferring databases. The username and host for remote machine is hardcoded in script, so you can either change it or modify script to add those as parameters when calling.
#!/bin/bash usage="Usage: sync-db <source-db-name> <target-db-name>" host="example.com" user="peter" if [[ ! "$1" ]]; then echo "Source DB name missing" echo "$usage" exit fi if [[ ! "$2" ]]; then echo "Target DB name missing" echo "$usage" exit fi src=$1 trg=$2 echo "Piping "$src" database to "$trg" through interwebs..." ssh "$user@$host" "mongodump --quiet --archive --gzip -d "$src"" | mongorestore --archive --gzip --drop echo "Done."