How to export and import a database

Asked

Viewed 5,482 times

0

I have mongodb installed on two different computers, where I am studying/developing applications. Usually at the end of the day, I send all the code to a Git server, but the database remains local.

I would like to know how to take the (entire) database from one computer to another, so that I can have access to the same records that I use for testing - and if there is a "better" way to work, what would it be.

PS. I know services like mongoDB Atlas and mLab - that nay is what I seek.

  • 1

    Your idea is to synchronize the banks, automagically leave them with the same data? Or backing up one computer and taking it to the other is enough?

  • @Jorgec.Bernhardtautz without automatic - only backup even! ;)

1 answer

3


Option 1:

mongodump and mongorestore. These are two tools that are in the bin directory of the Mongodb installation. You could write a script to automate the dump on one side, and Restore on the other. There is an article (in English) about the backup with these tools here. Bank service (mongod) needs to be active on both machines in this case.

Example of mongodump:

  • Linux: mongodump -h localhost --gzip -o /Backups/MongoDB/dados
  • Windows: mongodump /h localhost /gzip /o C:\Backups\MongoDB\dados

The above command backs up all databases of a Mongodb server running on the local computer, at the default port (27017). Compact this backup and put the data in the /Backups/Mongodb/data directory. There are several options of the command, worth looking at the documentation. A simple mongodump already runs the backup of a database running on the local machine, placing the files in a "dump" directory within the current directory.

Examples of mongorestore:

  • Linux: mongorestore -h localhost --gzip --dir /Backups/MongoDB/dados
  • Windows: mongorestore /h localhost /gzip /dir C:\Backups\MongoDB\dados

The command restores to a server running on localhost by unzipping the result (-gzip) present in the /Backups/Mongodb/data directory. I am considering that you used the above examples to generate the backup/dump. If you use the option to generate the backup in a file, for example, you should use the --Archive option.

Option 2

Copying data files from one installation to another is also a viable option. It is even one of the recommended methods as backup in documentation mongodb.

I do not recommend

Use the mongoexport and mongoimport because they work with output/input text files. So you lose information:

  • You are converting BSON to JSON, JSON does not have information as rich as data types.
  • The indexes you created in the Collections are not taken in the process. You need to recreate them when you import.
  • Thanks! You can put an example of using the two commands for those who get here already know what to do?

  • 1

    @Danielgomes added the examples and gave an organized to be clear that are two options.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.