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.
mongodump command script syntax is as follows:
>mongodump -h dbhost -d dbname -o dbdirectory
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
Database instance to be backed up, for example: test
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.
27017 used locally to start your mongod service. Open a command prompt, enter MongoDB installation directory bin directory, enter the command 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:
mongodump command optional parameters are listed below:
|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.
mongorestore command script syntax is as follows:
>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
MongoDB server address where
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
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!
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:
Execute the above command output results are as follows: