diff --git a/zsc-sync.sh b/zsc-sync.sh index 127badd..c9185fe 100755 --- a/zsc-sync.sh +++ b/zsc-sync.sh @@ -26,27 +26,30 @@ if ! lockfile-create -p -r 2 "$0"; then fi -echo "Fetching remote snapshots..." -remote_snapshots=$(ssh $REMOTE zfs list -H -t snapshot -o name -s creation) +update_remote_state() { + echo "Fetching remote snapshots..." + remote_snapshots=$(ssh $REMOTE zfs list -H -t snapshot -o name -s creation) -echo "Fetching subdatasets..." -remote_subdatasets=$(ssh $REMOTE zfs list -H -o name -s creation -r "$REMOTE_ROOT_DATASET") + echo "Fetching subdatasets..." + remote_subdatasets=$(ssh $REMOTE zfs list -H -o name -s creation -r "$REMOTE_ROOT_DATASET") -echo "Fetching resume tokens..." -resume_tokens=$(ssh $REMOTE zfs get -H -p receive_resume_token -o name,value -t filesystem) + echo "Fetching resume tokens..." + resume_tokens=$(ssh $REMOTE zfs get -H -p receive_resume_token -o name,value -t filesystem) +} ### Resume existing transfer ### -# Returns 0 if no action was performed, 1 if a transfer was resumed and finished successfully, -1 if there was an error during resumed transfer +# Returns 0 if no action was performed or transfer is successful, 1 on error resume_transfer() { remote_dataset=$1 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 -F -s "$remote_dataset" || return -1 + zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -F -s "$remote_dataset" || return 1 echo "Resumed transfer finished." - return 1 + update_remote_state + return 0 fi return 0 @@ -75,7 +78,7 @@ handle_dataset() { echo - resume_transfer "$TARGET_REMOTE_DATASET" || return 0 + resume_transfer "$TARGET_REMOTE_DATASET" || return 1 ### Sync existing snapshots ### @@ -133,7 +136,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 0 + resume_transfer "$TARGET_REMOTE_DATASET" || return 1 if [ $(echo -e "$remote_subdatasets" | grep -e "^$TARGET_REMOTE_DATASET\$" | wc -l) -eq 0 ]; then echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..." @@ -164,6 +167,8 @@ init_subdataset() { } +update_remote_state + for line in $(cat "$ZSC_CONFIG") ; do IFS=: read dataset recursive d w m y prefix <<< "$line" echo