containers icon indicating copy to clipboard operation
containers copied to clipboard

How to do backups of postgresql databases in the image

Open niclan opened this issue 3 years ago • 3 comments

Name and Version

bitnami/postgresql

What is the problem this feature will solve?

Making backups with no password needed in the backup script outside of kubernetes.

It's a bit cumbersome

What is the feature you are proposing to solve the problem?

If you have kubectl access to a postgres pod deployed by bitnami helm chart, here is a kubectl command to get a backup dump from it on stdout, just like pg_dump usually does:

kubectl exec -n $NAMESPACE $PODNAME -- bash -c ". /opt/bitnami/scripts/libpostgresql.sh && postgresql_enable_nss_wrapper && PGPASSWORD=\$POSTGRES_PASSWORD pg_dump $OPTS -c -U postgres $DB"

You have to supply your own values for NAMESPACE, PODNAME, OPTS (can be empty) and the database name in DB.

If you were to include a very small shell wrapper (inside the container) to do the nss_wrapper and PGPASSWORD bits and then run a given pg* command this would be very very helpful. The 'bash -c' trick and the quoting rules to obtain the POSTGRESS_PASSWORD value took me a while to puzzle out and they would not be needed if a wrapper script is provided. Also took some extra time because I made some spelling-mistakes...

What alternatives have you considered?

No response

niclan avatar Aug 04 '22 10:08 niclan

Hi,

Thanks for the feedback! Contributions are welcome, would you like to propose a script to be added to the container in a PR? We can discuss and provide feedback :D

javsalgar avatar Aug 05 '22 07:08 javsalgar

I propose for pgexec this:

#!/bin/bash

# Designed to aid execution of e.g. psql or pg_dump within the container using the admin password which is known inside
# the container.

# Exit on all errors
set -e

PROG="$1"
shift

# Set up environment where userid of the user running postgres is known - this is local to this script environment
. /opt/bitnami/scripts/libpostgresql.sh
postgresql_enable_nss_wrapper

# This removes the nss setup files
trap 'rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP"' EXIT

# Execute with userid and password
PGUSER=postgres PGPASSWORD="$POSTGRES_PASSWORD" "$PROG" "$@"

If you agree that this should work and is a good approach I'll even test and debug it :-)

niclan avatar Aug 05 '22 11:08 niclan

Thanks a lot @niclan, please feel free to open a PR. I don't see any inconvenience at the moment

fmulero avatar Aug 08 '22 10:08 fmulero

This Issue has been automatically marked as "stale" because it has not had recent activity (for 15 days). It will be closed if no further activity occurs. Thanks for the feedback.

github-actions[bot] avatar Aug 24 '22 01:08 github-actions[bot]

Due to the lack of activity in the last 5 days since it was marked as "stale", we proceed to close this Issue. Do not hesitate to reopen it later if necessary.

github-actions[bot] avatar Aug 29 '22 01:08 github-actions[bot]