Docker下如何导入导出neo4j数据

neo4j
neo4j

[摘要] 运行在Docker容器中的neo4j数据库,如何使用 neo4j-admin restore|dump 进行导入导出操作?

neo4j 官方文档有说明,使用 neo4j-admin restore / dump 导出和恢复数据库的时候需要停掉数据,否则会报数据库正在使用的错误:
command failed: the database is in use — stop Neo4j and try again

官方文档这样说:
Restore the database graph.db from the backup located in /mnt/backup/graph.db-backup. Note that the database to be restored must be shut down.

neo4j-home> bin/neo4j stop
neo4j-home> bin/neo4j-admin restore --from=/mnt/backup/graph.db-backup --database=graph.db --force
neo4j-home> bin/neo4j start

但是问题来了,容器里面entrypoint默认是启动的neo4j的进程:

# ps aux|grep neo4j
1 root      0:01 /sbin/tini -g -- /docker-entrypoint.sh neo4j
7 neo4j     3:16 /usr/lib/jvm/java-1.8-openjdk/jre/bin/java -cp /var/lib/neo4j/plugins:/var/lib/neo4j/conf:/var/lib/neo4j/lib/*:/var/lib/neo4j/plugins/* -server -Xms512M -Xmx512M -XX:+UseG1GC -XX:-OmitStackTraceInFastThrow -XX:+AlwaysPreTouch -XX:+UnlockExperimentalVMOptions -XX:+TrustFinalNonStaticFields -XX:+DisableExplicitGC -Djdk.tls.ephemeralDHKeySize=2048 -Djdk.tls.rejectClientInitiatedRenegotiation=true -Dunsupported.dbms.udc.source=tarball -Dfile.encoding=UTF-8 org.neo4j.server.CommunityEntryPoint --home-dir=/var/lib/neo4j --config-dir=/var/lib/neo4j/conf

现在进入容器shudown的话,neo4j容器会停掉(docker-run)或者重启数据库(docker-compose),所以这里采用的迂回的方法:

(1)停掉neo4j容器

docker stop neo4j-container

(2)使用docker run启动一个带有TTY新的容器,如果使用了数据持久化,需要使用-v参数带上volume

docker run \
-v /data/deploy/nlpdemo/neo4j-data-persistence:/data \
--name neo4j-container-dump \
-it \
neo4j /bin/bash

(3)导入或者导出数据库

#dump
neo4j-admin dump --database=graph.db --to=/backups/graph.db/2019-01-10.dump

#restore
neo4j-admin load --from=/backups/graph.db/2019-01-10.dump --database=graph.db --force

(4)关闭临时容器,重启原容器