Skip to content

Commit 62fc817

Browse files
committed
fix
1 parent c250505 commit 62fc817

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

charts/dependencies/files/elasticsearch/backup.sh

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,34 @@
88
# & Healthcare Disclaimer located at http://opencrvs.org/license.
99
#
1010
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11-
ELASTIC_HOST=${ELASTIC_HOST:-"elasticsearch:9200"}
11+
12+
# Initial variables configuration
1213
# Today's date is used for filenames if LABEL is not provided
13-
#-----------------------------------
1414
BACKUP_DATE=$(date +%Y-%m-%d)
15-
REMOTE_DIR="$REMOTE_DIR/${LABEL:-$BACKUP_DATE}"
15+
# Local directory inside container
16+
BACKUP_DIR="/backups"
17+
# Temporal archive path inside container
18+
ARCHIVE_PATH="/tmp/elasticsearch_backup_${LABEL}.tar.gz"
19+
# Remote directory on backup server
20+
REMOTE_DIR="$BACKUP_REMOTE_DIR/${LABEL:-$BACKUP_DATE}"
21+
# Number of retries for backup creation
22+
MAX_RETRIES=10
23+
# Reference to container within the same k8s pod
24+
ELASTIC_HOST=${ELASTIC_HOST:-"elasticsearch:9200"}
25+
# Backup encryption password
26+
ENCRYPT_PASS=${ENCRYPT_PASS:?Must provide ENCRYPT_PASS}
1627

1728
# Install required tools
1829
apk add --no-cache bash curl openssl openssh jq
30+
1931
echo "[$(date +%F\ %H:%M:%S)] Waiting for Elasticsearch container"
20-
sleep 3
32+
sleep ${TIMEOUT:-"300"}
33+
2134
echo "[$(date +%F\ %H:%M:%S)] Running backup container"
22-
set +x
23-
MAX_RETRIES=10
24-
# elasticsearch:9200, is container:port in this case, not pod name or service name
35+
36+
# Hostname for elasticsearch container
37+
# - password protected
38+
# - no-password access
2539
elasticsearch_host() {
2640
if [ ! -z ${ELASTIC_PASSWORD+x} ]; then
2741
echo "elastic:$ELASTIC_PASSWORD@${ELASTIC_HOST}"
@@ -30,6 +44,7 @@ elasticsearch_host() {
3044
fi
3145
}
3246

47+
# List indices on server by patterns ocrvs-|events_
3348
get_target_indices() {
3449
curl -s "http://$(elasticsearch_host)/_cat/indices?h=index" \
3550
| grep -E '^(ocrvs-|events_)' \
@@ -53,11 +68,10 @@ create_elasticsearch_snapshot_repository() {
5368
exit 1
5469
}
5570

56-
# Improved recursive backup function: replaced by loop.
5771
create_elasticsearch_backup() {
5872
local indices=$(get_target_indices)
5973
if [ -z "$indices" ]; then
60-
echo "[$(date +%F\ %H:%M:%S)] No indices matching ocrvs-* or events-* found, skipping snapshot."
74+
echo "[$(date +%F\ %H:%M:%S)] No indices matching ocrvs-* or events_* found, skipping snapshot."
6175
return 1
6276
fi
6377

@@ -88,6 +102,18 @@ delete_all_snapshots() {
88102
done
89103
}
90104

105+
create_encrypted_backup(){
106+
# Tar/gzip all snapshot content
107+
tar czf "$ARCHIVE_PATH" -C "$BACKUP_DIR" .
108+
109+
# Encrypt
110+
openssl enc -aes-256-cbc -pbkdf2 -salt -in "$ARCHIVE_PATH" -out "${ARCHIVE_PATH}.enc" -pass env:ENCRYPT_PASS
111+
112+
# Remove plain file (optional)
113+
rm -f "$ARCHIVE_PATH"
114+
echo "Backup encrypted at ${ARCHIVE_PATH}.enc"
115+
}
116+
91117
echo "List snapshots"
92118
curl -s "http://$(elasticsearch_host)/_cat/snapshots/ocrvs?h=id"
93119

@@ -107,5 +133,8 @@ echo ""
107133
echo "[$(date +%F\ %H:%M:%S)] Backup Elasticsearch as a set of snapshot files into an elasticsearch sub folder"
108134
echo ""
109135
create_elasticsearch_backup
136+
137+
create_encrypted_backup
138+
110139
sleep 86400
111140
done
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if .Values.backup.enabled }}
2+
{{/* Prevent secret mutation between helm upgrades */}}
3+
{{- $secret := lookup "v1" "Secret" .Release.Namespace .Values.backup.backup_encryption_secret }}
4+
{{- if not $secret }}
5+
{{- $backup_encryption_key := randAlphaNum 32 }}
6+
---
7+
apiVersion: v1
8+
kind: Secret
9+
metadata:
10+
name: {{ .Values.backup.backup_encryption_secret }}
11+
annotations:
12+
"helm.sh/hook": pre-install,pre-upgrade
13+
"helm.sh/resource-policy": keep
14+
type: Opaque
15+
data:
16+
backup_encryption_key: {{ $backup_encryption_key | b64enc }}
17+
{{- end }}
18+
{{- end }}

charts/dependencies/templates/elasticsearch.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ spec:
8181
{{- end }}
8282
{{- end }}
8383
initContainers:
84+
# Change ownership to elasticsearch user and group
8485
- name: volume-permission-fix
8586
image: busybox
8687
command:
8788
- chown
8889
- -R
89-
- "1000:0" # Change ownership to elasticsearch user and group
90+
- "1000:0"
9091
- /usr/share/elasticsearch/data
9192
- /data/backups/elasticsearch
9293
volumeMounts:
@@ -96,6 +97,7 @@ spec:
9697
name: elasticsearch-backup
9798
containers:
9899
- env:
100+
# FIXME: Make fields configurable
99101
- name: ES_JAVA_OPTS
100102
value: -Xms2048m -Xmx8192m
101103
- name: cluster.routing.allocation.disk.threshold_enabled
@@ -148,6 +150,21 @@ spec:
148150
name: {{ .Values.elasticsearch.admin_user_secret_name }}
149151
key: ELASTIC_PASSWORD
150152
{{- end }}
153+
- name: ENCRYPT_PASS
154+
valueFrom:
155+
secretKeyRef:
156+
name: {{ .Values.backup.backup_encryption_secret }}
157+
key: backup_encryption_key
158+
- name: BACKUP_USER
159+
valueFrom:
160+
secretKeyRef:
161+
name: backup-ssh-key
162+
key: user
163+
- name: BACKUP_HOST
164+
valueFrom:
165+
secretKeyRef:
166+
name: backup-ssh-key
167+
key: host
151168
volumeMounts:
152169
- name: backup-ssh-key
153170
mountPath: /ssh
@@ -181,6 +198,9 @@ spec:
181198
- name: backup-ssh-key
182199
secret:
183200
secretName: {{ .Values.backup.backup_server_secret }}
201+
items:
202+
- key: ssh_key
203+
path: id_rsa
184204
- name: elasticsearch-scripts
185205
configMap:
186206
name: elasticsearch-scripts

charts/dependencies/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,5 @@ backup:
116116
# - user, backup server username
117117
# - host, hostname / IP of backup server
118118
# - ssh_key, ssh key for passwordless connection
119-
backup_server_secret: backup-ssh-key
119+
backup_server_secret: backup-ssh-key
120+
backup_encryption_secret: backup-encryption-secret

infrastructure/server-setup/backup.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- hosts: backup
1111
become: yes
1212
become_method: sudo
13+
# FIXME: Should be executed only of backup host is defined
1314
tags: backup
1415
tasks:
1516
- name: Ensure backup user is present
@@ -60,6 +61,8 @@
6061
ansible.builtin.command: >
6162
kubectl create secret generic backup-ssh-key --namespace opencrvs-deps-{{ k8s_cluster_env }}
6263
--from-file=ssh_key=/tmp/id_ed25519_backup
64+
--from-literal=user={{ backup_server_user }} \
65+
--from-literal=host={{ hostvars['backup']['ansible_host'] }}
6366
args:
6467
removes: /tmp/id_ed25519_backup
6568
# If you want this to update the secret if it already exists, use kubectl apply or kubectl delete/create

0 commit comments

Comments
 (0)