How to migrate databases with mongodump and mongorestore

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 migraterestore 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.

 

Author: Jack Vamvas

Leave a Reply

Discover more from ciquery

Subscribe now to keep reading and get access to the full archive.

Continue reading