Add feature to ask deletion already exsisting snapshots on remote end

This commit is contained in:
2026-09-02 08:20:38 +03:00
parent 2364c8a618
commit f9504eb14a
2 changed files with 27 additions and 0 deletions
+22
View File
@@ -113,6 +113,24 @@ run() {
fi
}
remote_snapshot_exists() {
local remote_snapshot="$1"
ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" \
"sudo -n zfs list -H -o name -t snapshot '$remote_snapshot' >/dev/null 2>&1"
}
remove_remote_snapshot() {
local remote_snapshot="$1"
local reply
printf 'Remote snapshot %s already exists. Delete it and retry? [y/N] ' "$remote_snapshot" >&2
read -r reply
[[ "$reply" =~ ^[Yy]([Ee][Ss])?$ ]] || die "remote snapshot was not deleted; stopping"
ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs destroy '$remote_snapshot'" \
|| die "could not delete remote snapshot $remote_snapshot"
}
mapfile -t FILESYSTEMS < <(zfs list -H -o name -t filesystem -r "$SOURCE_POOL")
((${#FILESYSTEMS[@]} > 0)) || die "no filesystems found in $SOURCE_POOL"
@@ -120,6 +138,7 @@ printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME"
for filesystem in "${FILESYSTEMS[@]}"; do
destination="$(map_destination "$filesystem")"
snapshot="${filesystem}@${SNAPSHOT_NAME}"
remote_snapshot="${destination}@${SNAPSHOT_NAME}"
printf 'Transferring %s to %s:%s\n' "$filesystem" "$REMOTE_HOST" "$destination"
run zfs snapshot "$snapshot"
@@ -129,6 +148,9 @@ for filesystem in "${FILESYSTEMS[@]}"; do
printf ' %q' "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'" >&2
printf '\n' >&2
else
if remote_snapshot_exists "$remote_snapshot"; then
remove_remote_snapshot "$remote_snapshot"
fi
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'"
fi