From 065455440d6115558ce571df07fda5219ea3f05b Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 24 Apr 2021 11:29:51 +0200 Subject: [PATCH] zsc-sync.sh: diversify error codes, fix error detection --- zsc-sync.sh | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/zsc-sync.sh b/zsc-sync.sh index 7114544..1304bf5 100755 --- a/zsc-sync.sh +++ b/zsc-sync.sh @@ -30,11 +30,28 @@ update_remote_state() { echo "Fetching remote snapshots..." remote_snapshots=$(ssh $REMOTE zfs list -H -t snapshot -o name -s creation) + r=$? + if [ $r -ne 0 ]; then + return $r + fi + echo "Fetching subdatasets..." remote_subdatasets=$(ssh $REMOTE zfs list -H -o name -s creation -r "$REMOTE_ROOT_DATASET") + r=$? + if [ $r -ne 0 ]; then + return $r + fi + echo "Fetching resume tokens..." resume_tokens=$(ssh $REMOTE zfs get -H -p receive_resume_token -o name,value -t filesystem) + + r=$? + if [ $r -ne 0 ]; then + return $r + fi + + return 0 } @@ -46,9 +63,9 @@ resume_transfer() { RESUME_TOKEN=$(echo -e "$resume_tokens" | grep -P "^$remote_dataset\t.*\$" | cut -d$'\t' -f2) if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then echo "Resuming interrupted transfer..." - zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -Fu -s "$remote_dataset" || return 1 + zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -Fu -s "$remote_dataset" || return 10 echo "Resumed transfer finished." - update_remote_state + update_remote_state || return 20 return 0 fi @@ -78,7 +95,7 @@ handle_dataset() { echo - resume_transfer "$TARGET_REMOTE_DATASET" || return 1 + resume_transfer "$TARGET_REMOTE_DATASET" || return 11 ### Sync existing snapshots ### @@ -120,7 +137,7 @@ handle_dataset() { if [ $RECURSIVE = true ] ; then recu="-R" fi - zfs send -v -p $recu $incr "$DATASET@$TO_SNAPSHOT" | ssh $REMOTE zfs receive -Fu -s "$TARGET_REMOTE_DATASET" || return 1 + zfs send -v -p $recu $incr "$DATASET@$TO_SNAPSHOT" | ssh $REMOTE zfs receive -Fu -s "$TARGET_REMOTE_DATASET" || return 12 echo echo "Done" @@ -140,7 +157,7 @@ init_subdataset() { TARGET_REMOTE_DATASET="$REMOTE_ROOT_DATASET/"$(echo -e $PARENT_DATASET | sed -r "s/\//_/g")"/"${DATASET:$(echo -e "$PARENT_DATASET" | wc -c)} - resume_transfer "$TARGET_REMOTE_DATASET" || return 1 + resume_transfer "$TARGET_REMOTE_DATASET" || return 13 if [ $(echo -e "$remote_subdatasets" | grep -e "^$TARGET_REMOTE_DATASET\$" | wc -l) -eq 0 ]; then echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..." @@ -149,11 +166,11 @@ init_subdataset() { if test -z "$LOCAL_SNAPSHOT"; then echo "No local snapshot" - return 1 + return 14 fi echo "[$DATASET@$LOCAL_SNAPSHOT -> $TARGET_REMOTE_DATASET]" - zfs send -v -p "$DATASET@$LOCAL_SNAPSHOT" | ssh $REMOTE zfs receive -Fu -s "$TARGET_REMOTE_DATASET" || return 1 + zfs send -v -p "$DATASET@$LOCAL_SNAPSHOT" | ssh $REMOTE zfs receive -Fu -s "$TARGET_REMOTE_DATASET" || return 15 echo echo "--- Done" @@ -162,7 +179,7 @@ init_subdataset() { echo echo "--- Subdataset $TARGET_REMOTE_DATASET is out of date compared to parent dataset. Syncing first..." - handle_dataset "$DATASET" false "" "$TARGET_REMOTE_DATASET" || return 1 + handle_dataset "$DATASET" false "" "$TARGET_REMOTE_DATASET" || return 16 echo echo "--- Done" @@ -173,7 +190,7 @@ init_subdataset() { } -update_remote_state +update_remote_state || exit 21 HAD_ERROR=0 @@ -216,7 +233,7 @@ for line in $(cat "$ZSC_CONFIG") ; do for p in "${SUBDATASETS[@]}"; do init_subdataset $p $dataset "$latest_synchronized_snapshot" r=$? - if [ ! $r ]; then + if [ $r -ne 0 ]; then echo "Error while initing subdataset $p, continuing" if [ $HAD_ERROR ]; then HAD_ERROR=$r @@ -227,12 +244,12 @@ for line in $(cat "$ZSC_CONFIG") ; do echo "----------" echo - update_remote_state + update_remote_state || exit 22 fi handle_dataset $dataset $recursive $prefix r=$? - if [ ! $r ]; then + if [ $r -ne 0 ]; then echo "Error while initing subdataset $p, continuing" if [ $HAD_ERROR ]; then HAD_ERROR=$r