MongoDB Database Operations--Backup restore Export Import

Source: Internet
Author: User
Tags mongodump mongorestore



One, Mongodump backup database



1, Common command grid

One, mongodump backup database

1, commonly used command grid

 
1
mongodump -h IP --port port -u username -p password -d database -o file path
If there are no users, you can remove -u and -p. If you export the local 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

 

[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
  
. . . . . . . . . . . . . . . . . . . . Omitted. . . . . . . . . . . . . . . . . . . . . . . . . .
3. 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
Three, mongorestore restore the database

1, Common command format

 

mongorestore -h IP --port port -u username -p password -d database --drop file exists path
  --drop means delete all records first and then restore.

2, restore all databases to mongodb

 

[root @ localhost mongodb] # mongorestore / home / zhangy / mongodb / #The path here is the backup path of all libraries
  3. Restore the specified database

 

[root @ localhost mongodb] # mongorestore -d tank / home / zhangy / mongodb / tank / #tankThis database backup path
  
[root @ localhost mongodb] # mongorestore -d tank_new / home / zhangy / mongodb / tank / #The tank and tank_new database
  These two commands can backup and restore the database. The file format is json and bson. Cannot refer to write to table backup or restore.

Four, mongoexport export table, or some fields in the table

1, Common command format

 

mongoexport -h IP --port port -u username -p password -d database -c table name -f field -q conditional export --csv -o file name
The above parameters are easy to understand, and focus on the following: -f export refers to the field, divided by font size, -f name, email, and age export the three fields of name, email, and age -q can be exported based on the query condition, -q '{" uid ":" 100 "} 'Export data with a uid of 100-csv means that the exported file format is csv. This is more useful because most relational databases support csv and have something in common here

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
3. 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, according to the conditions to dare to output data

 

[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
Five, mongoimport import table, or some fields in the table

1, Common command format

1.1, restore the non-csv file exported by the entire table mongoimport -h IP --port port -u username -p password -d database -c table name --upsert --drop file name to talk about --upsert, other parameters above The command has been mentioned, --upsert 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 --upsert like 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 can be other permutations and combinations.

2. Restore the exported table data


[root @ localhost mongodb] # mongoimport -d tank -c users --upsert tank / users.dat
connected to: 127.0.0.1
Tue Dec 3 08: 26: 52.852 imported 4 objects
3. Table data import 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] # mongoimport -d tank -c users --type csv --headerline --file tank / users.csv
connected to: 127.0.0.1
Tue Dec 3 08: 37: 21.961 imported 4 objects
The overall feeling is that the backup and restore of mongodb is quite powerful, although a little troublesome.

mongodb database operation-backup, restore, export, import

label:

Original address: http://www.cnblogs.com/smallstupidwife/p/4836811.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.