How to export and import a database

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 stays local.

  

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

PS. I know about services like mongoDB Atlas and mLab - this no is what I'm looking for.

    
asked by anonymous 16.05.2017 / 02:30

1 answer

2

Option 1:

mongodump and mongorestore . There are two tools that are in the MongoDB installation bin directory. You could make a script to automate the dump on one side, and restore on the other. There is an article on backup with these tools here . The bank service (mongod) must be active on both machines in this case.

mongodump example:

  • 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 on a MongoDB server running on the local computer, on the default port (27017). Compacts this backup and places the data in the / Backups / MongoDB / data directory. There are several command options, it's worth looking at the documentation. A simple mongodump already executes the backup of a bank 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, unzipping the result (--gzip) in the / Backups / MongoDB / data directory. I'm considering that you used the examples above to generate the backup / dump. If you use the option to backup to 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 backup methods in documentation > from MongoDB.

Do not recommend

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

  • You are converting BSON to JSON, JSON does not have information as rich as the data types.
  • The indexes you created in the collections are not taken in the process. You need to re-create them when you import.
17.05.2017 / 15:49