- You have completed the setup of a production mLab database.
- You have mongoLab client installed in your computer so that you can run these
Copy from mLab production database to another mLab staging database.
mLab cloud backup vs local backup
The copy of production database to test database is achieved using backup and restore mechanism. mLab provides cloud based backup and restore functionality using the web based admin page. However, each backup cost 50 cents. Alternatively, you can achieve this free of charge by installing your own mongoLab client in your computer and perform backup to your local computer and then restore to another test database in mLab.
Copy mLab database from production to staging using local backup
You will first need to run a mongodump to backup your mLab production database to your local PC. After that you run mongorestore to restore your local PC database backup to another mLab staging database.
MongoDB provides two mechanisms for importing and exporting data. One way is via the mongoimport and mongoexport utilities. These allow you to import and export JSON and CSV representations of your data. The other way is with mongorestore and mongodump utilities which deal with binary dumps.
The commands to dump, delete all tables/data and restore are as follows:
- mongodump -h <production-hostname> -d <production-database-name> -u <user> -p <password> -o <output directory>
- mongo <staging-hostname>/<staging-database-name> -u <user> -p <password> --eval "db.dropDatabase();"
- mongorestore -h <staging-hostname> -d <staging-database-name> -u <user> -p <password> <input db directory>