Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech - commande de restauration de la db en dev #876

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dev-run-infra:
dev-dump-db:
sh ./infra/scripts/backup_dev_db.sh

dev-restore-db:
sh ./infra/scripts/restore_dev_db.sh

dev-erase-db:
docker compose \
--project-name $(PROJECT_NAME) \
Expand Down Expand Up @@ -91,9 +94,6 @@ docker-build-app:
--build-arg SENTRY_ORG=$(SENTRY_ORG) \
--build-arg SENTRY_PROJECT=$(SENTRY_PROJECT)

reset:
sh ./infra/scripts/restore_dev_db.sh

# INIT commands
.PHONY: load-sig-data prod-load-sig-data init-geoserver
load-sig-data:
Expand Down
3 changes: 3 additions & 0 deletions infra/docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
services:
db:
volumes:
- "../../.backups/:/opt/monitorenv_backups"
geoserver:
restart: unless-stopped
links:
Expand Down
4 changes: 2 additions & 2 deletions infra/scripts/backup_dev_db.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash
set -e

BACKUP_FILE_PATH="./.backups/$(date '+%Y-%m-%d').sqlc"
BACKUP_FILE_PATH="./.backups/$(date '+%Y-%m-%d').sql"

if [ ! -d ./.backups ]; then
echo "Creating directory './.backups'…"
mkdir ./.backups
fi

echo "Dumping databases in '${BACKUP_FILE_PATH}'…"
docker exec -t monitorenv-db-1 pg_dump -c -F c -U postgres monitorenvdb > "${BACKUP_FILE_PATH}"
docker exec -t monitorenv-db-1 pg_dump -c -F p -U postgres monitorenvdb > "${BACKUP_FILE_PATH}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu n'as pas de soucis d'ordre des requêtes SQL qui sont dumpées lorsqu'elles sont restorées ? J'avais utilisé le format c en espérant que ça évite le problème mais sans succès.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non j'ai pas l'impression. Avec l'option --clean l'ajout des contraintes se fait à la fin et ça semble bien se passer.

22 changes: 1 addition & 21 deletions infra/scripts/restore_dev_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@ set -e
LAST_BACKUP_FILE_NAME=$(ls -p ./.backups | grep -v / | sort -V | tail -n 1)
LAST_BACKUP_FILE_PATH="./.backups/${LAST_BACKUP_FILE_NAME}"

# echo "Stopping and removing database container with its volume…"
# docker compose \
# --project-name monitorenv \
# --project-directory ./infra/docker \
# --env-file='./infra/.env' \
# -f ./infra/docker/docker-compose.yml \
# -f ./infra/docker/docker-compose.dev.yml \
# rm -f -s -v db

# echo "Starting databse container…"
# docker compose \
# --project-name monitorenv \
# --project-directory ./infra/docker \
# --env-file='./infra/.env' \
# -f ./infra/docker/docker-compose.yml \
# -f ./infra/docker/docker-compose.dev.yml \
# up --wait db

# # echo "Waiting for database to be ready…"
# timeout 90s bash -c "until docker exec monitorenv-db-1 pg_isready ; do sleep 1 ; done"

echo "Restoring backup '${LAST_BACKUP_FILE_PATH}'…"
cat "${LAST_BACKUP_FILE_PATH}" | docker exec -t monitorenv-db-1 pg_restore -c -d monitorenvdb -F c -U postgres
docker exec -t monitorenv-db-1 psql -d monitorenvdb -U postgres -f "/opt/monitorenv_backups/${LAST_BACKUP_FILE_NAME}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonne idée de linker le dossier des dumps via un volume ^^.

Loading