aboutsummaryrefslogtreecommitdiff
path: root/scripts/zfs-replicate
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/zfs-replicate')
-rwxr-xr-xscripts/zfs-replicate25
1 files changed, 21 insertions, 4 deletions
diff --git a/scripts/zfs-replicate b/scripts/zfs-replicate
index 02ffcf5..a4cb15d 100755
--- a/scripts/zfs-replicate
+++ b/scripts/zfs-replicate
@@ -25,9 +25,14 @@ YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
-info() { echo -e "${GREEN}[INFO]${NC} $1"; }
-warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
-error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
+# Diagnostics go to stderr, never stdout. determine_host below runs inside a
+# command substitution, so anything these print on stdout would be captured
+# into TRUENAS_HOST instead of reaching the terminal or the journal -- which is
+# exactly how an unreachable TrueNAS used to abort this script with exit 1 and
+# no output at all. stderr also keeps the captured hostname clean.
+info() { echo -e "${GREEN}[INFO]${NC} $1" >&2; }
+warn() { echo -e "${YELLOW}[WARN]${NC} $1" >&2; }
+error() { echo -e "${RED}[ERROR]${NC} $1" >&2; exit 1; }
command -v syncoid >/dev/null 2>&1 || error "syncoid not found. Install sanoid package."
@@ -58,6 +63,12 @@ fi
info "Starting ZFS replication to $TRUENAS_HOST"
echo ""
+# A failed dataset must not stop the others -- but it must not be forgotten
+# either. This runs as a systemd oneshot on a nightly timer, so the exit code
+# is the only signal anyone sees; exiting 0 after every dataset failed made a
+# backup that had not run in months look identical to a working one.
+failed=0
+
for dataset in $DATASETS; do
dest="$TRUENAS_USER@$TRUENAS_HOST:$TRUENAS_POOL/$BACKUP_PATH/${dataset#zroot/}"
info "Replicating $dataset -> $dest"
@@ -66,8 +77,14 @@ for dataset in $DATASETS; do
info " Success"
else
warn " Failed (will retry next run)"
+ failed=$((failed + 1))
fi
- echo ""
+ echo "" >&2
done
+if [ "$failed" -gt 0 ]; then
+ warn "Replication complete with $failed of $(echo "$DATASETS" | wc -w) datasets failed."
+ exit 1
+fi
+
info "Replication complete."