변군이글루

[리눅스] MongoDB 백업 및 복구 본문

* 리눅스

[리눅스] MongoDB 백업 및 복구

변군 변군이글루 2021. 7. 14. 16:47
728x90
반응형

MongoDB 백업 및 복구

백업(mongodump)하기

모든 데이터를 백업

mongodump --host HOST_NAME --port PORT_NUMBER

지정한 경로에 백업

mongodump --host 127.0.0.1 --port 27017 --out /tmp/mongo_backup
$ mongodump --host 127.0.0.1 --port 27017 --out /tmp/mongo_backup
2021-07-15T09:50:57.544+0900	writing admin.system.version to
2021-07-15T09:50:57.545+0900	done dumping admin.system.version (1 document)
2021-07-15T09:50:57.545+0900	writing WEB_STATIC.WEBACC_DATE to
2021-07-15T09:50:57.545+0900	writing WEB_STATIC.WEBACC_KEYWORD to
2021-07-15T09:50:57.545+0900	writing WEB_STATIC.WEBACC_TOTAL to
2021-07-15T09:50:57.546+0900	writing WEB_STATIC.WEBACC_LOG to
2021-07-15T09:51:00.530+0900	[........................]  WEB_STATIC.WEBACC_KEYWORD   289993/85209455  (0.3%)
2021-07-15T09:51:00.530+0900	[........................]     WEB_STATIC.WEBACC_DATE  192123/348907014  (0.1%)
2021-07-15T09:51:00.530+0900	[........................]    WEB_STATIC.WEBACC_TOTAL    212446/5980259  (3.6%)
2021-07-15T09:51:00.530+0900	[........................]      WEB_STATIC.WEBACC_LOG    163050/4280165  (3.8%)
...
2021-07-15T10:30:03.529+0900	[#######################.]  WEB_STATIC.WEBACC_DATE  346562554/348907014  (99.3%)
2021-07-15T10:30:06.530+0900	[#######################.]  WEB_STATIC.WEBACC_DATE  347105234/348907014  (99.5%)
2021-07-15T10:30:09.530+0900	[#######################.]  WEB_STATIC.WEBACC_DATE  347653502/348907014  (99.6%)
2021-07-15T10:30:12.529+0900	[#######################.]  WEB_STATIC.WEBACC_DATE  348239348/348907014  (99.8%)
2021-07-15T10:30:15.529+0900	[#######################.]  WEB_STATIC.WEBACC_DATE  348812834/348907014  (100.0%)
2021-07-15T10:30:15.811+0900	[########################]  WEB_STATIC.WEBACC_DATE  348918903/348907014  (100.0%)
2021-07-15T10:30:15.811+0900	done dumping WEB_STATIC.WEBACC_DATE (348918903 documents)

아카이브(gzip)로 백업

mongodump --host 127.0.0.1 --port 27017 --archive=/tmp/mongo_backup/serv-205.gz

백업 파일 전송

tar ssh 전송

$ cd /tmp/mongo_backup/admin

$ tar czf - * | ssh root@222.222.222.222 "cd /tmp/mongo_backup/admin; tar xfp -"

복구(mongorestore)하기

지정한 경로로 복구

mongorestore --host 127.0.0.1 --port 27017 --db admin /tmp/mongo_backup/admin
$ mongorestore --host 127.0.0.1 --port 27017 --db admin --drop /tmp/mongo_backup/admin
2021-07-15T13:37:29.670+0900	The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-07-15T13:37:29.670+0900	building a list of collections to restore from /tmp/mongo_backup/admin dir
2021-07-15T13:37:29.670+0900	reading metadata for db.system.version from /tmp/mongo_backup/admin/system.version.metadata.json
2021-07-15T13:37:29.672+0900	finished restoring db.system.version (0 documents, 0 failures)
2021-07-15T13:37:29.672+0900	Failed: db.system.version: error creating collection db.system.version: error running create command: (InvalidNamespace) cannot write to 'db.system.version'
2021-07-15T13:37:29.672+0900	0 document(s) restored successfully. 0 document(s) failed to restore.

아카이브(gzip)로 복구하기

mongorestore --host 127.0.0.1 --port 27017 --gzip --archive=/tmp/mongo_backup/serv-205.gz

 

Reference > MongoDB Package Components > mongodump

https://docs.mongodb.com/database-tools/mongorestore/

https://docs.mongodb.com/v4.2/reference/program/mongodump/

 

728x90
반응형
0 Comments
댓글쓰기 폼