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.