Mongodb backup and recovery

Time:2021-10-20

More welcome to the blog:https://imjianjian.github.io

Mongodb data backup


Command: mongodump

Parameters:

  • -h:

Server address, for example: 127.0.0.1. Of course, you can also specify port number: 127.0.0.1:27017. If it is not filled in, the default is localhost.

  • -p:

Server port. If – H is used, this parameter can be omitted.

  • -d:

The database instance that needs to be backed up, for example: test. If this parameter is not filled in, all instances will be backed up by default.

  • -c:

Specifies the collection of backed up databases, which can be used with – D.

-Only the a set under the test database instance is backed up: mongodump - D test - C A
  • -f:

For the fields to be exported, use the same method as above.

  • -o:

The backup data storage location, such as C: datadump. Of course, this directory needs to be established in advance. After the backup is completed, the system automatically establishes a test directory under the dump directory, which stores the backup data of the database instance. If this parameter is not filled in, the backup will be saved in the dump directory under the directory where the current backup command is executed by default.

Mongodb data recovery


Command: mongorestore

Parameters:

  • -h

The server address of mongodb. The default is localhost: 27017

  • -d :

The database instance that needs to be restored, such as test. Of course, the name can also be different from that at the time of backup, such as test2

  • –drop:

When restoring, first delete the current data, and then restore the backed up data. In other words, after recovery, the data added and modified after backup will be deleted. Use with caution!

  • –dir:

Specify the directory for the backup