-
Notifications
You must be signed in to change notification settings - Fork 3
/
sbml4j.sh
executable file
·168 lines (152 loc) · 7.4 KB
/
sbml4j.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Default volume name should be prefixed with folder name
default_volume_prefix="$(echo ${PWD##*/} | tr '[A-Z]' '[a-z]')"
function show_usage() {
echo "Usage: "
echo "${0} -h | -b | -i | -a | -r | -p {argument}"
echo "Details:"
echo "This script is used to either setup the SBML4J volumes, setup the database from database dumps, or backup the databases into a database dump."
echo "You can either use any one option alone, or use the -i and -r options together."
echo " -h : Print this help"
echo " -b {argument} :"
echo " Backup the current database into the backup files named by the {argument}"
echo " -i :"
echo " Install prerequisits for SBML4J."
echo " This will recreate the volumes used for SBML4J and the (empty) neo4j database."
echo " -r {argument} :"
echo " Restore the neo4j database from the database dumps in the files with prefix given by the {argument}"
echo " -p {argument} :"
echo " Use {argument} as prefix for the volumes created, i.e. argument_sbml4j_service_vol"
echo " Use in conjuction with the -b, -i, -r flags."
echo " "
echo "Examples:"
echo "${0} -i :"
echo " This will (re)-create the volumes for SBML4J and use the default api definition file ${default_api_def} as source for the api page"
echo "${0} -b mydbbackup"
echo " This will create a database dump of the neo4j and system database in the files mydbbackup-neo4j.dump and mydbbackup-system.dump respectively"
echo "${0} -r mydbbackup"
echo " This will resore a database dump from the neo4j and system database dump files mydbbackup-neo4j.dump and mydbbackup-system.dump."
echo " WARNING: Any data currently stored in the database will be overwritten"
echo "${0} -i -r mydbbackup"
echo " This will (re-)create the volumes for SBML4J (as described above) and load the database backup from the database dunmp files as described above."
echo "${0} -i -r mydbbackup -p my-compose"
echo " This will (re-)create the volumes with names my-compose_sbml4j_neo4j_vol instead of the default name ${default_volume_prefix}_sbml4j_neo4j_vol (derived from the current directory) for SBML4J (as described above) and load the database backup from the database dunmp files as described above."
echo " This needs to be used when you want to use these volumes in a different compose setup"
}
function install() {
prefix_name=$1
# Steps to do:
# Start a docker container running a shell
# mount the /vol dir to create all prerequisits for neo4j
# run script inside container that
# creates plugin dir
# downloads apoc plugin
# creates logs dir
# creates data dir
# creates conf dir
# copies conf file in volume
# fixes permissions
echo "Creating volume for neo4j database"
docker run --rm --detach --mount type=bind,src=${PWD}/scripts,dst=/scripts --mount type=bind,src=${PWD}/conf,dst=/conf --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol alpine /scripts/setup_neo4j.sh
# Start a docker container running a shell
# mount the /sbml4j dir to create all prerequisits for sbml4j
# basically create the logs folder in the volume
echo "Creating volume for the sbml4j service"
docker run --rm --detach --mount type=volume,src=${prefix_name}_sbml4j_service_vol,dst=/logs alpine touch /logs/root.log
}
function ensure_db_backups_folder_exists() {
if [ ! -d "${PWD}/db_backups" ]
then
echo "Directory for database backups not found. Creating directory with name 'db_backups' in current folder ${PWD}"
mkdir -p ${PWD}/db_backups
fi
}
function setup_db() {
backup_base_name=$1
prefix_name=$2
ensure_db_backups_folder_exists
# Start neo4j for restoring the neo4j backup (twice: one neo4j, one system)
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin load --from=/backups/${backup_base_name}-neo4j.dump --database=neo4j --force
#
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin load --from=/backups/${backup_base_name}-system.dump --database=system --force
}
function backup_db() {
backup_base_name=$1
prefix_name=$2
ensure_db_backups_folder_exists
# Start neo4j for backing upthe neo4j database (twice: one neo4j, one system)
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin dump --database=neo4j --to=/backups/${backup_base_name}-neo4j.dump
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin dump --database=system --to=/backups/${backup_base_name}-system.dump
}
declare -i i=0
while getopts hb:ir:p: flag
do
case "${flag}" in
h) show_usage
exit 0
;;
b) backup_name=${OPTARG}
do_backup=True
i=i+100
#echo "Performing database backup into file: $backup_name"
;;
i) do_install=True
i=i+1
;;
r) backup_name=${OPTARG}
do_setup=True
#echo "Performing database setup from file: $backup_name"
i=i+10
;;
p) prefix_name=${OPTARG}
is_prefix_set=True
;;
esac
done
echo $i
function check_prefix_name() {
# Do we have a custom prefix set
if [ "$is_prefix_set" = True ]
then
echo "Using custom prefix name ${prefix_name}"
else
echo "Using default prefix name ${default_volume_prefix}"
prefix_name=$default_volume_prefix
fi
}
if [ "$i" -lt "1" ]
then
echo "No argument given. Please give one or two arguments."
show_usage
exit 0
elif [ "$i" -lt "10" ]
then
echo "Performing installation of prerequisits for running sbml4j."
check_prefix_name
install $prefix_name
echo "Successfully installed prerequisists for running sbml4j."
exit 0
elif [ "$i" -lt "11" ]
then
echo "Restoring database from dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
check_prefix_name
setup_db $backup_name $prefix_name
echo "Successfully restored database."
exit 0
elif [ "$i" -lt "12" ]
then
echo "Performing installation of prerequisits for running sbml4j."
check_prefix_name
install $prefix_name
echo "Restoring database from dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
setup_db $backup_name $prefix_name
echo "Successfully installed prerequisits and resored database state."
exit 0
elif [ "$i" -lt "101" ]
then
echo "Performing database backup into dump-files $backup_name-neo4j.dump and $backup_name-system.dump."
check_prefix_name
backup_db $backup_name $prefix_name
echo "Successfully created database backup into dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
exit 0
fi