services/postgres/run

48 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/bin/bash
set -eu
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $DIR/../help.sh
postgres_help() {
echo "./run backup : Lancer la sauvegarde d'une base de données"
echo "./run restore : Restore une sauvegarde"
}
postgres_backup() {
script_env
backup_folder_create
POSTGRES_BACKUP_FILE_DEFAULT=backups/`date +%Y%m%d_%H%M%S`_$POSTGRES_DB.sql
POSTGRES_BACKUP_FILE=${POSTGRES_BACKUP_FILE:-$POSTGRES_BACKUP_FILE_DEFAULT}
echo "🏁 Start backup PostgreSQL database '$POSTGRES_DB' in '$POSTGRES_BACKUP_FILE'"
docker exec -i $POSTGRES_CONTAINER_NAME pg_dump $POSTGRES_DB -U $POSTGRES_USER > $POSTGRES_BACKUP_FILE
}
postgres_restore() {
script_env
echo "🏁 Start restore PostgreSQL database '$POSTGRES_DB' from '$POSTGRES_BACKUP_FILE'"
docker restart $POSTGRES_CONTAINER_NAME
docker exec $POSTGRES_CONTAINER_NAME psql template1 -U $POSTGRES_USER -c "DROP DATABASE $POSTGRES_DB"
docker exec $POSTGRES_CONTAINER_NAME psql template1 -U $POSTGRES_USER -c "CREATE DATABASE $POSTGRES_DB with owner $POSTGRES_USER"
cat $POSTGRES_BACKUP_FILE | docker exec -i $POSTGRES_CONTAINER_NAME psql -U $POSTGRES_USER -d $POSTGRES_DB
}
if [ $# -ge 1 ]; then
if [ "${1}" == "backup" ]; then
script_start
postgres_backup
script_end
elif [ "${1}" == "restore" ]; then
script_start
postgres_restore
script_end
elif [ "${1}" != "--only-source" ]; then
postgres_help
fi
else
postgres_help
fi