Create neo4j databse from backup inside neo4j dock

2019-02-10 17:32发布

问题:

Neo4j is new to me. I have a backup of neo4j database and I would like to build a docker container by creating a database by using that backup.

I know I can use neo4j-admin restore --from=<backup-directory> [--database=<name>] [--force[=<true|false>]] command but am looking for something which a docker container would be able to use to recreate database when the container is created.

The documentation of the neo4j docker image uses an exiting database database inside container. But I need to restore a backup and create database from it.

回答1:

EXTENSION_SCRIPT official image hook

Neo4j's official image provides a hook so you can load data on startup. For that, you must define an environment variable named EXTENSION_SCRIPT at runtime, which points to your database restore script to run (See https://neo4j.com/developer/docker-23/).

Here is an example using docker-compose (this could also be done with a Dockerfile):

docker-compose.yml file :

version: '2'
services:
  neo4j:
    image: neo4j:3.2
    ports:
     - "7474:7474"
     - "7687:7687"
    environment:
     - EXTENSION_SCRIPT=/neo4j-data/neo4j-init.sh :
    volumes:
     - ./neo4j-data:/neo4j-data

Then, in your initialization script, you must restore the database once, the first time

neo4j-init.sh file :

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# do not run init script at each container strat but only at the first start
if [ ! -f /tmp/neo4j-import-done.flag ]; then
    /var/lib/neo4j/bin/neo4j-admin neo4j-admin restore --from=<backup-directory mount as a docker volume under /neo4j-data> [--database=<name>] [--force[=<true|false>]]
    touch /tmp/neo4j-import-done.flag
else
    echo "The import has already been made."
fi


标签: docker neo4j