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- # -----------------------------------
1414BACKUP_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
1829apk add --no-cache bash curl openssl openssh jq
30+
1931echo " [$( date +%F\ %H:%M:%S) ] Waiting for Elasticsearch container"
20- sleep 3
32+ sleep ${TIMEOUT:- " 300" }
33+
2134echo " [$( 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
2539elasticsearch_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_
3348get_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.
5771create_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+
91117echo " List snapshots"
92118curl -s " http://$( elasticsearch_host) /_cat/snapshots/ocrvs?h=id"
93119
@@ -107,5 +133,8 @@ echo ""
107133echo " [$( date +%F\ %H:%M:%S) ] Backup Elasticsearch as a set of snapshot files into an elasticsearch sub folder"
108134echo " "
109135create_elasticsearch_backup
136+
137+ create_encrypted_backup
138+
110139sleep 86400
111140done
0 commit comments