zsc-sync.sh: diversify error codes, fix error detection

This commit is contained in:
Alice Gaudon 2021-04-24 11:29:51 +02:00
parent e0af0c4021
commit 065455440d

View File

@ -30,11 +30,28 @@ update_remote_state() {
echo "Fetching remote snapshots..." echo "Fetching remote snapshots..."
remote_snapshots=$(ssh $REMOTE zfs list -H -t snapshot -o name -s creation) 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..." echo "Fetching subdatasets..."
remote_subdatasets=$(ssh $REMOTE zfs list -H -o name -s creation -r "$REMOTE_ROOT_DATASET") 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..." echo "Fetching resume tokens..."
resume_tokens=$(ssh $REMOTE zfs get -H -p receive_resume_token -o name,value -t filesystem) 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) RESUME_TOKEN=$(echo -e "$resume_tokens" | grep -P "^$remote_dataset\t.*\$" | cut -d$'\t' -f2)
if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then
echo "Resuming interrupted transfer..." 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." echo "Resumed transfer finished."
update_remote_state update_remote_state || return 20
return 0 return 0
fi fi
@ -78,7 +95,7 @@ handle_dataset() {
echo echo
resume_transfer "$TARGET_REMOTE_DATASET" || return 1 resume_transfer "$TARGET_REMOTE_DATASET" || return 11
### Sync existing snapshots ### ### Sync existing snapshots ###
@ -120,7 +137,7 @@ handle_dataset() {
if [ $RECURSIVE = true ] ; then if [ $RECURSIVE = true ] ; then
recu="-R" recu="-R"
fi 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
echo "Done" 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)} 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 if [ $(echo -e "$remote_subdatasets" | grep -e "^$TARGET_REMOTE_DATASET\$" | wc -l) -eq 0 ]; then
echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..." echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..."
@ -149,11 +166,11 @@ init_subdataset() {
if test -z "$LOCAL_SNAPSHOT"; then if test -z "$LOCAL_SNAPSHOT"; then
echo "No local snapshot" echo "No local snapshot"
return 1 return 14
fi fi
echo "[$DATASET@$LOCAL_SNAPSHOT -> $TARGET_REMOTE_DATASET]" 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
echo "--- Done" echo "--- Done"
@ -162,7 +179,7 @@ init_subdataset() {
echo echo
echo "--- Subdataset $TARGET_REMOTE_DATASET is out of date compared to parent dataset. Syncing first..." 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
echo "--- Done" echo "--- Done"
@ -173,7 +190,7 @@ init_subdataset() {
} }
update_remote_state update_remote_state || exit 21
HAD_ERROR=0 HAD_ERROR=0
@ -216,7 +233,7 @@ for line in $(cat "$ZSC_CONFIG") ; do
for p in "${SUBDATASETS[@]}"; do for p in "${SUBDATASETS[@]}"; do
init_subdataset $p $dataset "$latest_synchronized_snapshot" init_subdataset $p $dataset "$latest_synchronized_snapshot"
r=$? r=$?
if [ ! $r ]; then if [ $r -ne 0 ]; then
echo "Error while initing subdataset $p, continuing" echo "Error while initing subdataset $p, continuing"
if [ $HAD_ERROR ]; then if [ $HAD_ERROR ]; then
HAD_ERROR=$r HAD_ERROR=$r
@ -227,12 +244,12 @@ for line in $(cat "$ZSC_CONFIG") ; do
echo "----------" echo "----------"
echo echo
update_remote_state update_remote_state || exit 22
fi fi
handle_dataset $dataset $recursive $prefix handle_dataset $dataset $recursive $prefix
r=$? r=$?
if [ ! $r ]; then if [ $r -ne 0 ]; then
echo "Error while initing subdataset $p, continuing" echo "Error while initing subdataset $p, continuing"
if [ $HAD_ERROR ]; then if [ $HAD_ERROR ]; then
HAD_ERROR=$r HAD_ERROR=$r