Merge pull request #6893 from Ocelot-Social-Community/6812-script-auto-backups-for-kubernetes-servers--database-name-as-variable

6812 script auto backups for kubernetes servers  database name as variable
This commit is contained in:
Wolfgang Huß 2023-12-12 16:32:42 +01:00 committed by GitHub
commit 7f8ac262ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 16 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ yarn-error.log*
.yarn-integrity .yarn-integrity
.eslintcache .eslintcache
kubeconfig.yaml kubeconfig.yaml
backup-cron-job.log
node_modules/ node_modules/
cypress/videos cypress/videos

View File

@ -109,7 +109,7 @@ The backups will be saved into your networks folders `backup` folder in a new fo
⚠️ *Attention: Please check carefully whether really the oldest backups have been deleted. As shells on different systems behave differently with regard to the commands used in this script.* ⚠️ *Attention: Please check carefully whether really the oldest backups have been deleted. As shells on different systems behave differently with regard to the commands used in this script.*
Install automated backups by a cron job. Install automated backups by a [cron job](https://en.wikipedia.org/wiki/Cron).
Be aware of having the bash shell installed to run the script. Be aware of having the bash shell installed to run the script.
The environment variables for the automated backups are described above. The environment variables for the automated backups are described above.
@ -131,3 +131,7 @@ In the editor add the line:
``` ```
This way the terminal output is written into a log file named `backup-cron-job.log` located in the deployment folder. This way the terminal output is written into a log file named `backup-cron-job.log` located in the deployment folder.
Be aware that the server datetime can differ from your local time.
Especially by the change between summer and winter time, because servers usually have UTC.
Find out the actual difference by running the command `date` on your server.

View File

@ -9,7 +9,7 @@ SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname $SCRIPT_PATH) SCRIPT_DIR=$(dirname $SCRIPT_PATH)
# check CONFIGURATION # check CONFIGURATION
if [[ -z ${CONFIGURATION} ]]; then if [[ -z "$CONFIGURATION" ]] || [[ $CONFIGURATION == "" ]]; then
echo "!!! You must provide a CONFIGURATION via environment variable !!!" echo "!!! You must provide a CONFIGURATION via environment variable !!!"
exit 1 exit 1
fi fi
@ -27,13 +27,16 @@ mkdir -p ${BACKUP_FOLDER}
${SCRIPT_DIR}/cluster.neo4j.sh maintenance on ${SCRIPT_DIR}/cluster.neo4j.sh maintenance on
# database backup # database backup
echo "Dumping database ..."
kubectl --kubeconfig=${KUBECONFIG} -n default exec -it \ kubectl --kubeconfig=${KUBECONFIG} -n default exec -it \
$(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') \ $(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') \
-- neo4j-admin dump --database=neo4j --to=/var/lib/neo4j/$BACKUP_DATE-neo4j-dump -- neo4j-admin dump --to=/var/lib/neo4j/$BACKUP_DATE-neo4j-dump
# copy neo4j backup to local drive # copy neo4j backup to local drive
echo "Coping database ..."
kubectl --kubeconfig=${KUBECONFIG} cp \ kubectl --kubeconfig=${KUBECONFIG} cp \
default/$(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-neo4j |awk '{ print $1 }'):/var/lib/neo4j/$BACKUP_DATE-neo4j-dump $BACKUP_FOLDER/neo4j-dump default/$(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-neo4j |awk '{ print $1 }'):/var/lib/neo4j/$BACKUP_DATE-neo4j-dump $BACKUP_FOLDER/neo4j-dump
# copy image data # copy image data
echo "Coping public uploads ..."
kubectl --kubeconfig=${KUBECONFIG} cp \ kubectl --kubeconfig=${KUBECONFIG} cp \
default/$(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-backend |awk '{ print $1 }'):/app/public/uploads $BACKUP_FOLDER/public-uploads default/$(kubectl --kubeconfig=${KUBECONFIG} -n default get pods | grep ocelot-backend |awk '{ print $1 }'):/app/public/uploads $BACKUP_FOLDER/public-uploads

View File

@ -5,7 +5,7 @@ SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname $SCRIPT_PATH) SCRIPT_DIR=$(dirname $SCRIPT_PATH)
# check CONFIGURATION # check CONFIGURATION
if [ -z ${CONFIGURATION} ]; then if [[ -z "$CONFIGURATION" ]] || [[ $CONFIGURATION == "" ]]; then
echo "You must provide a `CONFIGURATION` via environment variable" echo "You must provide a `CONFIGURATION` via environment variable"
exit 1 exit 1
fi fi
@ -16,9 +16,11 @@ KUBECONFIG=${KUBECONFIG:-${SCRIPT_DIR}/../configurations/${CONFIGURATION}/kubeco
case $1 in case $1 in
on) on)
echo "Network maintenance: on"
kubectl --kubeconfig=${KUBECONFIG} patch ingress ingress-ocelot-webapp --type merge --patch-file ${SCRIPT_DIR}/../src/kubernetes/patches/patch.ingress.maintenance.on.yaml kubectl --kubeconfig=${KUBECONFIG} patch ingress ingress-ocelot-webapp --type merge --patch-file ${SCRIPT_DIR}/../src/kubernetes/patches/patch.ingress.maintenance.on.yaml
;; ;;
off) off)
echo "Network maintenance: off"
kubectl --kubeconfig=${KUBECONFIG} patch ingress ingress-ocelot-webapp --type merge --patch-file ${SCRIPT_DIR}/../src/kubernetes/patches/patch.ingress.maintenance.off.yaml kubectl --kubeconfig=${KUBECONFIG} patch ingress ingress-ocelot-webapp --type merge --patch-file ${SCRIPT_DIR}/../src/kubernetes/patches/patch.ingress.maintenance.off.yaml
;; ;;
*) *)

View File

@ -5,7 +5,7 @@ SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname $SCRIPT_PATH) SCRIPT_DIR=$(dirname $SCRIPT_PATH)
# check CONFIGURATION # check CONFIGURATION
if [ -z ${CONFIGURATION} ]; then if [[ -z "$CONFIGURATION" ]] || [[ $CONFIGURATION == "" ]]; then
echo "You must provide a `CONFIGURATION` via environment variable" echo "You must provide a `CONFIGURATION` via environment variable"
exit 1 exit 1
fi fi
@ -21,20 +21,24 @@ case $1 in
${SCRIPT_DIR}/cluster.maintenance.sh on ${SCRIPT_DIR}/cluster.maintenance.sh on
# set Neo4j in offline mode (maintenance) # set Neo4j in offline mode (maintenance)
echo "Neo4j maintenance: on"
kubectl --kubeconfig=${KUBECONFIG} get deployment ocelot-neo4j -o json \ kubectl --kubeconfig=${KUBECONFIG} get deployment ocelot-neo4j -o json \
| jq '.spec.template.spec.containers[] += {"command": ["tail", "-f", "/dev/null"]}' \ | jq '.spec.template.spec.containers[] += {"command": ["tail", "-f", "/dev/null"]}' \
| kubectl --kubeconfig=${KUBECONFIG} apply -f - | kubectl --kubeconfig=${KUBECONFIG} apply -f -
# wait for the container to restart # wait for the container to restart
echo "Wait 60s ..."
sleep 60 sleep 60
;; ;;
off) off)
# set Neo4j in online mode # set Neo4j in online mode
echo "Neo4j maintenance: off"
kubectl --kubeconfig=${KUBECONFIG} get deployment ocelot-neo4j -o json \ kubectl --kubeconfig=${KUBECONFIG} get deployment ocelot-neo4j -o json \
| jq 'del(.spec.template.spec.containers[].command)' \ | jq 'del(.spec.template.spec.containers[].command)' \
| kubectl --kubeconfig=${KUBECONFIG} apply -f - | kubectl --kubeconfig=${KUBECONFIG} apply -f -
# wait for the container to restart # wait for the container to restart
echo "Wait 60s ..."
sleep 60 sleep 60
# maintenance mode off # maintenance mode off

View File

@ -17,7 +17,7 @@ source ${SCRIPT_DIR}/../.env
set +a set +a
# check BACKUP_CONFIGURATIONS # check BACKUP_CONFIGURATIONS
if [[ -z ${BACKUP_CONFIGURATIONS} ]]; then if [[ -z "$BACKUP_CONFIGURATIONS" ]] || [[ $BACKUP_CONFIGURATIONS == "" ]]; then
#%! echo "You must provide a BACKUP_CONFIGURATIONS via environment variable" #%! echo "You must provide a BACKUP_CONFIGURATIONS via environment variable"
printf "!!! You must provide a BACKUP_CONFIGURATIONS via environment variable !!!\n" printf "!!! You must provide a BACKUP_CONFIGURATIONS via environment variable !!!\n"
exit 1 exit 1

View File

@ -137,7 +137,7 @@ $ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-neo4j
# bash: enter bash of Neo4j # bash: enter bash of Neo4j
$ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') -- bash $ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') -- bash
# generate Dump # generate Dump
neo4j% neo4j-admin dump --database=neo4j --to=/var/lib/neo4j/$(date +%F)-neo4j-dump neo4j% neo4j-admin dump --to=/var/lib/neo4j/$(date +%F)-neo4j-dump
# exit bash # exit bash
neo4j% exit neo4j% exit
@ -145,6 +145,8 @@ neo4j% exit
$ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') -- ls $ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-neo4j | awk '{ print $1 }') -- ls
``` ```
If you need a specific database name, add the option `--database=<name>` to the command `neo4j-admin dump`.
Lets copy the dump backup Lets copy the dump backup
```bash ```bash

View File

@ -111,10 +111,12 @@ services:
# settings reference: https://neo4j.com/docs/operations-manual/4.4/docker/ref-settings/ # settings reference: https://neo4j.com/docs/operations-manual/4.4/docker/ref-settings/
# TODO: This sounds scary for a production environment # TODO: This sounds scary for a production environment
- NEO4J_AUTH=none - NEO4J_AUTH=none
# - NEO4J_dbms_default__database=graph.db
# - NEO4J_dbms_default__database=neo4j
- NEO4J_dbms_security_procedures_unrestricted=algo.*,apoc.* - NEO4J_dbms_security_procedures_unrestricted=algo.*,apoc.*
- NEO4J_dbms_allow__format__migration=true - NEO4J_dbms_allow__format__migration=true
- NEO4J_dbms_allow__upgrade=true - NEO4J_dbms_allow__upgrade=true
# Uncomment following line for Neo4j Enterprise version instead of Community version # uncomment following line for Neo4j Enterprise version instead of Community version
# TODO: clarify if that is the only thing needed to unlock the Enterprise version # TODO: clarify if that is the only thing needed to unlock the Enterprise version
# - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes # - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
# TODO: Remove the playground from production # TODO: Remove the playground from production

View File

@ -1,4 +1,4 @@
# Neo4J # Neo4j 4.4
Human Connection is a social network. Using a graph based database which can Human Connection is a social network. Using a graph based database which can
model nodes and edges natively - a network - feels like an obvious choice. We model nodes and edges natively - a network - feels like an obvious choice. We
@ -16,8 +16,7 @@ docker-compose up
``` ```
You can access Neo4J through [http://localhost:7474/](http://localhost:7474/) You can access Neo4J through [http://localhost:7474/](http://localhost:7474/)
for an interactive cypher shell and a visualization of the graph. for an interactive Cypher shell and a visualization of the graph.
## Installation Without Docker ## Installation Without Docker
@ -51,7 +50,7 @@ in `backend/.env`.
Start Neo4J and confirm the database is running at [http://localhost:7474](http://localhost:7474). Start Neo4J and confirm the database is running at [http://localhost:7474](http://localhost:7474).
## Operations on Neo4j ## Operations on Neo4j 4.4
### Docker or Docker Compose ### Docker or Docker Compose
@ -68,13 +67,15 @@ To create a dump in Neo4j running in a Docker container:
# connect to the Docker containers Neo4j terminal # connect to the Docker containers Neo4j terminal
$ docker exec -it neo4j bash $ docker exec -it neo4j bash
# generate Dump # generate Dump
neo4j% neo4j-admin dump --database=neo4j --to=/var/lib/neo4j/$(date +%F)-neo4j-dump neo4j% neo4j-admin dump --to=/var/lib/neo4j/$(date +%F)-neo4j-dump
# exit bash # exit bash
neo4j% exit neo4j% exit
# copy the dump out of the running Docker container # copy the dump out of the running Docker container
$ docker cp <docker-image-name('neo4j')>:/var/lib/neo4j/neo4j-dump <local-folder-path>/$(date +%F)-neo4j-dump $ docker cp <docker-image-name('neo4j')>:/var/lib/neo4j/neo4j-dump <local-folder-path>/$(date +%F)-neo4j-dump
``` ```
If you need a specific database name, add the option `--database=<name>` to the command `neo4j-admin dump`.
### Import Neo4j Dump ### Import Neo4j Dump
To import a dump into Neo4j running in a Docker container: To import a dump into Neo4j running in a Docker container:
@ -88,11 +89,13 @@ $ docker cp <local-folder-path>/neo4j-dump <docker-image-name('neo4j')>:/var/lib
# connect to the Docker containers Neo4j terminal # connect to the Docker containers Neo4j terminal
$ docker exec -it neo4j bash $ docker exec -it neo4j bash
# to load the dump into the database we need the following command in this terminal # to load the dump into the database we need the following command in this terminal
neo4j% neo4j-admin load --expand-commands --database=neo4j --from /var/lib/neo4j/$(date +%F)-neo4j-dump --force neo4j% neo4j-admin load --from /var/lib/neo4j/$(date +%F)-neo4j-dump --force
# leave the terminal by entering # leave the terminal by entering
neo4j% exit neo4j% exit
``` ```
If you need a specific database name, add the option `--database=<name>` to the command `neo4j-admin load`.
## Commands ## Commands
Here we describe some rarely used Cypher commands for Neo4j that are needed from time to time: Here we describe some rarely used Cypher commands for Neo4j that are needed from time to time:
@ -143,7 +146,7 @@ $ kubectl -n default exec -it $(kubectl -n default get pods | grep ocelot-backen
***Cypher commands to show indexes and constraints*** ***Cypher commands to show indexes and constraints***
```bash ```bash
# in browser command line or cypher shell # in browser command line or Cypher shell
# show all indexes and constraints # show all indexes and constraints
$ :schema $ :schema
@ -158,7 +161,7 @@ $ CALL db.constraints();
***Cypher commands to create and drop indexes and constraints*** ***Cypher commands to create and drop indexes and constraints***
```bash ```bash
# in browser command line or cypher shell # in browser command line or Cypher shell
# create indexes # create indexes
$ CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"]); $ CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"]);
@ -171,3 +174,21 @@ $ DROP CONSTRAINT ON ( image:Image ) ASSERT image.url IS UNIQUE
# drop all indexes and constraints # drop all indexes and constraints
$ CALL apoc.schema.assert({},{},true) YIELD label, key RETURN * ; $ CALL apoc.schema.assert({},{},true) YIELD label, key RETURN * ;
``` ```
### Database Management Commands
***Cypher commands to manage databases***
```bash
# in browser command line or Cypher shell
# show the default database
$ SHOW DEFAULT DATABASE
# show all databases
$ SHOW DATABASES
```
To set the default database by configuration, use `NEO4J_dbms_default__database` as an environment variable when starting Neo4j 4.4, see [Docker specific configuration settings](<https://neo4j.com/docs/operations-manual/4.4/docker/ref-settings/>).
If a database with this name does not exist, an empty database with this name is created and all other databases remain.
You can switch back to an existing database without damaging it.