From 8c9ba67296344eeba0bbf5616867fb1b273e0a5d Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Tue, 9 Feb 2021 16:22:32 +1100
Subject: [PATCH] borg-backup: save PIPESTATUS before referencing

It's not obvious, but the if statements can change the PIPESTATUS
meaning we're not matching what we think we're matching.  Save the
pipestatus of the backup commands so we exit the backup script with
the right code.

Change-Id: I83c7db45d3622067eb05107e26fbdc7a8aeecf63
---
 playbooks/roles/borg-backup/templates/borg-backup.j2 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/playbooks/roles/borg-backup/templates/borg-backup.j2 b/playbooks/roles/borg-backup/templates/borg-backup.j2
index 5a05b6e3f9..af9606e435 100644
--- a/playbooks/roles/borg-backup/templates/borg-backup.j2
+++ b/playbooks/roles/borg-backup/templates/borg-backup.j2
@@ -43,12 +43,13 @@ do
     bash $f | ${BORG_CREATE} --stdin-name ${stream_name} \
                       ::"{hostname}-${stream_name}-{now}" -
 
-    if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
+    _status=( "${PIPESTATUS[@]}" )
+    if [[ ${_status[0]} -ne 0 ]]; then
         info "Streaming script ${f} failed!"
-        stream_exit=${PIPESTATUS[0]}
-    elif [[ ${PIPESTATUS[1]} -ne 1 ]]; then
+        stream_exit=${_status[0]}
+    elif [[ ${_status[1]} -ne 1 ]]; then
         info "Borg failed!"
-        stream_exit=${PIPESTATUS[1]}
+        stream_exit=${_status[1]}
     else
         stream_exit=0
     fi