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