From d1ad5103ed7de9c368ce300caf967e35b7bfe6a1 Mon Sep 17 00:00:00 2001 From: Chl Date: Wed, 24 Jul 2019 22:13:23 +0200 Subject: [PATCH] sauv_postgresql: correct return code with Bash --- sauvegarde_postgresql.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/sauvegarde_postgresql.sh b/sauvegarde_postgresql.sh index 5c02e53..cf579b9 100755 --- a/sauvegarde_postgresql.sh +++ b/sauvegarde_postgresql.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Configuration # Le fichier sera nommé BASE_DIR/PREFIXEbasenameSUFFIXE @@ -7,17 +7,27 @@ PREFIXE="sauv_pgsql" SUFFIXE=_$( date +%Y%m%d-%H%M ).sql.gz DUREE_DE_VIE=5 -# Vérifications initiales +# Exit at first error +# note : check that you receive crontab's mails in case something goes wrong. +set -e +if [ -n "$BASH" ]; then + # Bash required for this. If not available, return code will be 0 + # even if pg_dumpall fails (but usually it write something on stderr + # so cron will send a mail nevertheless). + set -o pipefail +fi + +# Initial checks if [ ! -d "$BASE_DIR" ]; then echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2 exit 1 fi -# Sauvegarde des bases +# Backup of the whole default cluster su -l -c pg_dumpall postgres | gzip -c > "$BASE_DIR/$PREFIXE$SUFFIXE" -# Suppression des anciennes bases +# Deletion of old backups if [ "$1" = "--delete-olds" ]; then find $BASE_DIR -name "$PREFIXE*" -mtime +$DUREE_DE_VIE -print0 | xargs -n 200 -r -0 rm -f fi