MongoDB Data Backup
In MongoDB we use the Mongodump command to back up MongoDB data. The command can export all data to the specified directory.
The Mongodump command allows you to specify the server to which the exported data can be dumped by parameter.
Grammar
The mongodump command script syntax is as follows:
>mongodump-h dbhost-d Dbname-o dbdirectory
-H:
MONGDB the server address, for example: 127.0.0.1, you can also specify the port number: 127.0.0.1:27017
-D:
The database instance that needs to be backed up, for example: test
-O:
Backup data storage location, for example: C:\data\dump, of course, the directory needs to be established in advance, after the backup is completed, the system automatically establishes a test directory under the Dump directory, which holds the backup data of the database instance.
Instance
Create a library and insert content that verifies the backup effect
> Use MyDB
Switched to DB MyDB
> db.createcollection (' db ')//Create a collection
{"OK": 1}
> Show collections//View Collection
Db
MyDB
Backup as Below
[[email protected] ~]# mongodump-h 127.0.0.1-d mydb-o/mnt///Backup MyDB library to/mnt/
As shown below, the visible data has been backed up to/mnt/
The list of optional parameters for the Mongodump command is as follows:
[[email protected] ~]# mongodump--collection db-d mydb//Backup DB collection in MyDB Library
Grammar |
Description |
Example |
Mongodump--host host_name--port port_number |
This command will back up all MONGODB data |
Mongodump--host runoob.com--port 27017 |
Mongodump--dbpath Db_path--out backup_directory |
|
Mongodump--dbpath/data/db/--out/data/backup/ |
Mongodump--collection Collection--db db_name |
The command backs up a collection of the specified databases. |
Mongodump--collection mycol--db test |
MongoDB Data Recovery
MongoDB uses the Mongorestore command to restore the backed up data.
Grammar
The mongorestore command script syntax is as follows:
>mongorestore-h
--host <:p Ort>,-h <:p Ort>:
MongoDB server address, default is: localhost:27017
--db,-D:
A database instance that needs to be recovered, for example: test, and of course the name can be different from the backup time, such as Test2
--drop:
When recovering, delete the current data and then restore the backed up data. That is, after the recovery, add the modified data after the backup will be deleted, use with caution Oh!
<path>:
Mongorestore the last parameter, set the location of the backup data, for example: C:\data\dump\test.
You cannot specify both the <path> and--dir options,--dir can also set up a backup directory.
--dir:
Specify the directory to be backed up
You cannot specify both the <path> and--dir options.
Instance
[[email protected] ~]# mongorestore-h 127.0.0.1:27017-d mydbb/mnt/mydb Restore the contents of the/mnt/mydb path to the database MYDBB
Enter MONGO to view
> Use MYDBB
Switched to DB MYDBB
> Show Collections
Db
MyDB
You can see that the data has been recovered in MYDBB.
MongoDB Backup and Recovery