MongoDB Backup recovery, data import Export

Source: Internet
Author: User
Tags mongodump mongorestore

Data export Mongoexport

Suppose there's a apachelog table in the library with 2 records, and we're going to export it

/test/mongodb/bin/mongouse Wxdata

Switched to DB Wxdata

Db.apachelog. Find ();

{"_id": ObjectId ("53993357e0e73ac14b29da8a"), "host": "66.249.69.194", "Method": "GET", "uri": "/uploads/yaopinimg/20 13012415323219.jpg "," Httpstatus ": $," size ": 7428," Agent ":" googlebot-image/1.0 "," status ": 0}

1 Common export methods

/test/mongodb/bin/mongoexport-d wxdata-c Apachelog-o Apachelog.dat

Connected to:127.0.0.1
Exported 2 records

Cat Apachelog.dat

{"_id": ObjectId ("53993357e0e73ac14b29da8a"), "host": "66.249.69.194", "Method": "GET", "uri": "/uploads/yaopinimg/20 13012415323219.jpg "," Httpstatus ": $," size ": 7428," Agent ":" googlebot-image/1.0 "," status ": 0}

Parameter description:
-d indicates which library to use, in this case "Wxdata"
-C indicates the table to be exported, in this case "Apachelog"
-O indicates the name of the file to be exported, in this case "Apachelog.dat"
From the above you can see how the export is using the JSON style.

2 exporting files in CSV format

/test/mongodb/bin/mongoexport-d wxdata-c apachelog--csv-f host,time,method-o apachelog_csv.dat

Connected to:127.0.0.1
Exported 2 records

Cat Apachelog_csv.dat

Host,time,method
"66.249.69.194", "12/jun/2014:12:57:59", "GET"
Parameter description:
--csv means to export to CSV format
-F indicates which examples need to be exported
More detailed usage can be mongoexport–help to see

Data import Mongoimport

1 Importing JSON data
We'll first remove the table Apachelog to show the effect

Db.apachelog.drop ();

True
Show collections;
System.indexes

Then import the data

/test/mongodb/bin/mongoimport-d wxdata-c Apachelog Apachelog.dat

Connected to:127.0.0.1
Imported 1 objects

The table structure is created implicitly when importing data

2 Importing CSV data

We'll first remove the table Apachelog to show the effect

Db.apachelog.drop ();

True
Show collections;
System.indexes

Then import the data

/test/mongodb/bin/mongoimport-d wxdata-c apachelog--type csv--headerline--file Apachelog_csv.dat

Connected to:127.0.0.1
Imported 1 objects

Parameter description:
--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
Attention:
CSV format is good, mainstream database support export to CSV format, so this format is very beneficial to heterogeneous data migration

Data backup Mongodump

Use Mongodump to make a library or table-level backup of MongoDB

Backing Up the Wxdata database

/test/mongodb/bin/mongodump-d Wxdata

Connected to:127.0.0.1
Database:wxdata to Dump/wxdata
Wxdata.system.indexes to Dump/wxdata/system.indexes.bson
1 objects
Wxdata.apachelog to Dump/wxdata/apachelog.bson
2 objects

A dump directory is created under the current directory to hold the backed up files
You can also specify the directory where the backup resides.

/test/mongodb/bin/mongodump-d Wxdata-o Wxdata_dump

Connected to:127.0.0.1
Database:wxdata to Wxdata_dump/wxdata
Wxdata.system.indexes to
Wxdata_dump/wxdata/system.indexes.bson
1 objects
Wxdata.apachelog to Wxdata_dump/wxdata/apachelog.bson
2 objects

In this example, the backup file exists in the Wxdata_dump directory under the current directory

Data Recovery Mongorestore

Since the backup has just been done, we will first remove the library Wxdata

Use Wxdata

Switched to DB Wxdata
Db.dropdatabase ()
{"Dropped": "Wxdata", "OK": 1}
Show DBS
Admin (empty)
Local (empty)
Test (empty)

Next we're going to restore the database

/test/mongodb/bin/mongorestore-d wxdata wxdata_dump/*

Connected to:127.0.0.1
Wed APR 00:03:03 Wxdata_dump/wxdata/apachelog.bson
Wed APR 00:03:03 going into namespace [Wxdata.apachelog]
Wed APR 00:03:03 2 objects found
Wed APR 00:03:03 Wxdata_dump/wxdata/system.indexes.bson
Wed APR 00:03:03 going into namespace [wxdata.system.indexes]
After verifying the database back, in fact, if you want to restore the library, it is not necessary to delete the Wxdata library, as long as it refers to
--drop parameter, you can delete the table and then insert the data into the table when you restore it.

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.