From 47313bca65de105145f2168538b294a9bcc0e98d Mon Sep 17 00:00:00 2001
From: intrigeri <intrigeri@boum.org>
Date: Fri, 8 Jan 2010 23:40:23 +0100
Subject: [PATCH] ldap,mysql,pgsql: use bash pipefail option so that failed
 dumps are reported as such

This should fix Redmine bug #1340.

This option makes pipelines return as status the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit
successfully. See bash(1) for details.

E.g. this prevents the following from exiting with status 0 (!) if pg_dumpall
fails:

        pg_dumpall | gzip
---
 ChangeLog         | 6 ++++++
 handlers/ldap.in  | 2 +-
 handlers/mysql.in | 8 ++++----
 handlers/pgsql.in | 8 ++++----
 4 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 130a544..18ed157 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,8 @@ version 0.9.7 -- UNRELEASED
     handler changes
 	ldap:
 	 . Use gzip's --rsyncable option.
+	 . Use bash pipefail option when needed so that failed dumps are
+	   reported as such.
 	maildir:
 	 . fix location of deleted_on file
 	 . add missing destid_file options to ssh connections
@@ -32,10 +34,14 @@ version 0.9.7 -- UNRELEASED
 	 . Use gzip's --rsyncable option.
 	 . Quote output filenames to support shell meta-characters in
 	   database names.
+	 . Use bash pipefail option when needed so that failed dumps are
+	   reported as such.
 	pgsql:
 	 . Use gzip's --rsyncable option.
 	 . Quote output filenames to support shell meta-characters in
 	   database names.
+	 . Use bash pipefail option when needed so that failed dumps are
+	   reported as such.
 	sys:
 	 . New luksheaders option (default=disabled) to backup the Luks header
 	   of every Luks device.
diff --git a/handlers/ldap.in b/handlers/ldap.in
index 853cefb..fda24d0 100644
--- a/handlers/ldap.in
+++ b/handlers/ldap.in
@@ -91,7 +91,7 @@ if [ "$ldif" == "yes" ]; then
             execstr="$execstr > $dumpdir/$dbsuffix.ldif"
          fi
          debug "$execstr"
-         output=`su root -c "$execstr" 2>&1`
+         output=`su root -c "set -o pipefail ; $execstr" 2>&1`
          code=$?
          if [ "$code" == "0" ]; then
             debug $output
diff --git a/handlers/mysql.in b/handlers/mysql.in
index f3230ad..3488c51 100644
--- a/handlers/mysql.in
+++ b/handlers/mysql.in
@@ -237,14 +237,14 @@ then
    then
       if [ $usevserver = yes ]
       then
-         debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
-         databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
+         debug 'set -o pipefail ; echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
+         databases=`set -o pipefail ; echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
          if [ $? -ne 0 ]
          then
             fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
          fi
       else
-         databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
+         databases=$(set -o pipefail ; su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
          if [ $? -ne 0 ]
          then
             fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
@@ -298,7 +298,7 @@ then
       debug "su $user -c \"$execstr\""
       if [ ! $test ]
       then
-         output=`su $user -c "$execstr" 2>&1`
+         output=`su $user -c "set -o pipefail ; $execstr" 2>&1`
          code=$?
          if [ "$code" == "0" ]
          then
diff --git a/handlers/pgsql.in b/handlers/pgsql.in
index d7839fb..77a73fe 100644
--- a/handlers/pgsql.in
+++ b/handlers/pgsql.in
@@ -75,13 +75,13 @@ chmod 700 $vroot$backupdir
 if [ "$databases" == "all" ]; then
    if [ $usevserver = yes ]; then
       if [ "$compress" == "yes" ]; then
-         execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP --rsyncable > '$backupdir/${vsname}.sql.gz'\""
+         execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMPALL | $GZIP --rsyncable > '$backupdir/${vsname}.sql.gz'\""
       else
          execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > '$backupdir/${vsname}.sql'\""
       fi
    else
       if [ "$compress" == "yes" ]; then
-         execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP --rsyncable > '$backupdir/${localhost}-all.sql.gz'\""
+         execstr="su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMPALL | $GZIP --rsyncable > '$backupdir/${localhost}-all.sql.gz'\""
       else
          execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > '$backupdir/${localhost}-all.sql'\""
       fi
@@ -104,13 +104,13 @@ else
    for db in $databases; do
       if [ $usevserver = yes ]; then
          if [ "$compress" == "yes" ]; then
-            execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP --rsyncable > '$backupdir/${db}.sql.gz'\""
+            execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMP $db | $GZIP --rsyncable > '$backupdir/${db}.sql.gz'\""
          else
             execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > '$backupdir/${db}.sql'\""
          fi
       else
          if [ "$compress" == "yes" ]; then
-            execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP --rsyncable > '$backupdir/${db}.sql.gz'\""
+            execstr="su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMP $db | $GZIP --rsyncable > '$backupdir/${db}.sql.gz'\""
          else
             execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > '$backupdir/${db}.sql'\""
          fi
-- 
GitLab