Skip to content

Commit 48df126

Browse files
authored
Merge pull request #1078 from opencrvs/ocrvs-10682
Check Postgres backup is available before wiping and restoring
2 parents bbeedd2 + 4e3c716 commit 48df126

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

infrastructure/backups/restore.sh

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,17 @@ db.getSiblingDB('webhooks').dropDatabase();"
207207
# ------ POSTGRESQL -------
208208
##
209209

210-
docker run --rm \
211-
-e PGPASSWORD=$POSTGRES_PASSWORD \
212-
--network=$NETWORK \
213-
postgres:17.6 \
214-
bash -c "psql -h postgres -U $POSTGRES_USER -c 'DROP DATABASE IF EXISTS events;'"
210+
# Check if PostgreSQL backup exists before dropping database
211+
if [ -f "$ROOT_PATH/backups/postgres/events-${LABEL}.dump" ]; then
212+
echo "PostgreSQL backup found. Dropping existing events database..."
213+
docker run --rm \
214+
-e PGPASSWORD=$POSTGRES_PASSWORD \
215+
--network=$NETWORK \
216+
postgres:17.6 \
217+
bash -c "psql -h postgres -U $POSTGRES_USER -c 'DROP DATABASE IF EXISTS events;'"
218+
else
219+
echo "PostgreSQL backup not found for label ${LABEL}. Skipping PostgreSQL database drop..."
220+
fi
215221

216222
#####
217223
#
@@ -238,13 +244,18 @@ docker run --rm -v $ROOT_PATH/backups/mongo:/data/backups/mongo --network=$NETWO
238244
# ------ POSTGRESQL -------
239245
##
240246

241-
echo "Restoring PostgreSQL 'events' database"
242-
docker run --rm \
243-
-e PGPASSWORD=$POSTGRES_PASSWORD \
244-
-v $ROOT_PATH/backups/postgres:/backups \
245-
--network=$NETWORK \
246-
postgres:17.6 \
247-
bash -c "createdb -h postgres -U $POSTGRES_USER events && pg_restore -h postgres -U $POSTGRES_USER -d events /backups/events-${LABEL}.dump"
247+
# Check if PostgreSQL backup exists before restoring
248+
if [ -f "$ROOT_PATH/backups/postgres/events-${LABEL}.dump" ]; then
249+
echo "PostgreSQL backup found. Restoring PostgreSQL 'events' database..."
250+
docker run --rm \
251+
-e PGPASSWORD=$POSTGRES_PASSWORD \
252+
-v $ROOT_PATH/backups/postgres:/backups \
253+
--network=$NETWORK \
254+
postgres:17.6 \
255+
bash -c "createdb -h postgres -U $POSTGRES_USER events && pg_restore -h postgres -U $POSTGRES_USER -d events /backups/events-${LABEL}.dump"
256+
else
257+
echo "PostgreSQL backup not found for label ${LABEL}. Skipping PostgreSQL database restore..."
258+
fi
248259

249260
##
250261
# ------ ELASTICSEARCH -----

0 commit comments

Comments
 (0)