How to Migrate or Copy MongoDB documents to another Server
Sunday, January 6, 2019 Posted in Systems-Administration-and-DevOpsTags : mongodb database linux
I recently installed Kubuntu 18.04 on my desktop PC, after doing a wipe-and-reinstall of my Windows 10 installation on the SSD.Altough most of my code is backed up either locally or on Github my MongoDB documents are not, because i never found it necessary.
The current version of my chat app needs some filler data so as to test certain functionality, and so i decided to migrate MongoDB documents from my
laptop to my Kubuntu install on the desktop, this will be accomplished through native MongoDB utilities for backup and restore, and Secure Copy or scp
to copy files from Laptop to Desktop over the network through SSH
Setup
- The Receiver PC running
openssh-server, installable on Ubuntu/Debian based systems bysudo apt install openssh-server - The Sender PC having
sshinstalled, this should be default on most installations of Ubuntu - Both sender and receiver should have MongoDB installed, for obvious reasons :P
- Both sender and receiver should have
ifconfigtool fromnet-toolspackage on Ubuntu or Debian for finding out the IP address of both systemssudo apt install net-tools
Migrations
- On your sender PC and receiver PC, make a directory called
mongobackupsin your home folder or any other locationmkdir ~/mongobackups - Again on the sender PC, backup your mongoDB documents to the
mongobackupsfolder,yourdbhere is just the name of your MongoDB collectionsudo mongodump --db yourdb --out ~/mongobackups/`date +"%d-%m-%y"`This
date +"%d-%m-%y"creates a folder with the current date stamp, inside this folder there will be.bsonfiles containing your documents -
Make sure
openssh-serveris installed on the receiver PC,and runifconfigto find the IP address of your receiver PC, in my case it is 192.168.2.3 - On the Sender PC
scpthemongobackupsfolder to themongobackupson the receiver PC like so.Replaceshawnwith your username of your receiver PCscp -r ~/mongobackups/ shawn@192.168.2.3:~/mongobackupsYou will be prompted for your password.
- On the Receiver pc now your restore your backups using
mongorestore,06-01-19andyourdbare just placeholders,enter your own datestamp folder and collection name insteadsudo mongorestore --db yourdb --drop ~/mongobackups/06-01-19/yourdb/
You should now be able to use your MongoDB documents in your webapp now.