MongoDB backup restores data in one production environment

Source: Internet
Author: User

Recently developed a version of the functionality of the use of MongoDB paging, lazy to build data, and then studied the next from the production environment to export data to the local test.

Study a bit, found that MongoDB backup restore and MySQL syntax is quite similar, below please see detailed introduction.

One, backup data

Because you need to test the data, you must first back up a copy of the data from the production server (CentOS 6.5),

format : mongodump-h IP--port Port-u user name-p password-d database--drop file exists path

Example:

127.0. 0.1 27017 -u user name-p password-d ad_api_count-o/tmp

(Note that if the password has special characters that require \ Escape under Linux)

You can see the information from the two collections in Ad_api_count by entering the Ad_api_count directory of the backed up data.

Second, restore the database

Download the export database to the local e:\ee\ad_api_count under the production environment, then we can import the data to local MongoDB for testing.

command format : mongorestore-h IP--port Port-u user name-p password-d database file name specific path (suffix format is Bson)

Example:

CD C:\Program files\mongodb\server\3.4127.0. 0.1 27017 -u user name-p password-D ad_api_count E:\cust\ad_api_count\userAdCount.bson

As we know, we've done the restoration work.

Third, other

Mongoexport Export the table, or export some of the fields in the table:

Command format: mongoexport-h IP--port Port-u user name-p password-D database-C table name-F field-Q conditional export--csv-o file name specific path (suffix format can be. dat or. csv);

mongoimport Import the table, or some of the fields in the table :

1. restore a non-CSV file exported from an entire table

   Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name specific path (--drop when you do not need to delete the source file can not add)

2. restore the export file for some fields

  Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name specific path (--drop when you do not need to delete the source file can not add)

3. restore the exported CSV file (if the data is exported without the--CSV option, the exported data will have a lot of double quotes, the import will fail)

  Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type (CSV)--headerline--upsert--drop file name specific path

Note: If the database has data, to import the most recent data, you need to add the--upsert option, update the data, or you will get an error (Prompt for duplicate key fault collection)

MongoDB backup restores data in one production environment

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.