Browse Source

new: [postgres] add automatic backup every hours

Signed-off-by: Valentin Lab <valentin.lab@kalysto.org>
test
Valentin Lab 5 years ago
parent
commit
9cb7ed761c
  1. 2
      postgres/build/Dockerfile
  2. 23
      postgres/hooks/schedule_command-relation-joined
  3. 9
      postgres/metadata.yml
  4. 7
      postgres/resources/bin/pg-backup

2
postgres/build/Dockerfile

@ -38,4 +38,6 @@ COPY --from=builder /bin/pgm /bin
RUN apk add /tmp/mbuffer-2018*.apk --allow-untrusted
RUN apk add gzip
RUN ln -sf /usr/bin/mbuffer /usr/bin/buffer

23
postgres/hooks/schedule_command-relation-joined

@ -7,11 +7,13 @@
## - the target of the link is launched first, and get a chance to ``relation-set``
## - both side of the scripts get to use ``relation-get``.
. lib/common
set -e
## XXXvlab: should use container name here so that it could support
## multiple postgres
label=pg-backup-$SERVICE_NAME
label=${SERVICE_NAME}-pg-backup
DST=$CONFIGSTORE/$TARGET_SERVICE_NAME/etc/cron/$label
schedule=$(relation-get schedule)
@ -20,10 +22,6 @@ if ! echo "$schedule" | egrep '^\s*(([0-9/,*-]+\s+){4,4}[0-9/,*-]+|@[a-z]+)\s*$'
exit 1
fi
## XXXvlab: how is this meant to works if there's multiples postgres (about PGHOST=?) ?
## Warning: using '\' in heredoc will be removed in the final cron file, which
## is totally wanted: cron does not support multilines.
exclude_dbs=$(relation-get exclude-dbs 2>/dev/null) || true
exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null |
nspc) || true
@ -31,20 +29,17 @@ exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null |
## Warning: 'docker -v' will use HOST directory even if launched from
## 'cron' container.
file_put "$DST" <<EOF
COMPOSE_LAUNCHER_OPTS=$COMPOSE_LAUNCHER_OPTS
$schedule root lock $label -D -p 10 -c "\
docker run --rm \
-e PGHOST=\$POSTGRES_PORT_5432_TCP_ADDR \
-e PGHOST=${SERVICE_NAME} \
--network ${PROJECT_NAME}_default \
-e exclude_dbs=\"$exclude_dbs\" \
-v /root/.pgpass:/root/.pgpass \
-v \"$BASE_CHARM_PATH/resources/bin/pg-backup:/usr/sbin/pg-backup\" \
-v \"$LOCAL_DB_PASSFILE\":/root/.pgpass \
-v \"$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/resources/bin/pg-backup:/usr/sbin/pg-backup\" \
-v \"$SERVICE_DATASTORE/var/backups/pg:/var/backups/pg\" \
--entrypoint pg-backup \
\"$DOCKER_BASE_IMAGE\"" 2>&1 | ts '\%F \%T \%Z' >> /var/log/cron/pg-backup_script.log
EOF
chmod +x "$DST"
config-add "\
$MASTER_TARGET_SERVICE_NAME:
links:
- $MASTER_BASE_SERVICE_NAME
"

9
postgres/metadata.yml

@ -4,3 +4,12 @@ data-resources:
- /var/lib/postgresql/data
provides:
postgres-database:
uses:
schedule-command:
constraint: optional
auto: pair
solves:
backup: "Automatic regular backups"
default-options:
schedule: "31 * * * *" ## schedule backup every hour

7
postgres/resources/bin/pg-backup

@ -23,14 +23,15 @@ for db in $dbs; do
(( start = SECONDS ))
echo "Dumping database $db..." >&2
pg_dump -Ox "$db" | gzip --rsyncable > "$dst.inprogress"
errlvl="$?"
errlvl="${PIPESTATUS[0]}"
## Atomic replace
(( elapsed = SECONDS - start ))
if [ "$errlvl" != "0" ]; then
echo " !! Error when dumping database $db." >&2
rm "$dst.inprogress"
else
echo " ..dumped $db to $dst ($(du -sh "$dst" | cut -f 1) in ${elapsed}s)" >&2
fi
mv "$dst.inprogress" "$dst"
printf " ..dumped %-35s (%12s in %10s)\n" "${dst##*/}" "$(du -sh "$dst" | cut -f 1)" "${elapsed}s" >&2
fi
done
Loading…
Cancel
Save