Blame SOURCES/rear-bz1631183.patch

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