engineering

Transfer Data Between Remote Servers and MongoDB Instance

Transfer Data Between Remote Servers and MongoDB Instance

This article explains importing remote server data to MongoDB via mongodump and mongorestore command tools.

Ankit Bansal

Ankit Bansal

Aug 30, 2023 3 min read

Introduction:

In the world of software development, the process of importing data from remote servers to a local MongoDB instance plays a crucial role. Developers often require a copy of the production or development server's data on their local machines for testing, debugging, and fixing bugs. This article aims to provide a step-by-step guide on how to efficiently import data from servers to a local MongoDB instance, enabling developers to create a realistic development environment.

Prerequisites:

Step-by-Step Guide:

  1. Taking a Backup/Dump from the Remote Server:

    • If you don't have remote db access on your local machine you can take a dump on the server machine itself.
    • CLI tools
    mongodump --host <server_host> --port <server_port> --username <username> --password <password> --db peerlist --out <backup_folder_path>
    
    • Replace the placeholders with appropriate values. This command will create a backup of the remote server's data in the specified backup folder.
    • If you are getting any admin access error try using this command with additional parameters--authenticationDatabase admin
    mongodump --host <server_host> --port <server_port> --username <username> --password <password> --db <dbName> --out <backup_folder_path>  --authenticationDatabase admin
    
  2. Transfer Backup to Local Machine:

    • If the backup is created on a server machine, transfer it to your local machine. You can use secure file transfer protocols like SCP or SFTP, or cloud storage solutions to achieve this.
  3. Restore Data to Local MongoDB Instance:

    • After transferring the backup to your local machine, you can restore the data into your local MongoDB instance using the mongorestore tool.
    mongorestore --host <localhost> --port <27017> --username <your-username> --db <dbName-ifany> --password <yourpassword-ifany>  <backup_folder_path>/<db_folder> --authenticationDatabase admin
    
    • Replace <database_name> with the name you want for your local database. <backup_folder_path> should point to the directory containing the backup, and <db_folder> is the name of the folder within the backup directory containing the database dump.
  4. Verify Data Import:

    • Connect to your local MongoDB instance using a MongoDB client (DataGrip/Studio3T/Atlas) and verify that the data has been successfully imported. You now have a replica of the remote server's data on your local machine.
    • Or Mongo Shell:
      • Connect to an instance
      mongo --username <your-username-ifany> --<your-password-ifany> --authenticationDatabase admin --host <host|localhost> --port <27017>
      
    • To display the database you are using, type db
    • To use/switch databases use <database>
    • db.getCollection(<Collection-Name>).find()
  5. Single collection import Export

    • Export
      mongoexport --host <localhost> --port <27017> --username <username> --db <dbName> --password <Password> --authenticationDatabase admin --collection <collectionName> --out=<collection-json-file-name>.json --jsonArray
      
    • Import
      mongoimport --host <localhost> --port <27017> --username <username> --db <dbName> --password <Password> --authenticationDatabase admin --collection <collectionName> <collection-json-file.json> --jsonArray
      

Benefits of Local Data Import for Development:

  • Bug Fixing: Having a realistic dataset helps replicate and fix bugs accurately without affecting the production environment.
  • Performance Testing: Developers can test application performance with real data to identify bottlenecks or optimizations needed.
  • Offline Development: No dependency on a live server, enabling development in offline scenarios.
  • Data Privacy: Sensitive data remains on the server, minimizing security risks on local machines.
  • Version Compatibility: Ensures compatibility between server data and local development environment.

Conclusion:

Importing data from remote servers to a local MongoDB instance is a crucial practice for developers aiming to create a robust development and bug-fixing environment. By following the steps outlined in this guide, developers can ensure accurate testing, debugging, and performance optimization, all while maintaining the integrity and privacy of server data. This process enhances the overall development workflow and contributes to the creation of more reliable and efficient applications.

References:

  1. https://www.mongodb.com/docs/database-tools/mongodump/
  2. https://www.mongodb.com/docs/database-tools/mongorestore/
  3. https://www.mongodb.com/docs/database-tools/mongoimport/