Latest web development tutorials

MongoDB backup (mongodump) and recovery (mongorestore)

MongoDB data backup

In Mongodb we use MongoDB mongodump command to back up data. This command can export all data to the specified directory.

mongodump command can export the data specified by the parameters of the order of the server dump.

grammar

mongodump command script syntax is as follows:

>mongodump -h dbhost -d dbname -o dbdirectory
  • -h:

    MongDB where the server address, for example: 127.0.0.1, of course, you can also specify the port number: 127.0.0.1: 27017

  • -d:

    Database instance to be backed up, for example: test

  • -o:

    Data backup storage location, for example: c: \ data \ dump, of course, the directory needs to establish in advance, after the backup is complete, the system automatically creates a test directory under the dump directory, the directory backup data stored inside the database instance.

Examples

27017 used locally to start your mongod service. Open a command prompt, enter MongoDB installation directory bin directory, enter the command mongodump:

>mongodump

After executing the above command, the client will connect to the ip is 127.0.0.1 and port number 27017 on the MongoDB service and back up all data to bin / dump / directory. Command output results are as follows:

MongoDB data backup

mongodump command optional parameters are listed below:

grammar description Examples
mongodump --host HOST_NAME --port PORT_NUMBER This command will back up all data MongoDB mongodump --host w3cschool.cc --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY mongodump --dbpath / data / db / --out / data / backup /
mongodump --collection COLLECTION --db DB_NAME This command will set the specified database backup. mongodump --collection mycol --db test

MongoDB Data Recovery

mongodb use mongorestore command to restore the backup data.

grammar

mongorestore command script syntax is as follows:

>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
  • -h:

    MongoDB server address where

  • -d:

    Need to restore the database instance, such as: test, of course, the name can also be backed up and the time is not the same, such as test2

  • --directoryperdb:

    Backup location data, for example: c: \ data \ dump \ test, why should there be more a test, rather than the backup time of dump, the reader see the hint of it!

  • --drop:

    Recovery time, first delete the current data, and then restore the data backed up. That is, after the resumption, after adding backup modified data will be deleted, caution Oh!

Next we execute the following command:

>mongorestore

Execute the above command output results are as follows:

MongoDB Data Recovery