1
0
Fork 0

sauv_postgresql: correct return code with Bash

This commit is contained in:
Chl 2019-07-24 22:13:23 +02:00
parent f79dd1a13b
commit d1ad5103ed

View file

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
# Configuration # Configuration
# Le fichier sera nommé BASE_DIR/PREFIXEbasenameSUFFIXE # Le fichier sera nommé BASE_DIR/PREFIXEbasenameSUFFIXE
@ -7,17 +7,27 @@ PREFIXE="sauv_pgsql"
SUFFIXE=_$( date +%Y%m%d-%H%M ).sql.gz SUFFIXE=_$( date +%Y%m%d-%H%M ).sql.gz
DUREE_DE_VIE=5 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 if [ ! -d "$BASE_DIR" ]; then
echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2 echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2
exit 1 exit 1
fi fi
# Sauvegarde des bases # Backup of the whole default cluster
su -l -c pg_dumpall postgres | gzip -c > "$BASE_DIR/$PREFIXE$SUFFIXE" 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 if [ "$1" = "--delete-olds" ]; then
find $BASE_DIR -name "$PREFIXE*" -mtime +$DUREE_DE_VIE -print0 | xargs -n 200 -r -0 rm -f find $BASE_DIR -name "$PREFIXE*" -mtime +$DUREE_DE_VIE -print0 | xargs -n 200 -r -0 rm -f
fi fi