Support custom pg user/db names in DB backup script

This commit is contained in:
Christof Dorner 2022-12-26 15:02:41 +01:00
parent 54db892b90
commit cf76173bd1
2 changed files with 8 additions and 2 deletions

View file

@ -15,6 +15,6 @@ RUN touch /var/log/cron.log
# We need to start the cron service the first time it runs, when it's still being run
# as the root user. We're going to add a check that looks at the first argument and
# if it's 'cron', starts the service and then removes that argument.
RUN awk '$0 ~ /^\t_main "\$@"$/ { print "\tif [[ $1 == cron ]]; then\n\t\tservice cron start\n\t\tshift\n\tfi" }{ print }' docker-entrypoint.sh > bookwyrm-entrypoint.sh
RUN awk '$0 ~ /^\t_main "\$@"$/ { print "\tif [[ $1 == cron ]]; then\n\t\techo \"POSTGRES_DB=${POSTGRES_DB}\" > /backups/.env\n\t\techo \"POSTGRES_USER=${POSTGRES_USER}\" >> /backups/.env\n\t\tservice cron start\n\t\tshift\n\tfi" }{ print }' docker-entrypoint.sh > bookwyrm-entrypoint.sh
RUN chown postgres /bookwyrm-entrypoint.sh
RUN chmod u=rwx,go=r /bookwyrm-entrypoint.sh

View file

@ -1,7 +1,13 @@
#!/bin/bash
source /backups/.env
if [ -z "$POSTGRES_DB" ]; then
echo "Database not specified, defaulting to bookwyrm"
fi
if [ -z "$POSTGRES_USER" ]; then
echo "Database user not specified, defaulting to bookwyrm"
fi
BACKUP_DB=${POSTGRES_DB:-bookwyrm}
BACKUP_USER=${POSTGRES_USER:-bookwyrm}
filename=backup_${BACKUP_DB}_$(date +%F)
pg_dump -U $BACKUP_DB > /backups/$filename.sql
pg_dump -U $BACKUP_USER $BACKUP_DB > /backups/$filename.sql