Skip to content

Commit 525fc7b

Browse files
committed
feat: backup postgres databases owned by a specific user if PGDATABASE is not set and pg_dump is being used
1 parent 6926f45 commit 525fc7b

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

mariadb-logical-backup/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ LABEL maintainer="Anantharam R U anantharam@obmondo.com"
33

44
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
55

6+
ARG PIP_VERSION="24.0"
7+
68
RUN apt-get update \
79
&& apt-get install --no-install-recommends -y \
810
apt-utils \
@@ -17,7 +19,7 @@ RUN apt-get update \
1719
gcc \
1820
libffi-dev \
1921
&& curl -sL https://aka.ms/InstallAzureCLIDeb | bash \
20-
&& pip3 install --upgrade pip \
22+
&& pip3 install --no-cache-dir pip=="${PIP_VERSION}" \
2123
&& pip3 install --no-cache-dir awscli --upgrade \
2224
&& pip3 install --no-cache-dir gsutil --upgrade \
2325
&& apt-get update \

postgres-logical-backup/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN apt-get update \
1717
gcc \
1818
libffi-dev \
1919
&& curl -sL https://aka.ms/InstallAzureCLIDeb | bash \
20-
&& pip3 install --upgrade pip \
20+
&& pip3 install --no-cache-dir pip=="${PIP_VERSION}" \
2121
&& pip3 install --no-cache-dir awscli --upgrade \
2222
&& pip3 install --no-cache-dir gsutil --upgrade \
2323
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list \

postgres-logical-backup/dump.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ IFS=$'\n\t'
77
# PGHOST PGPASSWORD
88

99
ALL_DB_SIZE_QUERY="select sum(pg_database_size(datname)::numeric) from pg_database;"
10+
1011
PG_BIN=/usr/lib/postgresql/$PG_VERSION/bin
1112
DUMP_SIZE_COEFF=5
1213
ERRORCOUNT=0
@@ -23,9 +24,9 @@ function dump {
2324
echo "Taking dump from ${PGHOST} using ${USE_PG_DUMP:-pg_dumpall}"
2425

2526
if [[ "${USE_PG_DUMP:-}" == "true" ]]; then
26-
"$PG_BIN"/pg_dump
27+
"$PG_BIN"/pg_dump --verbose
2728
else
28-
"$PG_BIN"/pg_dumpall --exclude-database='postgres'
29+
"$PG_BIN"/pg_dumpall --exclude-database='postgres' --verbose
2930
fi
3031
}
3132

@@ -105,7 +106,7 @@ function aws_upload {
105106
[[ ! -z "${LOGICAL_BACKUP_S3_REGION}" ]] && args+=("--region=${LOGICAL_BACKUP_S3_REGION}")
106107

107108
echo "Uploading dump to s3"
108-
aws s3 cp - "$PATH_TO_BACKUP" "${args[@]//\'/}"
109+
aws s3 cp - "$PATH_TO_BACKUP" "${args[@]}"
109110
}
110111

111112
function gcs_upload {
@@ -130,6 +131,18 @@ if [ "$LOGICAL_BACKUP_PROVIDER" == "az" ]; then
130131
dump | compress > /tmp/azure-backup.sql.gz
131132
az_upload /tmp/azure-backup.sql.gz
132133
else
134+
135+
# Backup all the databases owned by the user if PGDATABASE is not set
136+
if [[ "${USE_PG_DUMP:-}" == "true" ]] && [[ -z "${PGDATABASE:-}" ]]; then
137+
GET_DATABASE_LIST_FOR_USER_QUERY="SELECT datname FROM pg_database WHERE datistemplate = false AND datname = '$PGUSER';"
138+
DATABASES=$(psql -U postgres -tqAc $GET_DATABASE_LIST_FOR_USER_QUERY)
139+
for DB in $DATABASES; do
140+
echo "Backing up $DB"
141+
# We set PGDATABASE locally for each run
142+
PGDATABASE=$DB dump | compress | upload
143+
done
144+
fi
145+
133146
dump | compress | upload
134147
[[ ${PIPESTATUS[0]} != 0 || ${PIPESTATUS[1]} != 0 || ${PIPESTATUS[2]} != 0 ]] && (( ERRORCOUNT += 1 ))
135148
set +x

0 commit comments

Comments
 (0)