Skip to content

Commit

Permalink
up postgres volume & backup
Browse files Browse the repository at this point in the history
  • Loading branch information
joelclems committed Aug 17, 2023
1 parent a13b2b5 commit 734979f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env.dev.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ [email protected]

# 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"
Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion .env.prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ [email protected]

# 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"
Expand Down Expand Up @@ -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


Expand Down
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions assets/postgres/scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 734979f

Please sign in to comment.