Blame SOURCES/rear-bz1631183.patch

3adda6
diff --git a/usr/share/rear/backup/NETFS/default/500_make_backup.sh b/usr/share/rear/backup/NETFS/default/500_make_backup.sh
3adda6
index 47266910..7170cda6 100644
3adda6
--- a/usr/share/rear/backup/NETFS/default/500_make_backup.sh
3adda6
+++ b/usr/share/rear/backup/NETFS/default/500_make_backup.sh
3adda6
@@ -67,49 +67,99 @@ else
3adda6
     SPLIT_COMMAND="dd of=$backuparchive"
3adda6
 fi
3adda6
 
3adda6
+# Used by "tar" method to record which pipe command failed
3adda6
+FAILING_BACKUP_PROG_FILE="$TMP_DIR/failing_backup_prog"
3adda6
+FAILING_BACKUP_PROG_RC_FILE="$TMP_DIR/failing_backup_prog_rc"
3adda6
+
3adda6
 LogPrint "Creating $BACKUP_PROG archive '$backuparchive'"
3adda6
 ProgressStart "Preparing archive operation"
3adda6
 (
3adda6
 case "$(basename ${BACKUP_PROG})" in
3adda6
-	# tar compatible programs here
3adda6
-	(tar)
3adda6
-		set_tar_features
3adda6
-		Log $BACKUP_PROG $TAR_OPTIONS --sparse --block-number --totals --verbose \
3adda6
-			--no-wildcards-match-slash --one-file-system \
3adda6
-			--ignore-failed-read "${BACKUP_PROG_OPTIONS[@]}" \
3adda6
-			$BACKUP_PROG_CREATE_NEWER_OPTIONS \
3adda6
-			${BACKUP_PROG_BLOCKS:+-b $BACKUP_PROG_BLOCKS} "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
-			-X $TMP_DIR/backup-exclude.txt -C / -c -f - \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE \| $BACKUP_PROG_CRYPT_OPTIONS BACKUP_PROG_CRYPT_KEY \| $SPLIT_COMMAND
3adda6
-		$BACKUP_PROG $TAR_OPTIONS --sparse --block-number --totals --verbose \
3adda6
-			--no-wildcards-match-slash --one-file-system \
3adda6
-			--ignore-failed-read "${BACKUP_PROG_OPTIONS[@]}" \
3adda6
-			$BACKUP_PROG_CREATE_NEWER_OPTIONS \
3adda6
-			${BACKUP_PROG_BLOCKS:+-b $BACKUP_PROG_BLOCKS} "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
-			-X $TMP_DIR/backup-exclude.txt -C / -c -f - \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE | $BACKUP_PROG_CRYPT_OPTIONS $BACKUP_PROG_CRYPT_KEY | $SPLIT_COMMAND
3adda6
-	;;
3adda6
-	(rsync)
3adda6
-		# make sure that the target is a directory
3adda6
-		mkdir -p $v "$backuparchive" >&2
3adda6
-		Log $BACKUP_PROG --verbose "${BACKUP_RSYNC_OPTIONS[@]}" --one-file-system --delete \
3adda6
-			--exclude-from=$TMP_DIR/backup-exclude.txt --delete-excluded \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) "$backuparchive"
3adda6
-		$BACKUP_PROG --verbose "${BACKUP_RSYNC_OPTIONS[@]}" --one-file-system --delete \
3adda6
-			--exclude-from=$TMP_DIR/backup-exclude.txt --delete-excluded \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) "$backuparchive" >&2
3adda6
-	;;
3adda6
-	(*)
3adda6
-		Log "Using unsupported backup program '$BACKUP_PROG'"
3adda6
-		Log $BACKUP_PROG "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
-			$BACKUP_PROG_OPTIONS_CREATE_ARCHIVE $TMP_DIR/backup-exclude.txt \
3adda6
-			"${BACKUP_PROG_OPTIONS[@]}" $backuparchive \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE > $backuparchive
3adda6
-		$BACKUP_PROG "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
-			$BACKUP_PROG_OPTIONS_CREATE_ARCHIVE $TMP_DIR/backup-exclude.txt \
3adda6
-			"${BACKUP_PROG_OPTIONS[@]}" $backuparchive \
3adda6
-			$(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE > $backuparchive
3adda6
-	;;
3adda6
+    # tar compatible programs here
3adda6
+    (tar)
3adda6
+        set_tar_features
3adda6
+        Log $BACKUP_PROG $TAR_OPTIONS --sparse --block-number --totals --verbose \
3adda6
+            --no-wildcards-match-slash --one-file-system \
3adda6
+            --ignore-failed-read "${BACKUP_PROG_OPTIONS[@]}" \
3adda6
+            $BACKUP_PROG_CREATE_NEWER_OPTIONS \
3adda6
+            ${BACKUP_PROG_BLOCKS:+-b $BACKUP_PROG_BLOCKS} "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
+            -X $TMP_DIR/backup-exclude.txt -C / -c -f - \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE \| $BACKUP_PROG_CRYPT_OPTIONS BACKUP_PROG_CRYPT_KEY \| $SPLIT_COMMAND
3adda6
+
3adda6
+        # Variable used to record the short name of piped commands in case of
3adda6
+        # error, e.g. ( "tar" "cat" "dd" ) in case of unencrypted and unsplit backup.
3adda6
+        backup_prog_shortnames=(
3adda6
+            "$(basename $(echo "$BACKUP_PROG" | awk '{ print $1 }'))"
3adda6
+            "$(basename $(echo "$BACKUP_PROG_CRYPT_OPTIONS" | awk '{ print $1 }'))"
3adda6
+            "$(basename $(echo "$SPLIT_COMMAND" | awk '{ print $1 }'))"
3adda6
+        )
3adda6
+        for index in ${!backup_prog_shortnames[@]} ; do
3adda6
+            [ -n "${backup_prog_shortnames[$index]}" ] || BugError "No computed shortname for pipe component $index"
3adda6
+        done
3adda6
+
3adda6
+        $BACKUP_PROG $TAR_OPTIONS --sparse --block-number --totals --verbose    \
3adda6
+            --no-wildcards-match-slash --one-file-system                        \
3adda6
+            --ignore-failed-read "${BACKUP_PROG_OPTIONS[@]}"                    \
3adda6
+            $BACKUP_PROG_CREATE_NEWER_OPTIONS                                   \
3adda6
+            ${BACKUP_PROG_BLOCKS:+-b $BACKUP_PROG_BLOCKS}                       \
3adda6
+            "${BACKUP_PROG_COMPRESS_OPTIONS[@]}"                                \
3adda6
+            -X $TMP_DIR/backup-exclude.txt -C / -c -f -                         \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE |   \
3adda6
+                                                                    \
3adda6
+        $BACKUP_PROG_CRYPT_OPTIONS $BACKUP_PROG_CRYPT_KEY |         \
3adda6
+                                                                    \
3adda6
+        $SPLIT_COMMAND
3adda6
+        pipes_rc=( ${PIPESTATUS[@]} )
3adda6
+
3adda6
+        # Exit code logic:
3adda6
+        # - never return rc=1 (this is reserved for "tar" warning about modified files)
3adda6
+        # - process exit code in pipe's reverse order
3adda6
+        #   - if last command failed (e.g. "dd"), return an error
3adda6
+        #   - otherwise if previous command failed (e.g. "encrypt"), return an error
3adda6
+        #   ...
3adda6
+        #   - otherwise return "tar" exit code
3adda6
+        #
3adda6
+        # When an error occurs, record the program name in $FAILING_BACKUP_PROG_FILE
3adda6
+        # and real exit code in $FAILING_BACKUP_PROG_RC_FILE.
3adda6
+
3adda6
+        let index=${#pipes_rc[@]}-1
3adda6
+        while [ $index -ge 0 ] ; do
3adda6
+            rc=${pipes_rc[$index]}
3adda6
+            if [ $rc -ne 0 ] ; then
3adda6
+                echo "${backup_prog_shortnames[$index]}" > $FAILING_BACKUP_PROG_FILE
3adda6
+                echo "$rc" > $FAILING_BACKUP_PROG_RC_FILE
3adda6
+                if [ $rc -eq 1 ] && [ "${backup_prog_shortnames[$index]}" != "tar" ] ; then
3adda6
+                    rc=2
3adda6
+                fi
3adda6
+                exit $rc
3adda6
+            fi
3adda6
+            # This pipe command succeeded, check the previous one
3adda6
+            let index--
3adda6
+        done
3adda6
+        # This was a success
3adda6
+        exit 0
3adda6
+    ;;
3adda6
+    (rsync)
3adda6
+        # make sure that the target is a directory
3adda6
+        mkdir -p $v "$backuparchive" >&2
3adda6
+        Log $BACKUP_PROG --verbose "${BACKUP_RSYNC_OPTIONS[@]}" --one-file-system --delete \
3adda6
+            --exclude-from=$TMP_DIR/backup-exclude.txt --delete-excluded \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) "$backuparchive"
3adda6
+        $BACKUP_PROG --verbose "${BACKUP_RSYNC_OPTIONS[@]}" --one-file-system --delete \
3adda6
+            --exclude-from=$TMP_DIR/backup-exclude.txt --delete-excluded \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) "$backuparchive" >&2
3adda6
+    ;;
3adda6
+    (*)
3adda6
+        Log "Using unsupported backup program '$BACKUP_PROG'"
3adda6
+        Log $BACKUP_PROG "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
+            $BACKUP_PROG_OPTIONS_CREATE_ARCHIVE $TMP_DIR/backup-exclude.txt \
3adda6
+            "${BACKUP_PROG_OPTIONS[@]}" $backuparchive \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE > $backuparchive
3adda6
+        $BACKUP_PROG "${BACKUP_PROG_COMPRESS_OPTIONS[@]}" \
3adda6
+            $BACKUP_PROG_OPTIONS_CREATE_ARCHIVE $TMP_DIR/backup-exclude.txt \
3adda6
+            "${BACKUP_PROG_OPTIONS[@]}" $backuparchive \
3adda6
+            $(cat $TMP_DIR/backup-include.txt) $RUNTIME_LOGFILE > $backuparchive
3adda6
+    ;;
3adda6
 esac 2> "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log"
3adda6
 # important trick: the backup prog is the last in each case entry and the case .. esac is the last command
3adda6
 # in the (..) subshell. As a result the return code of the subshell is the return code of the backup prog!
3adda6
@@ -121,44 +171,44 @@ sleep 1 # Give the backup software a good chance to start working
3adda6
 
3adda6
 # return disk usage in bytes
3adda6
 function get_disk_used() {
3adda6
-	let "$(stat -f -c 'used=(%b-%f)*%S' $1)"
3adda6
-	echo $used
3adda6
+    let "$(stat -f -c 'used=(%b-%f)*%S' $1)"
3adda6
+    echo $used
3adda6
 }
3adda6
 
3adda6
 # While the backup runs in a sub-process, display some progress information to the user.
3adda6
 # ProgressInfo texts have a space at the end to get the 'OK' from ProgressStop shown separated.
3adda6
 test "$PROGRESS_WAIT_SECONDS" || PROGRESS_WAIT_SECONDS=1
3adda6
 case "$( basename $BACKUP_PROG )" in
3adda6
-	(tar)
3adda6
-		while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
-			#blocks="$(stat -c %b ${backuparchive})"
3adda6
-			#size="$((blocks*512))"
3adda6
-			size="$(stat -c %s ${backuparchive}* | awk '{s+=$1} END {print s}')"
3adda6
-			ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
-		done
3adda6
-		;;
3adda6
-	(rsync)
3adda6
-		# since we do not want to do a $(du -s) run every second we count disk usage instead
3adda6
-		# this obviously leads to wrong results in case something else is writing to the same
3adda6
-		# disk at the same time as is very likely with a networked file system. For local disks
3adda6
-		# this should be good enough and in any case this is only some eye candy.
3adda6
-		# TODO: Find a fast way to count the actual transfer data, preferrable getting the info from rsync.
3adda6
-		let old_disk_used="$(get_disk_used "$backuparchive")"
3adda6
-		while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
-			let disk_used="$(get_disk_used "$backuparchive")" size=disk_used-old_disk_used
3adda6
-			ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
-		done
3adda6
-		;;
3adda6
-	(*)
3adda6
-		while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
-			size="$(stat -c "%s" "$backuparchive")" || {
3adda6
-				kill -9 $BackupPID
3adda6
-				ProgressError
3adda6
-				Error "$(basename $BACKUP_PROG) failed to create the archive file"
3adda6
-			}
3adda6
-			ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
-		done
3adda6
-		;;
3adda6
+    (tar)
3adda6
+        while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
+            #blocks="$(stat -c %b ${backuparchive})"
3adda6
+            #size="$((blocks*512))"
3adda6
+            size="$(stat -c %s ${backuparchive}* | awk '{s+=$1} END {print s}')"
3adda6
+            ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
+        done
3adda6
+        ;;
3adda6
+    (rsync)
3adda6
+        # since we do not want to do a $(du -s) run every second we count disk usage instead
3adda6
+        # this obviously leads to wrong results in case something else is writing to the same
3adda6
+        # disk at the same time as is very likely with a networked file system. For local disks
3adda6
+        # this should be good enough and in any case this is only some eye candy.
3adda6
+        # TODO: Find a fast way to count the actual transfer data, preferrable getting the info from rsync.
3adda6
+        let old_disk_used="$(get_disk_used "$backuparchive")"
3adda6
+        while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
+            let disk_used="$(get_disk_used "$backuparchive")" size=disk_used-old_disk_used
3adda6
+            ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
+        done
3adda6
+        ;;
3adda6
+    (*)
3adda6
+        while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null; do
3adda6
+            size="$(stat -c "%s" "$backuparchive")" || {
3adda6
+                kill -9 $BackupPID
3adda6
+                ProgressError
3adda6
+                Error "$(basename $BACKUP_PROG) failed to create the archive file"
3adda6
+            }
3adda6
+            ProgressInfo "Archived $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec] "
3adda6
+        done
3adda6
+        ;;
3adda6
 esac
3adda6
 ProgressStop
3adda6
 transfertime="$((SECONDS-starttime))"
3adda6
@@ -177,10 +227,12 @@ sleep 1
3adda6
 # everyone should see this warning, even if not verbose
3adda6
 case "$(basename $BACKUP_PROG)" in
3adda6
     (tar)
3adda6
-        if (( $backup_prog_rc == 1 )); then
3adda6
-            LogPrint "WARNING: $(basename $BACKUP_PROG) ended with return code $backup_prog_rc and below output:
3adda6
+        if (( $backup_prog_rc != 0 )); then
3adda6
+            prog="$(cat $FAILING_BACKUP_PROG_FILE)"
3adda6
+            if (( $backup_prog_rc == 1 )); then
3adda6
+                LogUserOutput "WARNING: $prog ended with return code 1 and below output:
3adda6
   ---snip---
3adda6
-$(grep '^tar: ' $RUNTIME_LOGFILE | sed -e 's/^/  /' | tail -n3)
3adda6
+$(grep '^tar: ' "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log" | sed -e 's/^/  /' | tail -n3)
3adda6
   ----------
3adda6
 This means that files have been modified during the archiving
3adda6
 process. As a result the backup may not be completely consistent
3adda6
@@ -188,16 +240,19 @@ or may not be a perfect copy of the system. Relax-and-Recover
3adda6
 will continue, however it is highly advisable to verify the
3adda6
 backup in order to be sure to safely recover this system.
3adda6
 "
3adda6
-        elif (( $backup_prog_rc > 1 )); then
3adda6
-            Error "$(basename $BACKUP_PROG) failed with return code $backup_prog_rc and below output:
3adda6
+            else
3adda6
+                rc=$(cat $FAILING_BACKUP_PROG_RC_FILE)
3adda6
+                Error "$prog failed with return code $rc and below output:
3adda6
   ---snip---
3adda6
-$(grep '^tar: ' $RUNTIME_LOGFILE | sed -e 's/^/  /' | tail -n3)
3adda6
+$(grep "^$prog: " "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log" | sed -e 's/^/  /' | tail -n3)
3adda6
   ----------
3adda6
 This means that the archiving process ended prematurely, or did
3adda6
 not even start. As a result it is unlikely you can recover this
3adda6
 system properly. Relax-and-Recover is therefore aborting execution.
3adda6
 "
3adda6
-        fi;;
3adda6
+            fi
3adda6
+        fi
3adda6
+        ;;
3adda6
     (*)
3adda6
         if (( $backup_prog_rc > 0 )) ; then
3adda6
             Error "$(basename $BACKUP_PROG) failed with return code $backup_prog_rc
3adda6
@@ -212,10 +267,12 @@ esac
3adda6
 
3adda6
 tar_message="$(tac $RUNTIME_LOGFILE | grep -m1 '^Total bytes written: ')"
3adda6
 if [ $backup_prog_rc -eq 0 -a "$tar_message" ] ; then
3adda6
-	LogPrint "$tar_message in $transfertime seconds."
3adda6
+    LogPrint "$tar_message in $transfertime seconds."
3adda6
 elif [ "$size" ]; then
3adda6
-	LogPrint "Archived $((size/1024/1024)) MiB in $((transfertime)) seconds [avg $((size/1024/transfertime)) KiB/sec]"
3adda6
+    LogPrint "Archived $((size/1024/1024)) MiB in $((transfertime)) seconds [avg $((size/1024/transfertime)) KiB/sec]"
3adda6
 fi
3adda6
 
3adda6
 ### Copy progress log to backup media
3adda6
 cp $v "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log" "${opath}/${BACKUP_PROG_ARCHIVE}.log" >&2
3adda6
+
3adda6
+# vim: set et ts=4 sw=4: