Mango knowledge supplement (1) MongoDB single-host backup and recovery

Source: Internet
Author: User
Tags mongodump mongorestore

Backup and recovery are important technologies for data storage. This is only a single machine learning record operation.
MongoDB supports backup and recovery. The backup format is bson and the backup format is JSON and CSV.

A) export bson format
It is mainly used for backup and snapshot of small data volumes on a single machine. Stored in bson format, bson (Binary JSON ),
Is a lightweight binary data format.

Backup command mongodump
Parameters
-- Help help
-V [-- verbose] Be more verbose (include multiple times for more
Verbosity e.g.-vvvvv)
-H [-- Host] Arg specifies the IP address of the server to be backed up
-- Port Arg specifies the server port to be backed up. It must be used with the server IP address in the format of IP: port.
-- IPv6 supports enabling IPv6, Which is disabled by default.
-U [-- username] Arg User Name
-P [-- Password] Arg Password
-- Dbpath Arg specifies the location for accessing data after database connection, but it cannot be the current directory
-- Directoryperdb creates a directory for each database.
-D [-- dB] Arg specifies the name of the database to be backed up
-C [-- collection] Arg specifies the collection to be exported
-O [-- Out] Arg (= dump) output directory. The default directory is the dump directory created in the path where the command is executed.
-Q [-- Query] Arg can be queried in JSON format and data can be selectively exported.
-- Use oplog
-- Repair tries to repair the damaged database

Restore command mongorestore
Parameters
-- Help help
-V [-- verbose] Be more verbose (include multiple times for more
Verbosity e.g.-vvvvv)
-H [-- Host] Arg specifies the IP address of the server to be imported
-- Port Arg specifies the server port to be imported. It must be used with the server IP address in the format of IP: port.
-- IPv6 supports enabling IPv6, Which is disabled by default.
-U [-- username] Arg User Name
-P [-- Password] Arg Password
-- Dbpath Arg specifies the location for accessing data after database connection, but it cannot be the current directory
-- Directoryperdb creates a directory for each database.
-D [-- dB] Arg specifies the name of the database to be backed up
-C [-- collection] Arg specifies the collection to be exported
-- Objcheck check validity of objects before insertion
-- Filter Arg: the filter before insertion
-- Drop: Delete the set to be imported before import
-- Oplogreplay: Log Recovery Time Point

Test, first view the database (mytest) file to be tested in uppercase
> Show DBS
Admin (empty)
Mytest 1.953125 GB
Start backup and export the local mytest database to the tmp directory
# Mongodump-D mytest-O/tmp
The export end time is as follows:
Real 0m5. 412 s
User 0m0. 007 s
Sys 0m2. 039 s
After export, the data is saved in the bson format and the volume is 577 MB. The mytest library stores MP3 files, so the exported database does not compress much.

Start the recovery test. First Delete the mytest database and import
# Mongorestore -- directoryperdb-D mytest/tmp/mytest
The import End Time is as follows:
Real 0m52. 587 s
User 0m0. 514 s
Sys 0m1. 732 s

Conclusion: The import time is about 10 times the export time.

B) Export the JSON format
Although the export speed is fast, but the export is JSON, CSV may be incompatible with some data types, and it is not guaranteed that the data can be completely imported.
So simply record it, it's not an experiment.
Export command export Export
Parameters
-Q [-- Query] Arg query filter, as a JSON string
-- CSV export to CSV instead of JSON
-- Jsonarray output to a JSON array rather than one object per
Line


Restore command restore Import
Parameters
-- Ignore the airspace in CSV and TSV files
-- Type Arg specifies the type of the imported file. The default value is JSON, which can be CSV or TSV.
-- Drop: Delete the original collection before import.
-- Headerline: only imports CSV files. The first line of the TSV file is used as the header file.
-- Upsert inserts or updates an existing object
-- Upsertfields Arg comma-separated fields for the query part of
Upsert. You shoshould make sure this is indexed
-- Stoponerror: stops when an import error occurs.
-- Jsonarray: imports a JSON array. Currently, each array has a limit of 4 MB.

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.