MongoDB Full library backup and restore, and single collection backup, recovery

Source: Internet
Author: User
Tags mongodb server server port mongodump mongorestore

Pre-backup Checks
> Show DBS
MyDB 0.0625GB
Admin (empty)
Bruce 0.0625GB
Local (empty)
Test 0.0625GB
> Use MyDB
Switched to DB MyDB
> Db.users.find ()
{"_id": ObjectId ("4e290aa39a1945747b28f1ee"), "a": 1, "B": 1}
{"_id": ObjectId ("4e2cd2182a65c81f21566318"), "a": 3, "B": 5}
>

Backup of the whole library:
Mongodump-h dbhost-d Dbname-o dbdirectory
-H:MONGDB the server address, for example: 127.0.0.1, you can also specify the port number: 127.0.0.1:27017
-D: The database instance that needs to be backed up, for example: test
-O: The data storage location of the backup, for example: C:\data\dump, of course, the directory needs to be established in advance, after the backup is completed, the system automatically establishes a test directory under the Dump directory, which holds the backup data of the database instance.

Mongodump official note (available through Mongodump--help view): Options:--help produce help message-v [--verbose] Being                More verbose (include multiple times for more verbosity e.g.-VVVVV)--version                           Print the program's version and Exit-h [--host] arg MONGO host to connect to (/S1,S2 for Sets)--port ARG server port. Can also use--host hostname:port--ipv6 enable IPV6 support (disabled by default)-U [--username] A RG Username-p [--password] arg password--dbpath arg directly access Mongod database files in the G Iven path, instead of connecting to a Mongod server-needs to lock t He data directory, so cannot being used if a mongod is currently accessing the same path--directo Ryperdb if dbpath specified, each db was in a separate directorY--journal enable journaling-d [--DB] arg database to use-c [--collection] arg collecti On to use (some commands)-o [--out] arg (=dump) output directory or "-" for Stdout-q [--query] arg json quer Y--oplog use Oplog for point-in-time snapshotting--repair try to recover a crashed da Tabase--forcetablescan Force a table scan (does not use $snapshot)

Full library Restore:
mongorestore-h dbhost-d dbname–directoryperdb dbdirectory
-h:mongodb server address
- D: The database instance that needs to be restored, for example: Test, of course, this name can be different from the time of backup, such as Test2
–directoryperdb: The location of the backup data, for example: C:\data\dump\test, Why do you want to add a test here instead of a dump at the time of backup, and read the tips yourself?
–drop: When recovering, delete the current data and then restore the backed up data. That is, after the recovery, add the modified data after the backup will be deleted, use with caution Oh!

Mongorestore official note (available through Mongorestore--help): Options:--help produce help message-v [--verbose]               Be + verbose (include multiple times for more verbosity e.g.-VVVVV)--version              Print the program's version and Exit-h [--host] arg MONGO host to connect to (/S1,S2 for sets)--port arg Server port. Can also use--host hostname:port--ipv6 enable IPV6 support (disabled by default)-u [--username] Ar                           G username-p [--password] arg password--dbpath arg directly access Mongod database files in the given Path, instead of connecting to a Mongod server-needs to lock the DAT         A directory, so cannot are used if a mongod is currently accessing the same path--directoryperdb If dbpath specified, each db are in a separate directory--journal enable Journaling-d [--DB] arg database to use-c [--collection] arg collection to use (some commands)--objcheck                  Validate object before inserting--filter arg filter to apply before inserting--drop Drop each collection before import--oplogreplay replay oplog for point-in-time restore--oploglimit ar G exclude Oplog entries newer than provided timestamp (epoch[:ordinal])--keepindexversio         n don ' t upgrade indexes to newest version--nooptionsrestore don ' t restore collection options--noindexrestore Don ' t restore indexes--w ARG (=1) minimum number of replicas per write

Single Collection backup:
mongoexport-h dbhost-d dbname-c collectionname-f collectionkey-o dbdirectory
-h:mongodb server Address
-D: The database instance that needs to be restored
-C: Collection to be recovered
-f: field to export (omitted for all fields)
-O: Indicates the exported file name

Mongoexport official note (available through Mongoexport--help):--help produce help message-v [--verbose] being M                 Ore verbose (include multiple times for more verbosity e.g.-VVVVV)--version                            Print the program's version and Exit-h [--host] arg MONGO host to connect to (/S1,S2 for Sets)--port ARG server port. Can also use--host hostname:port--ipv6 enable IPV6 support (disabled by default)-U [--username] Arg username-p [--password] arg password--dbpath arg directly access Mongod database files in t He given path, instead of connecting to a Mongod server-needs to Lock the Data directory, so cannot being used if a mongod is currently accessing the same path--                        Directoryperdb if dbpath specified, each db was in a separate    Directory--journal enable journaling-d [--DB] arg database to use-c [--collection]                            Arg collection to use (some commands)-F [--fields] arg comma separated list of field names e.g. F Name,age--fieldfile arg file with fields names-1 per line-q [--query] arg query Filter, as a JSON string--csv export to CSV instead of Json-o [--out] arg output file;                            If not specified, stdout are used--jsonarray output to a JSON array rather than one object per                            line-k [--slaveok] arg (=1) use secondaries for export if available, default True--forcetablescan force a table scan (does not use $snapshot)

Single Collection recovery:
mongoimport-d dbhost-c collectionname–type csv–headerline–file
-type: Indicates the file format to import
-headerline: The first row is not imported because the first row is a column name
-file: Indicates the file path to import

Mongoimport official note (available through Mongoimport--help):--help produce help message-v [--verbose] being more Verbose (include multiple times for more verbosity e.g.-vvvvv)--version print th              E program's version and Exit-h [--host] arg MONGO host to connect to (/S1,S2 for sets)--port arg Server port. Can also use--host hostname:port--ipv6 enable IPV6 support (disabled by default)-u [--username] Ar                           G username-p [--password] arg password--dbpath arg directly access Mongod database files in the given Path, instead of connecting to a Mongod server-needs to lock the DAT         A directory, so cannot are used if a mongod is currently accessing the same path--directoryperdb If dbpath specified, each db are in a separate directory--journal enable J Ournaling -d [--DB] arg database to use-c [--collection] arg collection to use (some commands)-F [--fields] Arg Comma separated list of field names e.g.-f name,age--fieldfile arg file with field names-1 per line--ig   Noreblanks if given, empty fields in CSV and TSV would be is ignored--type arg type of file to import. Default:json (JSON,CSV,TSV)--file arg file to import from; If not specified stdin is used--drop drop collection first--headerline csv,tsv only-use First line as headers--upsert insert or update objects that already exist--upsertfields Arg comma- Separated fields for the query part of the Upsert. Should make sure this is indexed--stoponerror stop importing at first error rather than continuing--json Array load a JSON array, not one item per line. Currently limited to 16MB.

Additional import and export operations:

1. mongoimport-d my_mongodb-c User User.dat

Parameter description:

-d indicates which library to use, in this case "My_mongodb"

-C indicates the table to be exported, in this case "user"

You can see that the table structure is created implicitly when importing data

2. mongoexport-d my_mongodb-c user-o User.dat

Parameter description:

-d indicates which library to use, in this case "My_mongodb"

-C indicates the table to be exported, in this case "user"

-O indicates the name of the file to be exported, in this case "User.dat"

From the above you can see how the export is using the JSON style.

Http://www.cr173.com/html/20275_1.html

MongoDB Full library backup and restore, and single collection backup, recovery

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.