본문 바로가기
데이터베이스

docker mongodump and mongorestore

by 혜룐 2019. 8. 19.

$ sudo docker volume create db_backup
a2776236537b5759fbf7be5034a43e7c595a829abd04357fe254a8457b18133e

$ sudo docker volume ls | grep db_backup
local               db_backup

docker run시 호스트디렉터리를 마운트해보자.

호스트에는 /catherine/data/backup이라는 디렉터리에는  mongodump 데이터들이 있다.

$ sudo docker run --name mongo -it -v /catherine/data/backup/:/db_backups -d mongo:3.6.11
2d64fa268ae0014b466dd0fef656d2772af8f24392a0a321495cd9b9d9c7ad51

 확인해보자

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
2d64fa268ae0        mongo:3.6.11        "docker-entrypoint..."   About a minute ago   Up About a minute   27017/tcp           mongo
[catherine@server-catherine-build001 bla]$ sudo docker exec -it mongo /bin/bash
root@2d64fa268ae0:/# cd db_backups/
root@2d64fa268ae0:/db_backups# ll
total 0
drwxr-xr-x 6 root root 59 Aug 19 07:37 ./
drwxr-xr-x 1 root root 46 Aug 19 07:56 ../
drwxr-xr-x 4 root root 36 Aug 16 05:55 alpha/
drwxr-xr-x 3 root root 20 Aug 16 05:57 stage/
root@2d64fa268ae0:/db_backups# cd alpha/
root@2d64fa268ae0:/db_backups/alpha# ll
total 0
drwxr-xr-x 4 root root 36 Aug 16 05:55 ./
drwxr-xr-x 6 root root 59 Aug 19 07:37 ../
drwxr-xr-x 2 root root  6 Aug 16 05:54 foo/
drwxrwxrwx 3 root root 71 Aug 16 05:55 bla/
root@2d64fa268ae0:/db_backups/alpha# cd bla/
root@2d64fa268ae0:/db_backups/alpha/bla# ll
total 33928
drwxrwxrwx 3 root root       71 Aug 16 05:55 ./
drwxr-xr-x 4 root root       36 Aug 16 05:55 ../
-rw-r--r-- 1 1000 1000 34737316 Aug 16 05:55 foo-mongo-dump-2019-08-16_14_55.tar.gz
drwxr-xr-x 2 1000 1000     4096 Aug 16 05:55 bla/

호스트에 마운트된 dump파일을 restore해보자

root@2d64fa268ae0:/db_backups/alpha/bla# mongorestore -d bla /db_backups/alpha/bla/
2019-08-19T08:00:36.659+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-08-19T08:00:36.659+0000    building a list of collections to restore from bla dir
2019-08-19T08:00:36.669+0000    reading metadata for bla.matchRule from bla/matchRule.metadata.json
2019-08-19T08:00:36.669+0000    reading metadata for bla.bordContents from bla/bordContents.metadata.json
2019-08-19T08:00:36.670+0000    reading metadata for bla.qasets from bla/qasets.metadata.json
2019-08-19T08:00:36.671+0000    reading metadata for bla.usersChange from bla/usersChange.metadata.json
2019-08-19T08:00:36.680+0000    restoring bla.matchRule from bla/matchRule.bson
2019-08-19T08:00:36.688+0000    restoring bla.qasets from bla/qasets.bson
2019-08-19T08:00:36.780+0000    restoring bla.bordContents from bla/bordContents.bson
2019-08-19T08:00:36.789+0000    restoring bla.usersChange from bla/usersChange.bson
2019-08-19T08:00:39.739+0000    [........................]           bla.matchRule   8.31MB/227MB   (3.7%)
2019-08-19T08:00:39.742+0000    [##########..............]              bla.qasets  8.81MB/20.3MB  (43.5%)

 복구가 잘되었는지 확인해보자

root@2d64fa268ae0:/db_backups/alpha/bla# mongo
MongoDB shell version v3.6.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0144459a-b8cd-4515-a75c-0d48af7ff543") }
MongoDB server version: 3.6.11
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-08-19T07:56:40.827+0000 I CONTROL  [initandlisten]
2019-08-19T07:56:40.827+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-19T07:56:40.827+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-19T07:56:40.827+0000 I CONTROL  [initandlisten]
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
bla  0.145GB
> use bla
switched to db bla
> db.terms.findOne()
{
	"_id" : ObjectId("6c8764ea3834005eb112dc85"),
	"title" : "테스트입니다",
	"content" : "하하호호하하호",
	"updatedAt" : ISODate("2019-03-12T06:03:10.818Z"),
	"createdAt" : ISODate("2019-03-12T06:03:10.818Z")
}