将mongodb数据导入到mongo映像

时间:2019-06-29 08:27:49

标签: mongodb docker

我有mongo个数据库备份目录。 另外,我从https://hub.docker.com/_/mongo运行了docker数据库的mongo图像

我的目标是使用以下方法将备份数据导入mongo映像:

mongorestore -d <database_name> <directory_backup>

我的项目结构:

├── docker-compose.yml
├── mongo-seed
│   ├── Dockerfile
│   └── backup
│       └── mydb
│           ├── beleagues.bson
│           ├── beleagues.metadata.json
│           ├── bookmakers.bson
│           ├── bookmakers.metadata.json
│           ├── globalstores.bson
├── package.json
└── yarn.lock

mongo-seed / Dockerfile:

1 FROM mongo
2 COPY backup /backup
3 CMD mongorestore -d mydb /backup

docker-compose.yml

1 mongodb:
2   image: mongo
3   ports:
4     - "27017:27017"
5
6 mongo-seed:
7   build: ./mongo-seed
8   links:
9     - mongodb

docker-compose up之后:

➜  project-dit docker-compose up
Building mongo-seed
Step 1/3 : FROM mongo
 ---> a3639b2a0f13
Step 2/3 : COPY backup /backup
 ---> 5a738f2746b6
Step 3/3 : CMD mongorestore -d mydb /backup
 ---> Running in 6a78a27f2214
Removing intermediate container 6a78a27f2214
 ---> 29908e84d056
Successfully built 29908e84d056
Successfully tagged gepick-tests_mongo-seed:latest
WARNING: Image for service mongo-seed was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating myproject-tests_mongodb_1 ... done
Creating myproject-tests_mongo-seed_1 ... done
Attaching to myproject-tests_mongodb_1, myproject-tests_mongo-seed_1
mongodb_1     | 2019-06-29T08:48:15.226+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=5ce234000e63
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] db version v4.0.10
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] modules: none
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] build environment:
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb_1     | 2019-06-29T08:48:15.231+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongodb_1     | 2019-06-29T08:48:15.232+0000 I STORAGE  [initandlisten]
mongodb_1     | 2019-06-29T08:48:15.232+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongodb_1     | 2019-06-29T08:48:15.232+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongodb_1     | 2019-06-29T08:48:15.233+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=487M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongodb_1     | 2019-06-29T08:48:15.657+0000 I STORAGE  [initandlisten] WiredTiger message [1561798095:657331][1:0x7fe1f8d52a80], txn-recover: Set global recovery timestamp: 0
mongodb_1     | 2019-06-29T08:48:15.664+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
mongodb_1     | 2019-06-29T08:48:15.674+0000 I CONTROL  [initandlisten]
mongodb_1     | 2019-06-29T08:48:15.674+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongodb_1     | 2019-06-29T08:48:15.674+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongodb_1     | 2019-06-29T08:48:15.674+0000 I CONTROL  [initandlisten]
mongodb_1     | 2019-06-29T08:48:15.676+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 81dcec38-31d8-438f-b3bd-c55642bafe5e
mongodb_1     | 2019-06-29T08:48:15.684+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
mongodb_1     | 2019-06-29T08:48:15.689+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 713feb5d-26c7-4804-833a-0a01775f5777
mongodb_1     | 2019-06-29T08:48:15.700+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongodb_1     | 2019-06-29T08:48:15.702+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
mongodb_1     | 2019-06-29T08:48:15.705+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 8a8fdc03-f680-4262-8804-33b92a950eb3
mongodb_1     | 2019-06-29T08:48:15.720+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
mongodb_1     | 2019-06-29T08:48:15.720+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongodb_1     | 2019-06-29T08:48:15.723+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs
mongo-seed_1  | 2019-06-29T08:48:19.291+0000    Failed: error connecting to db server: no reachable servers
myproject-tests_mongo-seed_1 exited with code 1
mongodb_1     | 2019-06-29T08:49:27.463+0000 I NETWORK  [listener] connection accepted from 127.0.0.1:54364 #1 (1 connection now open)
mongodb_1     | 2019-06-29T08:49:27.463+0000 I NETWORK  [conn1] received client metadata from 127.0.0.1:54364 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.10" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }

1 个答案:

答案 0 :(得分:0)

您可以查看您提到的图像,这是一种解决方法,但是要处理json集合。只需更正复制命令的路径

copy backup /docker-entrypoint-initdb.d/
  

首次启动容器时,它将执行文件   带有.sh和.js扩展名   /docker-entrypoint-initdb.d。文件将按字母顺序执行   订购。 .js文件将由mongo使用数据库执行   由MONGO_INITDB_DATABASE变量指定(如果存在),或者   否则测试。您也可以在.js脚本中切换数据库。

另一种方法是建立第二个容器,将其与mongo容器链接,然后将备份目录安装到容器上,并在备份完成后删除该容器。

 docker run --rm --link mongodb:mongo -v /root:/backup mongo \
 bash -c ‘mongorestore /backup --host mongo:27017’

使用docker compose更改此部分并添加此命令bash -c ‘mongorestore /backup --host mongo:27017’

6 mongo-seed:
7   build: ./mongo-seed
8   links:
9     - mongodb

或者您也可以在容器内运行命令。

docker exec -i mongo sh -c 'mongorestore --archive' < db.dump