From 9cb7ed761c1c103312760d9db7aa2fa0888b4818 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 15 Nov 2019 12:01:48 +0100 Subject: [PATCH] new: [postgres] add automatic backup every hours Signed-off-by: Valentin Lab --- postgres/build/Dockerfile | 2 ++ .../hooks/schedule_command-relation-joined | 23 ++++++++----------- postgres/metadata.yml | 11 ++++++++- postgres/resources/bin/pg-backup | 7 +++--- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/postgres/build/Dockerfile b/postgres/build/Dockerfile index 961c7ca..86e937e 100644 --- a/postgres/build/Dockerfile +++ b/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 \ No newline at end of file diff --git a/postgres/hooks/schedule_command-relation-joined b/postgres/hooks/schedule_command-relation-joined index fc15bc0..5312983 100755 --- a/postgres/hooks/schedule_command-relation-joined +++ b/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" <&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 -" diff --git a/postgres/metadata.yml b/postgres/metadata.yml index e5eea48..88ea8ed 100644 --- a/postgres/metadata.yml +++ b/postgres/metadata.yml @@ -3,4 +3,13 @@ maintainer: "Valentin Lab " data-resources: - /var/lib/postgresql/data provides: - postgres-database: \ No newline at end of file + postgres-database: + +uses: + schedule-command: + constraint: optional + auto: pair + solves: + backup: "Automatic regular backups" + default-options: + schedule: "31 * * * *" ## schedule backup every hour diff --git a/postgres/resources/bin/pg-backup b/postgres/resources/bin/pg-backup index daeffea..31d027f 100755 --- a/postgres/resources/bin/pg-backup +++ b/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 + mv "$dst.inprogress" "$dst" + printf " ..dumped %-35s (%12s in %10s)\n" "${dst##*/}" "$(du -sh "$dst" | cut -f 1)" "${elapsed}s" >&2 fi - mv "$dst.inprogress" "$dst" done