From 734979fce41ed4bdf2ddbdfba20bda4588633f55 Mon Sep 17 00:00:00 2001 From: "joel.clement" Date: Thu, 17 Aug 2023 11:58:57 +0200 Subject: [PATCH] up postgres volume & backup --- .env.dev.example | 4 +- .env.prod.example | 4 +- .../init_db}/add_pg_extensions.sql | 0 .../init_db}/docker_postgres_healthcheck.sh | 0 assets/postgres/scripts/backup.sh | 61 +++++++++++++++++++ docker-compose.yml | 2 + 6 files changed, 69 insertions(+), 2 deletions(-) rename assets/{db => postgres/init_db}/add_pg_extensions.sql (100%) rename assets/{db => postgres/init_db}/docker_postgres_healthcheck.sh (100%) create mode 100755 assets/postgres/scripts/backup.sh diff --git a/.env.dev.example b/.env.dev.example index 7e313ac..64d84b5 100644 --- a/.env.dev.example +++ b/.env.dev.example @@ -23,6 +23,7 @@ TRAEFIK_LETSENCRYPT_ACME_EMAIL=geon@tu.re # répertoire de base pour les volumes des services (data + config) VOLUME_DATA_DIRECTORY=./data +ASSETS_DIRECTORY=./assets # accès base de données POSTGRES_USER="geonatadmin" @@ -116,7 +117,8 @@ POSTGRES_IMAGE=postgis/postgis:15-3.3 POSTGRES_CONTAINER_NAME=${CONTAINER_NAME_PREFIX}postgres POSTGRES_VOLUME_DATA_DIRECTORY=${VOLUME_DATA_STORAGE_DIRECTORY}/postgres POSTGRES_VOLUME_BACKUP_DIRECTORY=${VOLUME_DATA_SERVICES_DIRECTORY}/postgres/backup -POSTGRES_VOLUME_INIT_DB_DIRECTORY="./assets/db" +POSTGRES_VOLUME_SCRIPTS_DIRECTORY=${ASSETS_DIRECTORY}/postgres/scripts +POSTGRES_VOLUME_INIT_DB_DIRECTORY=${ASSETS_DIRECTORY}/postgres/init_db POSTGRES_SHM_SIZE=1000000000 diff --git a/.env.prod.example b/.env.prod.example index b447309..3c52ccd 100644 --- a/.env.prod.example +++ b/.env.prod.example @@ -23,6 +23,7 @@ TRAEFIK_LETSENCRYPT_ACME_EMAIL=geon@tu.re # répertoire de base pour les volumes des services (data + config) VOLUME_DATA_DIRECTORY=./data +ASSETS_DIRECTORY=./assets # accès base de données POSTGRES_USER="geonatadmin" @@ -117,7 +118,8 @@ POSTGRES_IMAGE=postgis/postgis:15-3.3 POSTGRES_CONTAINER_NAME=${CONTAINER_NAME_PREFIX}postgres POSTGRES_VOLUME_DATA_DIRECTORY=${VOLUME_DATA_STORAGE_DIRECTORY}/postgres POSTGRES_VOLUME_BACKUP_DIRECTORY=${VOLUME_DATA_SERVICES_DIRECTORY}/postgres/backup -POSTGRES_VOLUME_INIT_DB_DIRECTORY="./assets/db" +POSTGRES_VOLUME_SCRIPTS_DIRECTORY=${ASSETS_DIRECTORY}/postgres/scripts +POSTGRES_VOLUME_INIT_DB_DIRECTORY=${ASSETS_DIRECTORY}/postgres/init_db POSTGRES_SHM_SIZE=1000000000 diff --git a/assets/db/add_pg_extensions.sql b/assets/postgres/init_db/add_pg_extensions.sql similarity index 100% rename from assets/db/add_pg_extensions.sql rename to assets/postgres/init_db/add_pg_extensions.sql diff --git a/assets/db/docker_postgres_healthcheck.sh b/assets/postgres/init_db/docker_postgres_healthcheck.sh similarity index 100% rename from assets/db/docker_postgres_healthcheck.sh rename to assets/postgres/init_db/docker_postgres_healthcheck.sh diff --git a/assets/postgres/scripts/backup.sh b/assets/postgres/scripts/backup.sh new file mode 100755 index 0000000..1849642 --- /dev/null +++ b/assets/postgres/scripts/backup.sh @@ -0,0 +1,61 @@ +#!/bin/sh +# +# Crée un dump de la base geonature +# Renseigne le fichier log avec la date de backup +# et la taille du fichier non compressé + +echo "+-------------------------------+" +echo "| Database backup |" +echo "+-------------------------------+" + +# - 1 )logs (start) +log_file_global=/backup/backup.log + +date_start=$(date '+%Y-%m-%d_%H-%M-%S') +date_jour=$(date '+%Y-%m-%d') +log_line_start="${date_start} -> ... (en cours)" +echo $log_line_start >> $log_file_global + + +# - 2 )creation du backup + +backup_file=/backup/geonature_backup.sql + +export PGPASSWORD=${POSTGRES_PASS} +pg_dump --format=plain \ + --create \ + --no-owner --no-acl \ + -d ${POSTGRES_DB} -h localhost -U ${POSTGRES_USER} \ + > $backup_file + +# patch pour l'extension pg_trgm +sed -i "s/with SCHEMA pg_catalog//" $backup_file + + +# - compression du backup + +tar cvfz ${backup_file}_${date_jour}.tar.gz ${backup_file} + +# - on regarde la taille du backup non compressé (pour les logs) + +size_h_backup=$(ls -lh $backup_file | awk '{print $5}') +size_backup=$(ls -l $backup_file | awk '{print $5}') + +# - suppression du fichier non compressé +rm ${backup_file} + +# - 3) log (end) + +date_end=$(date '+%Y-%m-%d_%H-%M-%S') +log_line_end="${date_start} -> ${date_end} : $size_h_backup ($size_backup)" +sed -i "s/${log_line_start}.*/${log_line_end}/" $log_file_global +tail -n 1 $log_file_global + +# rotation des fichiers +# on garde les 5 derniers dumps ? +files_to_remove=$(ls /backup/geonature_backup* | sort -r | awk 'NR > 5 { print $1}') + +if [ ! -z "${files_to_remove}" ]; then + echo rm ${files_to_remove} + rm ${files_to_remove} +fi \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index f1fbecd..8491b18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -107,6 +107,8 @@ services: volumes: - ${POSTGRES_VOLUME_INIT_DB_DIRECTORY}:/docker-entrypoint-initdb.d/ - ${POSTGRES_VOLUME_DATA_DIRECTORY}:/var/lib/postgresql/data + - ${POSTGRES_VOLUME_BACKUP_DIRECTORY}:/backup + - ${POSTGRES_VOLUME_SCRIPTS_DIRECTORY}:/scripts healthcheck: test: [CMD-SHELL, "/docker-entrypoint-initdb.d/docker_postgres_healthcheck.sh"] interval: 5s