Skip to content

Commit 7a6badb

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into develop
2 parents 2151bec + 899701d commit 7a6badb

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

infrastructure/backups/backup.sh

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ elasticsearch_host() {
177177
fi
178178
}
179179

180+
get_target_indices() {
181+
docker run --rm --network=$NETWORK appropriate/curl curl -s "http://$(elasticsearch_host)/_cat/indices?h=index" \
182+
| grep -E '^(ocrvs-|events_)' \
183+
| paste -sd, - \
184+
| sed 's/\,$//'
185+
}
186+
180187
# Today's date is used for filenames if LABEL is not provided
181188
#-----------------------------------
182189
BACKUP_DATE=$(date +%Y-%m-%d)
@@ -201,17 +208,6 @@ done
201208
# Backup PostgreSQL
202209
# -----------------
203210

204-
echo "Backing up PostgreSQL 'events' database"
205-
docker run --rm \
206-
-e PGPASSWORD=$POSTGRES_PASSWORD \
207-
-v $ROOT_PATH/backups/postgres:/backups \
208-
--network=$NETWORK \
209-
postgres:17 \
210-
bash -c "pg_dump -h postgres -U $POSTGRES_USER -d events -F c -f /backups/events-${LABEL:-$BACKUP_DATE}.dump"
211-
212-
# Backup PostgreSQL
213-
# -----------------
214-
215211
echo "Backing up PostgreSQL 'events' database"
216212
docker run --rm \
217213
-e PGPASSWORD=$POSTGRES_PASSWORD \
@@ -261,8 +257,12 @@ echo "Backup Elasticsearch as a set of snapshot files into an elasticsearch sub
261257
echo ""
262258

263259
create_elasticsearch_backup() {
260+
indices=$(get_target_indices)
261+
echo "List indices for backup: $indices"
264262
OUTPUT=""
265-
OUTPUT=$(docker run --rm --network=$NETWORK appropriate/curl curl -sS -X PUT -H "Content-Type: application/json;charset=UTF-8" "http://$(elasticsearch_host)/_snapshot/ocrvs/snapshot_${LABEL:-$BACKUP_DATE}?wait_for_completion=true&pretty" -d '{ "indices": "ocrvs" }' 2>/dev/null)
263+
json_payload="{\"indices\": \"${indices}\"}"
264+
OUTPUT=$(docker run --rm --network=$NETWORK appropriate/curl curl -sS -X PUT -H "Content-Type: application/json;charset=UTF-8" "http://$(elasticsearch_host)/_snapshot/ocrvs/snapshot_${LABEL:-$BACKUP_DATE}?wait_for_completion=true&pretty" -d "$json_payload" 2>/dev/null) || true
265+
266266
if echo $OUTPUT | jq -e '.snapshot.state == "SUCCESS"' > /dev/null; then
267267
echo "Snapshot state is SUCCESS"
268268
else
@@ -343,4 +343,3 @@ fi
343343
rm /tmp/${LABEL:-$BACKUP_DATE}.tar.gz.enc
344344
rm /tmp/${LABEL:-$BACKUP_DATE}.tar.gz
345345
rm -r $BACKUP_RAW_FILES_DIR
346-

infrastructure/backups/restore.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,25 @@ elasticsearch_host() {
139139

140140
echo "delete any previously created snapshot if any. This may error on a fresh install with a repository_missing_exception error. Just ignore it."
141141
docker run --rm --network=$NETWORK appropriate/curl curl -X DELETE "http://$(elasticsearch_host)/_snapshot/ocrvs"
142-
docker run --rm --network=$NETWORK appropriate/curl curl -X DELETE "http://$(elasticsearch_host)/*" -v
142+
143+
# Delete all data from elasticsearch
144+
#-----------------------------------
145+
approved_words=${ES_INDEX_PREFIXES:-"events_ ocrvs-"}
146+
indices=$(docker run --rm --network=$NETWORK appropriate/curl curl -sS -XGET "http://$(elasticsearch_host)/_cat/indices?h=index")
147+
echo "--------------------------"
148+
echo "🧹 cleanup for indices: $approved_words from $indices"
149+
echo "--------------------------"
150+
for index in ${indices[@]}; do
151+
for approved in $approved_words; do
152+
case "$index" in
153+
"$approved_words"*)
154+
echo "Delete index $index..."
155+
docker run --rm --network=$NETWORK appropriate/curl curl -sS -XDELETE "http://$(elasticsearch_host)/$index"
156+
break
157+
;;
158+
esac
159+
done
160+
done
143161

144162
echo "Waiting for elasticsearch to restart so that the restore script can find the updated volume."
145163
docker service update --force --update-parallelism 1 --update-delay 30s opencrvs_elasticsearch
@@ -239,7 +257,8 @@ sleep 10
239257

240258
# Restore all data from a backup into search
241259
#-------------------------------------------
242-
docker run --rm --network=$NETWORK appropriate/curl curl -X POST -H "Content-Type: application/json;charset=UTF-8" "http://$(elasticsearch_host)/_snapshot/ocrvs/snapshot_$LABEL/_restore?pretty" -d '{ "indices": "ocrvs" }'
260+
json_payload="{\"indices\": \"ocrvs-*,events_*\", \"include_global_state\": false}"
261+
docker run --rm --network=$NETWORK appropriate/curl curl -X POST -H "Content-Type: application/json;charset=UTF-8" "http://$(elasticsearch_host)/_snapshot/ocrvs/snapshot_$LABEL/_restore?pretty" -d "$json_payload"
243262
sleep 10
244263
echo "Waiting 1 minute to rotate elasticsearch passwords"
245264
echo

0 commit comments

Comments
 (0)