How to migrate databases with mongodump and mongorestore

02 June,2020 by Jack Vamvas

For small deployments , the MongoDB utilities mongodump and mongorestore  is an efficient and useful method. 

The steps below outline how to take a backup of the MongoDB databases on server1 and migrate\restore onto server2

The mongodump utility can create a backup for a server, individual databases or collection(s), or a customised query for a collection subset.

In this example - I'm taking a mongodump of all the user databases including admin on MongoDB - server1. I'm using a logon account with sufficient privileges , that has at least backupadmin privileges

Step 1 - 

This command backups all the databases and creates a dump folder in the current directory. Example:/mongodb/dump. If you look in the dump folder you'll see 1 or more directories representing each database from the MongoDB server

> mongodump --username="my_un" --password="mu_pw"

 

If you get a Failed:error dumping users error  - read on workaround - Failed: error dumping users and roles: error writing data for collection with mongodump

 

Step 2 - 

Go to target server and use scp to copy the dump directory. You don't need to use scp - any file transfer protocol is OK 

> scp -r mylogon@server1:/mongodb/dump /mongodb/dump

 

Step 3 - 

Use the mongorestore utility to restore all the databases in the /mongodb/dump on server2

>mongorestore --username="my_un" --password="mu_pw" /mongodb/dump

 

The above steps are a vanilla backup , transfer and restore of all the databases in a MongoDB server. You will need to analyse your particular setup and use switch. Both the mongodump and mongorestore have plenty of options to customise - check the MongoDB documentation for details.

 

Read more on MongoDB

How to migrate MongoDB data to PostgreSQL using mongoexport

How to read MongoDB system log file

 

 


Author: Jack Vamvas (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to migrate databases with mongodump and mongorestore


dba-ninja.com