MongoDB 가져오기 백업 복구 데이터 상세 정보 및 인스턴스 내보내기

6896 단어
데이터베이스 백업 및 데이터 복구의 중요성은 데이터 백업, 데이터 복구의 예를 들면 다음과 같습니다.
테스트 데이터 만들기
db:testdb,collection:user,10개의 레코드 삽입

mongo
MongoDB shell version: 3.0.2
connecting to: test
> use testdb
switched to db testdb
> db.user.insert({id:1,name:"  1"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:2,name:"  2"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:3,name:"  3"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:4,name:"  4"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:5,name:"  5"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:6,name:"  6"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:7,name:"  7"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:8,name:"  8"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:9,name:"  9"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:10,name:"  10"});
WriteResult({ "nInserted" : 1 })
> 
> db.user.find();
{ "_id" : ObjectId("574d7aae8780832e6c4e27b4"), "id" : 1, "name" : "  1" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b5"), "id" : 2, "name" : "  2" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b6"), "id" : 3, "name" : "  3" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b7"), "id" : 4, "name" : "  4" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b8"), "id" : 5, "name" : "  5" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b9"), "id" : 6, "name" : "  6" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27ba"), "id" : 7, "name" : "  7" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bb"), "id" : 8, "name" : "  8" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bc"), "id" : 9, "name" : "  9" }
{ "_id" : ObjectId("574d7ab08780832e6c4e27bd"), "id" : 10, "name" : "  10" }

데이터 내보내기mongoexport
매개변수 설명:
- d 데이터베이스 이름 - c collection 이름 - o 출력 파일 이름 - type 출력 형식, 기본 json-f 출력 필드, 만약 - type이 csv라면 - f "필드 이름"
자세한 매개변수 설명은 mongoexport Chelp 참조
예:user의 모든 기록을/tmp/user로 내보냅니다.json

mongoexport -d testdb -c user -o /tmp/user.json
2016-05-31T20:00:32.257+0800  connected to: localhost
2016-05-31T20:00:32.286+0800  exported 10 records

cat /tmp/user.json
{"_id":{"$oid":"574d7aae8780832e6c4e27b4"},"id":1,"name":"  1"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b5"},"id":2,"name":"  2"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b6"},"id":3,"name":"  3"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b7"},"id":4,"name":"  4"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b8"},"id":5,"name":"  5"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b9"},"id":6,"name":"  6"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27ba"},"id":7,"name":"  7"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bb"},"id":8,"name":"  8"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bc"},"id":9,"name":"  9"}
{"_id":{"$oid":"574d7ab08780832e6c4e27bd"},"id":10,"name":"  10"}


예:user의 모든 id를/tmp/user로 내보냅니다.csv
csv 형식이지만 필드를 지정하지 않으면 오류가 발생합니다.

mongoexport -d testdb -c user --type csv -o /tmp/user.csv
2016-05-31T20:01:05.393+0800  Failed: CSV mode requires a field list

mongoexport -d testdb -c user --type csv -f "id" -o /tmp/user.csv
2016-05-31T20:01:46.510+0800  connected to: localhost
2016-05-31T20:01:46.534+0800  exported 10 records

cat /tmp/user.csv
id
1
2
3
4
5
6
7
8
9
10


데이터 가져오기mongoimport
매개변수 설명:
- d 데이터베이스 이름 - c collection 이름 - type 가져오는 형식, 기본 json-f 가져오는 필드 이름 - headerline 가져오는 형식이 csv이면 첫 번째 줄의 제목을 가져오는 필드 - file 가져올 파일로 사용할 수 있습니다
자세한 매개변수 설명은 mongoimport ㎡Chelp 참조
가져오기 전에 collection user를 비웁니다.

> db.user.drop();
true
> db.user.find();
> 

예: 전례를 내보낸user.json 가져오기

mongoimport -d testdb -c user --file /tmp/user.json
2016-05-31T20:10:22.240+0800  connected to: localhost
2016-05-31T20:10:22.287+0800  imported 10 documents

예: 전례를 내보낸user.csv 가져오기

mongoimport -d testdb -c user --type csv --headerline --file /tmp/user.csv
2016-05-31T20:11:28.975+0800  connected to: localhost
2016-05-31T20:11:29.003+0800  imported 10 documents

데이터 백업 mongodump
매개변수 설명:
- d 데이터베이스 이름 - c collection 이름 - o 백업 파일 경로
더 많은 매개변수 설명은 mongodump ㎡Chelp 참조
예:testdb의user를/tmp에 백업

mongodump -d testdb -c user -o /tmp
2016-05-31T20:18:25.813+0800  writing testdb.user to /tmp/testdb/user.bson
2016-05-31T20:18:25.818+0800  writing testdb.user metadata to /tmp/testdb/user.metadata.json
2016-05-31T20:18:25.849+0800  done dumping testdb.user

데이터 복구 mongorestore
매개변수 설명:
- d 데이터베이스 이름 - c collection 이름
더 많은 매개변수 설명은 mongorestore ㎡Chelp 참조
가져오기 전에 collection user를 비웁니다.

> db.user.drop();
true
> db.user.find();
>

예: 이전 백업 데이터 복구

mongorestore -d testdb -c user /tmp/testdb/user.bson 
2016-05-31T20:21:23.050+0800  checking for collection data in /tmp/testdb/user.bson
2016-05-31T20:21:23.084+0800  reading metadata file from /tmp/testdb/user.metadata.json
2016-05-31T20:21:23.088+0800  restoring testdb.user from file /tmp/testdb/user.bson
2016-05-31T20:21:23.153+0800  restoring indexes for collection testdb.user from metadata
2016-05-31T20:21:23.156+0800  finished restoring testdb.user
2016-05-31T20:21:23.156+0800  done

읽어주셔서 감사합니다. 여러분께 도움이 되었으면 좋겠습니다. 본 사이트에 대한 지지에 감사드립니다!

좋은 웹페이지 즐겨찾기