Blame SOURCES/rear-bz1631183.patch

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