MongoDB data backup and restore is mainly divided into two kinds, one is for the library of Mongodump and Mongorestore, one is for the table in the library mongoexport and Mongoimport.
One, Mongodump backup database
1, Common command grid
Mongodump-h IP--port Port-u user name-p password-d database-o file exists path
If there are no users, you can remove-u and-P.
If you export the native database, you can remove-H.
If it is the default port, you can remove--port.
If you want to export all databases, you can remove-D.
2. Export all databases
[Email protected] mongodb]# mongodump-h 127.0.0.1-o/home/zhangy/mongodb/127.0.0.13 06:15:55.448 3 06:15:55.449 database:test to /home/zhangy/mongodb/3 06:15:55.449 Test.system.indexes to/home/zhangy/mongodb/test/3 06:15:55.450 13 06:15:55.450 Test.posts to/home/zhangy/mongodb/test/3 06:15:55.480
3, export the specified database
[Email protected] mongodb]# mongodump-h 192.168.1.108-d tank-o/home/zhangy/mongodb/connected to:192.168.1.108Tue Dec3 06:11:41.618 Database:tank to/home/zhangy/mongodb/Tanktue Dec3 06:11:41.623 tank.system.indexes to/home/zhangy/mongodb/tank/System.indexes.bsonTue Dec3 06:11:41.623 2Objectstue Dec3 06:11:41.623 tank.contact to/home/zhangy/mongodb/tank/Contact.bsontue Dec3 06:11:41.669 2Objectstue Dec3 06:11:41.670 Metadata forTank.contact to/home/zhangy/mongodb/tank/Contact.metadata.jsonTue Dec3 06:11:41.670 tank.users to/home/zhangy/mongodb/tank/Users.bsontue Dec3 06:11:41.685 2Objectstue Dec3 06:11:41.685 Metadata forTank.users To/home/zhangy/mongodb/tank/users.metadata.json
Third, Mongorestore restore the database
1, Common command format
Mongorestore-h IP--port Port-u user name-p password-d database--drop file exists path
--drop means to delete all records first and then restore them.
2. Restore all databases to MongoDB
[Email protected] mongodb]# mongorestore/home/zhangy/mongodb/ #这里的路径是所有库的备份路径
3. Restore the specified database
[Email protected] mongodb]# mongorestore-d tank/home/zhangy/mongodb/tank/ #tank这个数据库的备份路径 -D tank _new/home/zhangy/mongodb/tank/ #将tank还有tank_new数据库中
These two commands can be implemented to backup and restore the database, the file format is JSON and Bson. Unable to refer to a table backup or restore.
Four, Mongoexport export the table, or some fields in the table
1, Common command format
The above parameters are good to understand, focus on:
-F export refers to field, split by font size,-f name,email,age export name,email,age three fields
-Q can export the root query condition,-Q ' {"UID": "100"} ' exports the data with UID 100
--csv means that the exported file format is CSV, which is useful because most of the relational databases are supported by CSV, and there's something in common here.
2, export the whole sheet
[Email protected] mongodb]# mongoexport-d tank-c users-o/home/zhangy/mongodb/tank/127.0.0.1 4 Records
3. Export some fields from the table
[Email protected] mongodb]# mongoexport-d tank-c users--csv-f uid,name,sex-o tank/127.0.0.14 Recor Ds
4, according to the conditions to dare to out data
[[email protected] mongodb]# mongoexport-d tank-c users-q ' {uid:{$gt: 1}} '-Otank/127.0.0.1 3 Records
Five, Mongoimport import table, or partial field in table
1, Common command format
1.1. Restore a non-CSV file exported from the whole table
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name
Focus on--upsert, the other arguments above have been mentioned,--upsert insert or update existing data
1.2. Restore the export file for some fields
Mongoimport-h IP--port Port-u user name-p password-D database-C tablename--upsertfields field---upsertfields root--upsert
1.3. Restore the exported CSV file
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type--headerline--upsert--drop file name
In the above three cases, there can be other permutations and combinations.
2. Restore the exported table data
[Email protected] mongodb]# mongoimport-d tank-c users--upsert tank/127.0.0.13 08:26:52.852 importe D 4 Objects
3. Table data import for some fields
[Email protected] mongodb]# mongoimport-d tank-c users --upsertfields uid,name,sex tank/ 127.0.0.1 Tue Dec
4. Restore the CSV file
[[email protected] mongodb]# mongoimport-d tank-c users--type csv--headerline--file tank/127.0.0.13 08:37:21.961 Imported 4 objects
MongoDB Database Operations--Backup restore Export Import