mongodb database operation backup restore export import

  • 2020-05-07 20:37:34
  • OfStack

1, mongodump backup database

1. Common command grid


mongodump -h IP --port  port  -u  The user name  -p  password  -d  The database  -o  File existence path  

If there are no users who can drop -u and -p.
If you export a native database, you can remove -h.
If it's the default port, you can drop --port.
If you want to export all databases, you can remove -d.

Export all databases


[root@localhost mongodb]# mongodump -h 127.0.0.1 -o /home/zhangy/mongodb/ 
connected to: 127.0.0.1 
Tue Dec 3 06:15:55.448 all dbs 
Tue Dec 3 06:15:55.449 DATABASE: test   to   /home/zhangy/mongodb/test 
Tue Dec 3 06:15:55.449   test.system.indexes to /home/zhangy/mongodb/test/system.indexes.bson 
Tue Dec 3 06:15:55.450     1 objects 
Tue Dec 3 06:15:55.450   test.posts to /home/zhangy/mongodb/test/posts.bson 
Tue Dec 3 06:15:55.480     0 objects 
 
 . Omit...  

Export the specified database


[root@localhost mongodb]# mongodump -h 192.168.1.108 -d tank -o /home/zhangy/mongodb/ 
connected to: 192.168.1.108 
Tue Dec 3 06:11:41.618 DATABASE: tank   to   /home/zhangy/mongodb/tank 
Tue Dec 3 06:11:41.623   tank.system.indexes to /home/zhangy/mongodb/tank/system.indexes.bson 
Tue Dec 3 06:11:41.623     2 objects 
Tue Dec 3 06:11:41.623   tank.contact to /home/zhangy/mongodb/tank/contact.bson 
Tue Dec 3 06:11:41.669     2 objects 
Tue Dec 3 06:11:41.670   Metadata for tank.contact to /home/zhangy/mongodb/tank/contact.metadata.json 
Tue Dec 3 06:11:41.670   tank.users to /home/zhangy/mongodb/tank/users.bson 
Tue Dec 3 06:11:41.685     2 objects 
Tue Dec 3 06:11:41.685   Metadata for tank.users to /home/zhangy/mongodb/tank/users.metadata.json 

3, mongorestore restore database

1. Common command format


mongorestore -h IP --port  port  -u  The user name  -p  password  -d  The database  --drop  File existence path  

 
--drop means to delete all records and then restore them.

2. Restore all databases to mongodb


[root@localhost mongodb]# mongorestore /home/zhangy/mongodb/  # The path here is the backup path for all libraries  

 
Restore the specified database


[root@localhost mongodb]# mongorestore -d tank /home/zhangy/mongodb/tank/  #tank The backup path for this database  
 
[root@localhost mongodb]# mongorestore -d tank_new /home/zhangy/mongodb/tank/  # will tank There are tank_new In the database 

 
These two commands can achieve the database backup and restore, the file format is json and bson. Cannot refer to writing to a table backup or restore.

4, mongoexport exports the table, or some fields in the table

1. Common command format


mongoexport -h IP --port  port  -u  The user name  -p  password  -d  The database  -c  The name of the table  -f  field  -q  Conditions for export  --csv -o  The file name  

The above parameters are easy to understand, the key point is 1:
-f       export refers to the field, split by size, -f name,email,age export name,email,age three fields
-q       can be exported by root query condition, -q '{"uid" : "100"}' can be exported by data with uid of 100
--csv means that the exported file format is csv, which is useful because most relational databases support csv, which has something in common

2. Export the entire table


[root@localhost mongodb]# mongoexport -d tank -c users -o /home/zhangy/mongodb/tank/users.dat 
connected to: 127.0.0.1 
exported 4 records 

Export some fields in the table


[root@localhost mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv 
connected to: 127.0.0.1 
exported 4 records 

4. Figure out the data according to the conditions


[root@localhost mongodb]# mongoexport -d tank -c users -q '{uid:{$gt:1}}' -o tank/users.json 
connected to: 127.0.0.1 
exported 3 records 

5, mongoimport imports the table, or some of the fields in the table

1. Common command format

1.1. Restore the non-csv files exported from the whole table
mongoimport-h IP --port port -- u username -- p password -- d database -- c table name --upsert --drop file name --  
Focus on 1 --upsert, other parameters mentioned in the above command, --upsert to insert or update existing data
1.2. Restore the export file of some fields
mongoimport-h IP --port port -- u username -- p password -- d database -- c table name --upsertFields field --drop file name --  
- upsertFields root - upsert1 sample
1.3. Restore the exported csv file
mongoimport-h IP --port port -- u username -- p password -- d database -- c table name --type type --headerline --upsert --drop file name --  
In the above three cases, there are other permutations and combinations.

2. Restore the exported table data


[root@localhost mongodb]# mongodump -h 127.0.0.1 -o /home/zhangy/mongodb/ 
connected to: 127.0.0.1 
Tue Dec 3 06:15:55.448 all dbs 
Tue Dec 3 06:15:55.449 DATABASE: test   to   /home/zhangy/mongodb/test 
Tue Dec 3 06:15:55.449   test.system.indexes to /home/zhangy/mongodb/test/system.indexes.bson 
Tue Dec 3 06:15:55.450     1 objects 
Tue Dec 3 06:15:55.450   test.posts to /home/zhangy/mongodb/test/posts.bson 
Tue Dec 3 06:15:55.480     0 objects 
 
 . Omit...  

0

3. Import table data of some fields

[root@localhost mongodb]# mongoimport -d tank -c users   --upsertFields uid,name,sex   tank/users.dat  
connected to: 127.0.0.1  
Tue Dec   3 08:31:15.179 imported 4 objects  

4. Restore the csv file


[root@localhost mongodb]# mongodump -h 127.0.0.1 -o /home/zhangy/mongodb/ 
connected to: 127.0.0.1 
Tue Dec 3 06:15:55.448 all dbs 
Tue Dec 3 06:15:55.449 DATABASE: test   to   /home/zhangy/mongodb/test 
Tue Dec 3 06:15:55.449   test.system.indexes to /home/zhangy/mongodb/test/system.indexes.bson 
Tue Dec 3 06:15:55.450     1 objects 
Tue Dec 3 06:15:55.450   test.posts to /home/zhangy/mongodb/test/posts.bson 
Tue Dec 3 06:15:55.480     0 objects 
 
 . Omit...  

1

Overall, the backup and restore of mongodb is quite powerful, though a bit cumbersome.


Related articles: