MongoDB Backup Restore Export Import

Source: Internet
Author: User
Tags mongodump mongorestore

Zhang Ying published in 2013-12-03

Category list: NoSQL

Tags: mongodb, backup, import, export, restore

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

    1. Mongodump-h IP--port Port-u user name-p password-d database-o file exists path

If there is no user who 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

  1. [Email protected] mongodb]# mongodump-h 127.0.0.1-o/home/zhangy/mongodb/
  2. Connected to:127.0.0.1
  3. Tue Dec 3 06:15:55.448 all DBS
  4. Tue Dec 3 06:15:55.449 database:test to/home/zhangy/mongodb/test
  5. Tue Dec 3 06:15:55.449 test.system.indexes To/home/zhangy/mongodb/test/system.indexes.bson
  6. Tue Dec 3 06:15:55.450 1 objects
  7. Tue Dec 3 06:15:55.450 test.posts To/home/zhangy/mongodb/test/posts.bson
  8. Tue Dec 3 06:15:55.480 0 objects
  9. 。。。。。。。。。。。。。。。。。。。。 Omitted..........................

3, export the specified database

View copy print?
  1. [Email protected] mongodb]# mongodump-h 192.168.1.108-d tank-o/home/zhangy/mongodb/
  2. Connected to:192.168.1.108
  3. Tue Dec 3 06:11:41.618 database:tank To/home/zhangy/mongodb/tank
  4. Tue Dec 3 06:11:41.623 tank.system.indexes To/home/zhangy/mongodb/tank/system.indexes.bson
  5. Tue Dec 3 06:11:41.623 2 objects
  6. Tue Dec 3 06:11:41.623 tank.contact To/home/zhangy/mongodb/tank/contact.bson
  7. Tue Dec 3 06:11:41.669 2 objects
  8. Tue Dec 3 06:11:41.670 Metadata for tank.contact To/home/zhangy/mongodb/tank/contact.metadata.json
  9. Tue Dec 3 06:11:41.670 tank.users To/home/zhangy/mongodb/tank/users.bson
  10. Tue Dec 3 06:11:41.685 2 objects
  11. Tue Dec 3 06:11:41.685 Metadata for tank.users To/home/zhangy/mongodb/tank/users.metadata.json

Third, Mongorestore restore the database

1, Common command format

    1. 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

    1. [Email protected] mongodb]# mongorestore/home/zhangy/mongodb/#这里的路径是所有库的备份路径

3. Restore the specified database

    1. [Email protected] mongodb]# mongorestore-d tank/home/zhangy/mongodb/tank/#tank这个数据库的备份路径
    2. [Email protected] mongodb]# mongorestore-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

    1. Mongoexport-h IP--port Port-u user name-p password-D database-C table name-F field-Q conditional export--csv-o file name

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

    1. [Email protected] mongodb]# mongoexport-d tank-c users-o/home/zhangy/mongodb/tank/users.dat
    2. Connected to:127.0.0.1
    3. Exported 4 Records

3. Export some fields from the table

    1. [Email protected] mongodb]# mongoexport-d tank-c users--csv-f uid,name,sex-o tank/users.csv
    2. Connected to:127.0.0.1
    3. Exported 4 Records

4, according to the conditions to dare to out data

View copy print?
    1. [[email protected] mongodb]# mongoexport-d tank-c users-q ' {uid:{$gt: 1}} '-O Tank/users.json
    2. Connected to:127.0.0.1
    3. Exported 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

    1. 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

    1. Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name

--upsertfields Root--upsert like

1.3. Restore the exported CSV file

    1. 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

    1. [Email protected] mongodb]# mongoimport-d tank-c users--upsert Tank/users.dat
    2. Connected to:127.0.0.1
    3. Tue DEC 3 08:26:52.852 imported 4 objects

3. Table data import for some fields

    1. [Email protected] mongodb]# mongoimport-d tank-c users--upsertfields uid,name,sex tank/users.dat
    2. Connected to:127.0.0.1
    3. Tue DEC 3 08:31:15.179 imported 4 objects

4. Restore the CSV file

    1. [Email protected] mongodb]# mongoimport-d tank-c users--type csv--headerline--file tank/users.csv
    2. Connected to:127.0.0.1
    3. Tue DEC 3 08:37:21.961 imported 4 objects

Overall feeling, MongoDB backup and restore, or quite powerful, though a little troublesome.

MongoDB Backup Restore Export Import

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.