mongoDB 数据备份文档

参考:http://docs.mongodb.org/manual/administration/import-export/

mporting and Exporting MongoDB Data

Full database instance backups are useful for disaster recovery protection and routine database backup operation; however, some cases require additional import and export functionality.

This document provides an overview of the import and export tools provided in distributions for MongoDB administrators. These utilities are useful when you want to backup or export a portion of your database without capturing the state of the entire database. For more complex data migration tasks, you may want to write your own import and export scripts using a client driver to interact with the database itself.

 

Collection Export with mongoexport

With the mongoexport utility you can create a backup file. In the most simple invocation, the command takes the following form:

mongoexport --collection collection --out collection.json
 

 

This will export all documents in the collection named collection into the file collection.json . Without the output specification (i.e. “--out collection.json ”,) mongoexport writes output to standard output (i.e. “stdout.”) You can further narrow the results by supplying a query filter using the “--query ” and limit results to a single database using the “--db ” option. For instance:

mongoexport --db sales --collection contacts --query '{"field": 1}'
 

This command returns all documents in the sales database’s contacts collection, with a field named field with a value of 1 . Enclose the query in single quotes (e.g. ' ) to ensure that it does not interact with your shell environment. The resulting documents will return on standard output.

By default, mongoexport returns one JSON document per MongoDB document. Specify the “--jsonArray ” argument to return the export as a single JSON array. Use the “--csv ” file to return the result in CSV (comma separated values) format.

If your mongod instance is not running, you can use the “--dbpath ” option to specify the location to your MongoDB instance’s database files. See the following example:

mongoexport --db sales --collection contacts --dbpath /srv/MongoDB/
 

This reads the data files directly. This locks the data directory to prevent conflicting writes. The mongod process must not be running or attached to these data files when you run mongoexport in this configuration.

The “--host ” and “--port ” options allow you to specify a non-local host to connect to capture the export. Consider the following example:

mongoexport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --file mdb1-examplenet.json
 

On any mongoexport command you may, as above specify username and password credentials as above.

Collection Import with mongoimport

To restore a backup taken with mongoexport . Most of the arguments to mongoexport also exist for mongoimport . Consider the following command:

mongoimport --collection collection --file collection.json
 

This imports the contents of the file collection.json into the collection named collection . If you do not specify a file with the “--file ” option, mongoimport accepts input over standard input (e.g. “stdin.”)

If you specify the “--upsert ” option, all of mongoimport operations will attempt to update existing documents in the database and insert other documents. This option will cause some performance impact depending on your configuration.

You can specify the database option “--db ” to import these documents to a particular database. If your MongoDB instance is not running, use the “--dbpath ” option to specify the location of your MongoDB instance’s database files. Consider using the “--journal ” option to ensure that mongoimport records its operations in the journal. The mongod process must not be running or attached to these data files when you run mongoimport in this configuration.

Use the “--ignoreBlanks ” option to ignore blank fields. For CSV and TSV imports, this option provides the desired functionality in most cases: it avoids inserting blank fields in MongoDB documents.

你可能感兴趣的:(mongodb)