Skip to content

Instantly share code, notes, and snippets.

@dPacc
Created June 29, 2023 07:40
Show Gist options
  • Save dPacc/033b0337cfd293a036d397726f554631 to your computer and use it in GitHub Desktop.
Save dPacc/033b0337cfd293a036d397726f554631 to your computer and use it in GitHub Desktop.
MongoDB dump and restore

Mongodb has two very useful commands for this purpose: mongodump and mongorestore. Here's how to use them:

  1. mongodump: It is a utility for creating a binary export of the contents of a database. Basically, using this command you can export MongoDb database.

    Use the following command to dump your local database:

    mongodump --db <your_database_name> --out <directory_path>
    

    For example:

    mongodump --db myDatabase --out /myData
    

    This command will create a dump of your database named myDatabase and will put it in a directory at /myData/myDatabase/.

  2. Now, you can transfer these dumped files to another system by any means (e.g. scp, rsync, your USB stick, etc).

  3. mongorestore: On the other system, use mongorestore utility to import the data. mongorestore is a tool for restoring a binary backup.

    Use it like so:

    mongorestore --db <new_database_name> <directory_path>
    

    For example:

    mongorestore --db newDatabase /myData/myDatabase/
    

    This command will create (or overwrite) a MongoDB database named newDatabase using the dumped data stored at /myData/myDatabase/.

Note:

  • You may need to start mongodb service or give the host details in the command. The command for starting MongoDB service depends on the OS and installation method. For most of the Ubuntu systems it’s sudo service mongod start.
  • If your database is secured, you might need to add your username and password to these commands with the -u <username> and -p <password> options.

Make sure to replace <your_database_name>, <new_database_name>, and <directory_path> with your actual database names and path.

@dPacc
Copy link
Author

dPacc commented Jan 8, 2024

If your MongoDB database (myDatabase) is hosted in the cloud (for instance, on MongoDB Atlas, AWS, Azure, etc.), you can still use mongodump to export data from it, but you'll need to specify additional connection parameters.

Here is a generalized command structure to perform mongodump from a cloud-hosted MongoDB:

mongodump --uri="mongodb+srv://username:password@yourMongoDBHost/yourDatabaseName" --out /path/to/your/local/directory

Replace the following placeholders with your actual details:

  • username: Your MongoDB username.
  • password: Your MongoDB password.
  • yourMongoDBHost: The address of your MongoDB server or cluster. If you're using MongoDB Atlas, this will be the connection string provided by Atlas.
  • yourDatabaseName: The name of your database.
  • /path/to/your/local/directory: The local directory where you want to store the backup.

For example, if your MongoDB is hosted on Atlas and your database name is myDatabase, your command might look something like this:

mongodump --uri="mongodb+srv://myUser:myPassword@mycluster.mongodb.net/myDatabase" --out ~/Desktop/myDatabaseBackup

In this example:

  • Replace myUser with your MongoDB username.
  • Replace myPassword with your MongoDB password.
  • Replace mycluster.mongodb.net with your Atlas cluster's address.
  • The data will be dumped to a folder named myDatabaseBackup on your desktop.

Some important notes:

  • Ensure that the user myUser has the necessary permissions to perform a dump on myDatabase.
  • Your MongoDB Atlas cluster's network security settings must allow connections from your IP address.
  • The mongodump tool must be installed on your local machine, and you should have the necessary permissions to write to the specified local directory.
  • If your database requires TLS/SSL, you might need to add additional options like --ssl to the command. MongoDB Atlas usually requires this.

Remember to handle sensitive information like your username and password securely and avoid exposing them in shared scripts or command histories.

@dPacc
Copy link
Author

dPacc commented Jan 8, 2024

Great to hear that mongodump worked for you! To use the data you've dumped from your cloud database on your local MongoDB instance, you will use mongorestore. This tool is designed to restore a binary backup created by mongodump.

Here's a basic step-by-step guide on how to do it:

  1. Ensure MongoDB is Running on Your Local Machine:

    • Before you proceed, make sure that MongoDB is installed and running on your local machine. You can check if MongoDB is running by using commands like mongo --version to check the installation and version or systemctl status mongod (on Linux) to check if the MongoDB service is active.
  2. Using mongorestore:

    • The mongorestore utility restores data from the dump files created by mongodump. You will point it to the directory where you stored your dump.
    • The basic syntax is as follows:
      mongorestore /path/to/your/dump/directory
    • Replace /path/to/your/dump/directory with the path to the directory where you stored the backup from mongodump.
  3. Example:

    • For instance, if you dumped your database to ~/Desktop/myDatabaseBackup, you would use:
      mongorestore ~/Desktop/myDatabaseBackup
  4. Restoring Specific Database or Collection (Optional):

    • If you only want to restore a specific database or collection, you can provide additional arguments to mongorestore.
    • To restore a specific database:
      mongorestore --db yourLocalDatabaseName /path/to/your/dump/directory/yourDatabaseName
    • Replace yourLocalDatabaseName with the name you want for the database on your local MongoDB instance.
  5. Handling Errors or Issues:

    • If you encounter any errors during the restore process, they will typically be displayed in the command line. Common issues include permission problems or conflicts with existing data.
  6. Verification:

    • After the restore process is complete, you can verify the data by connecting to your local MongoDB instance (using the mongo shell or a GUI tool like MongoDB Compass) and checking the contents of the restored database.

Remember, mongorestore will add the data to your local MongoDB instance without overwriting the entire database, but it will overwrite existing collections if they have the same name as those in the backup. If you want to avoid this, consider restoring to a new database or removing the existing collections first.

@dPacc
Copy link
Author

dPacc commented Mar 10, 2024

LOCAL to CLOUD

To dump your local copilotly-test MongoDB database and restore it to your cloud MongoDB instance hosted at the given URI, you'll follow a two-step process. First, you'll use mongodump to export your local database, and then use mongorestore to import that data into your cloud MongoDB.

Step 1: Dump your local MongoDB database

You'll need to run mongodump to export the copilotly-test database from your local MongoDB instance. If your MongoDB server is running with default settings (i.e., on localhost with the default port 27017), you may not need to specify a URI for the local dump command. Here's how you do it:

mongodump --db copilotly-test --out /path/to/your/local/directory

Replace /path/to/your/local/directory with the actual path where you want to store the dump files.

Step 2: Restore the dump to your cloud MongoDB

After dumping your local database, the next step is to use mongorestore to import the data into your cloud MongoDB. You'll need to use the --uri flag to specify the connection string for your cloud MongoDB instance. Here's how you can do it based on the URI you've provided:

mongorestore --uri="mongodb+srv://admin:admin@cluster0.bhmosab.mongodb.net/testdb?retryWrites=true&w=majority" /path/to/your/local/directory/copilotly-test

Remember to replace /path/to/your/local/directory/copilotly-test with the actual path to the directory containing your local database dump. Also, ensure that your mongorestore command points to the exact folder that contains the dump of the copilotly-test database.

Additional Notes

  • Ensure that the MongoDB versions between your local instance and the cloud service are compatible to avoid any potential issues during the restore process.
  • If your local or cloud MongoDB instances require additional connection parameters (like SSL, specific ports, or authentication databases other than the default), make sure to include those in your commands.
  • If you encounter any permission issues or errors related to accessing the cloud database, double-check your URI for accuracy, including the username and password.

By following these steps, you should be able to successfully dump your local copilotly-test database and restore it to your cloud MongoDB instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment