diff --git a/zsc-sync.sh b/zsc-sync.sh index d89ec71..e5d9286 100755 --- a/zsc-sync.sh +++ b/zsc-sync.sh @@ -107,6 +107,49 @@ handle_dataset() { fi } +init_subdataset() { + DATASET=$1 + PARENT_DATASET=$2 + + TARGET_REMOTE_DATASET="$REMOTE_ROOT_DATASET/"$(echo -e $PARENT_DATASET | sed -r "s/\//_/g")"/"${DATASET:$(echo -e "$PARENT_DATASET" | wc -c)} + REMOTE_DATASETS=($(ssh $REMOTE zfs list -H -o name -s creation -r $REMOTE_ROOT_DATASET)) + + + ### Resume existing transfer ### + RESUME_TOKEN=$(ssh $REMOTE zfs get -H -p receive_resume_token -o value "$TARGET_REMOTE_DATASET") + if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then + echo "Resuming interrupted transfer..." + zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1 + echo "Resumed transfer finished." + return 0 + fi + + + found=false + for d in "${REMOTE_DATASETS[@]}"; do + if [ "$TARGET_REMOTE_DATASET" == "$d" ]; then + found=true + break + fi + done + + if [ $found = false ]; then + echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..." + + LOCAL_SNAPSHOT=($(zfs list -H -t snapshot -o name -s creation "$DATASET" | cut -d"@" -f 2 | tail -n 1)) + + if test -z "$LOCAL_SNAPSHOT"; then + echo "No local snapshot" + return 1 + fi + + echo "[$DATASET@$LOCAL_SNAPSHOT -> $TARGET_REMOTE_DATASET]" + zfs send -v -p "$DATASET@$LOCAL_SNAPSHOT" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1 + + echo -e "Done" + fi +} + for line in $(cat "$ZSC_CONFIG") ; do IFS=: read dataset recursive d w m y prefix <<< "$line" @@ -116,10 +159,9 @@ for line in $(cat "$ZSC_CONFIG") ; do SUBDATASETS=($(zfs list -o name | grep "$dataset/")) echo -e "| Subdatasets: ${SUBDATASETS[@]}" echo -e '|' - #for p in "${SUBDATASETS[@]}"; do - # handle_dataset $p $d $w $m $y $prefix | awk '{print "| " $0}' || exit 1 - # echo -e '|' - #done + for p in "${SUBDATASETS[@]}"; do + init_subdataset $p $dataset | awk '{print "| " $0}' || exit 1 + done fi handle_dataset $dataset $recursive $d $w $m $y $prefix | awk '{print "| " $0}' || exit 1