Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit 65e502d

Browse files
author
Dmitriy "DK" Korobskiy
committed
* Split script for switching DBs
1 parent 267162e commit 65e502d

2 files changed

Lines changed: 67 additions & 22 deletions

File tree

Neo4j/neo4j_load.sh

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ set -xe
2222
set -o pipefail
2323

2424
# Get a script directory, same as by $(dirname $0)
25-
#script_dir=${0%/*}
26-
#absolute_script_dir=$(cd "${script_dir}" && pwd)
25+
script_dir=${0%/*}
26+
absolute_script_dir=$(cd "${script_dir}" && pwd)
2727
#work_dir=${1:-${absolute_script_dir}/build} # $1 with the default
2828
#if [[ ! -d "${work_dir}" ]]; then
2929
# mkdir "${work_dir}"
@@ -32,10 +32,10 @@ set -o pipefail
3232
#cd "${work_dir}"
3333
echo -e "\n## Running under ${USER}@${HOSTNAME} at ${PWD} ##\n"
3434

35-
#if ! which cypher-shell >/dev/null; then
36-
# echo "Please install Neo4j"
37-
# exit 1
38-
#fi
35+
if ! which cypher-shell >/dev/null; then
36+
echo "Please install Neo4j"
37+
exit 1
38+
fi
3939

4040
nodes_file="$1"
4141
edges_file="$2"
@@ -63,22 +63,9 @@ set +x
6363
# `java.io.FileNotFoundException: import.report (Permission denied)` error
6464
echo "$3" | sudo --stdin -u neo4j bash -c "set -xe
6565
echo 'Loading data into ${db_name} ...'
66-
neo4j-admin import --nodes:Publication '${nodes_file}' --relationships:CITES '${edges_file}' --database="${db_name}"
67-
sed --in-place --expression='s/dbms.active_database=.*/dbms.active_database=${db_name}/' /etc/neo4j/neo4j.conf"
68-
69-
echo "Restarting Neo4j with a new active database ..."
70-
echo "$3" | sudo --stdin systemctl restart neo4j
71-
72-
declare -i time_limit_s=30
73-
echo "Waiting for the service to become active up to ${time_limit_s} seconds ..."
74-
# Ping Neo4j. Even if a service is active it might not be responding yet.
75-
while ! cypher-shell "CALL dbms.components()" 2>/dev/null; do
76-
if ((time_limit_s-- == 0)); then
77-
echo "ERROR: Neo4j failed to start." >&2
78-
exit 2
79-
fi
80-
sleep 1
81-
done
66+
neo4j-admin import --nodes:Publication '${nodes_file}' --relationships:CITES '${edges_file}' --database='${db_name}'"
67+
68+
${absolute_script_dir}/neo4j_switch_db.sh "${db_name}" "$3"
8269
set -x
8370
# endregion
8471

Neo4j/neo4j_switch_db.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
if [[ $1 == "-h" || $# -lt 1 ]]; then
3+
cat <<'HEREDOC'
4+
NAME
5+
neo4j_switch_db.sh -- switches Neo4j active DB and restarts Neo4j
6+
7+
SYNOPSIS
8+
neo4j_load.sh neo4j_db current_user_password
9+
neo4j_load.sh -h: display this help
10+
11+
DESCRIPTION
12+
# See available DBs in `/var/lib/neo4j/data/databases`
13+
# Current user must be a sudoer
14+
HEREDOC
15+
exit 1
16+
fi
17+
18+
set -xe
19+
set -o pipefail
20+
21+
# Get a script directory, same as by $(dirname $0)
22+
#script_dir=${0%/*}
23+
#absolute_script_dir=$(cd "${script_dir}" && pwd)
24+
#work_dir=${1:-${absolute_script_dir}/build} # $1 with the default
25+
#if [[ ! -d "${work_dir}" ]]; then
26+
# mkdir "${work_dir}"
27+
# chmod g+w "${work_dir}"
28+
#fi
29+
#cd "${work_dir}"
30+
echo -e "\n## Running under ${USER}@${HOSTNAME} at ${PWD} ##\n"
31+
32+
if ! which cypher-shell >/dev/null; then
33+
echo "Please install Neo4j"
34+
exit 1
35+
fi
36+
37+
db_name="$1"
38+
39+
# region Hide password from the output
40+
set +x
41+
echo "$3" | sudo --stdin -u neo4j bash -c "set -xe
42+
sed --in-place --expression='s/dbms.active_database=.*/dbms.active_database=${db_name}/' /etc/neo4j/neo4j.conf"
43+
44+
echo "Restarting Neo4j with a new active database ..."
45+
echo "$3" | sudo --stdin systemctl restart neo4j
46+
47+
declare -i time_limit_s=30
48+
echo "Waiting for the service to become active up to ${time_limit_s} seconds ..."
49+
# Ping Neo4j. Even if a service is active it might not be responding yet.
50+
while ! cypher-shell "CALL dbms.components()" 2>/dev/null; do
51+
if ((time_limit_s-- == 0)); then
52+
echo "ERROR: Neo4j failed to start." >&2
53+
exit 2
54+
fi
55+
sleep 1
56+
done
57+
set -x
58+
# endregion

0 commit comments

Comments
 (0)