This commit is contained in:
2026-10-02 12:45:03 +03:00
parent 6e5f2ddde8
commit cf246d1329
2 changed files with 336 additions and 251 deletions
+261 -250
View File
@@ -1,251 +1,262 @@
# ZFS Move Over SSH # ZFS Move Over SSH
`zfs-move-over-ssh.sh` snapshots every ZFS filesystem in a source pool, then `zfs-move-over-ssh.sh` snapshots every ZFS filesystem in a source pool, then
sends each snapshot to a remote ZFS server over SSH. A vdev is the underlying sends each snapshot to a remote ZFS server over SSH. A vdev is the underlying
storage device; ZFS filesystems belong to a pool, so pass the pool name. storage device; ZFS filesystems belong to a pool, so pass the pool name.
For a source tree such as: For a source tree such as:
```text ```text
tank/data tank/data
tank/data/documents tank/data/documents
tank/data/media tank/data/media
``` ```
and destination root `backup/data`, the script sends the filesystems to: and destination root `backup/data`, the script sends the filesystems to:
```text ```text
backup/data backup/data
backup/data/documents backup/data/documents
backup/data/media backup/data/media
``` ```
Each local snapshot is named `move-YYYYMMDD-HHMMSS`. Each local snapshot is named `move-YYYYMMDD-HHMMSS`.
## Requirements ## Requirements
- Run the script as an account that can use `zfs` on the source server. - Run the script as an account that can use `zfs` on the source server.
- The source host needs Bash, ZFS utilities, and SSH. - The source host needs Bash, ZFS utilities, and SSH.
- The destination host needs ZFS utilities and the destination pool must exist. - The destination host needs ZFS utilities and the destination pool must exist.
- Both hosts need `rsync` to run the verification script. - Both hosts need `rsync` to run the verification script.
- The SSH account on the destination must be able to execute `sudo -n zfs` - The SSH account on the destination must be able to execute `sudo -n zfs`
without a password prompt. without a password prompt.
- For verification, that account must also be allowed to run `sudo -n rsync` - For verification, that account must also be allowed to run `sudo -n rsync`
without a password prompt. without a password prompt.
- SSH must use a private key or SSH agent. Password authentication is disabled - SSH must use a private key or SSH agent. Password authentication is disabled
by the script so a migration cannot pause and prompt once per dataset. by the script so a migration cannot pause and prompt once per dataset.
- SSH connectivity and host-key verification must already work. - SSH connectivity and host-key verification must already work.
The script is intended for one-shot full snapshot transfers. It does not use The script is intended for one-shot full snapshot transfers. It does not use
incremental sends or resume tokens. incremental sends or resume tokens.
## Usage ## Usage
```bash ```bash
bash zfs-move-over-ssh.sh [options] SOURCE_POOL REMOTE_HOST DESTINATION_ROOT bash zfs-move-over-ssh.sh [options] SOURCE_POOL REMOTE_HOST DESTINATION_ROOT
``` ```
Example: Example:
```bash ```bash
bash zfs-move-over-ssh.sh tank root@new-server backup/tank bash zfs-move-over-ssh.sh tank root@new-server backup/tank
``` ```
Verify the resulting filesystems after mounting them on both hosts: Verify the resulting filesystems after mounting them on both hosts:
```bash ```bash
bash zfs-verify-over-ssh.sh tank root@new-server backup/tank bash zfs-verify-over-ssh.sh tank root@new-server backup/tank
``` ```
The verifier maps datasets using the same relative paths as the move script. The verifier maps datasets using the same relative paths as the move script.
It uses a read-only `rsync --dry-run --checksum` comparison and exits with It uses a read-only `rsync --dry-run --checksum` comparison and exits with
status `1` when it finds missing, extra, changed, or metadata-different files. status `1` when it finds missing, extra, changed, or metadata-different files.
Use a non-standard SSH key or port when needed: On a terminal (not with `--dry-run`), each filesystem shows a live progress block:
```bash ```text
bash zfs-move-over-ssh.sh \ [3/12] tank/data/media
--identity-file ~/.ssh/zfs-migration \ Dataset [##########--------------------] 34% 1.2GB / 3.4GB 45.2MB/s ETA 0:00:51
--ssh-port 2222 \ Overall [######------------------------] 21%
tank admin@new-server backup/tank ```
```
The overall bar is weighted by each filesystem's logical size. Output that is
## Verification not a terminal falls back to plain output.
`zfs-verify-over-ssh.sh` compares each ZFS filesystem below `SOURCE_POOL` to Use a non-standard SSH key or port when needed:
the corresponding filesystem below `DESTINATION_ROOT`. For example,
`tank/data/documents` is compared with `backup/tank/data/documents`. ```bash
bash zfs-move-over-ssh.sh \
```bash --identity-file ~/.ssh/zfs-migration \
bash zfs-verify-over-ssh.sh [options] SOURCE_POOL REMOTE_HOST DESTINATION_ROOT --ssh-port 2222 \
``` tank admin@new-server backup/tank
```
Both sides of every filesystem pair must be mounted. The transfer script uses
`zfs receive -u`, so mount the received remote datasets before verification: ## Verification
```bash `zfs-verify-over-ssh.sh` compares each ZFS filesystem below `SOURCE_POOL` to
ssh admin@new-server 'sudo zfs mount -a' the corresponding filesystem below `DESTINATION_ROOT`. For example,
bash zfs-verify-over-ssh.sh tank admin@new-server backup/tank `tank/data/documents` is compared with `backup/tank/data/documents`.
```
```bash
The verifier uses `rsync` with `--dry-run`, `--checksum`, `--delete`, archive bash zfs-verify-over-ssh.sh [options] SOURCE_POOL REMOTE_HOST DESTINATION_ROOT
metadata preservation, hard-link checking, ACL checking, extended-attribute ```
checking, and `--one-file-system`. It does not modify either host. Any
itemized `rsync` output identifies an extra, missing, changed, or Both sides of every filesystem pair must be mounted. The transfer script uses
metadata-different path. `zfs receive -u`, so mount the received remote datasets before verification:
The verifier continues with the next filesystem when one dataset cannot be ```bash
checked. It prints an `ERROR` line for each failed dataset and summarizes the ssh admin@new-server 'sudo zfs mount -a'
number of uncheckable filesystems at the end, so one run shows all failures bash zfs-verify-over-ssh.sh tank admin@new-server backup/tank
encountered along the way. ```
Verification options: The verifier uses `rsync` with `--dry-run`, `--checksum`, `--delete`, archive
metadata preservation, hard-link checking, ACL checking, extended-attribute
| Option | Description | checking, and `--one-file-system`. It does not modify either host. Any
| --- | --- | itemized `rsync` output identifies an extra, missing, changed, or
| `-i`, `--identity-file FILE` | Use the specified SSH private key. | metadata-different path.
| `-p`, `--ssh-port PORT` | Connect to the specified SSH port. |
| `-h`, `--help` | Show command help. | The verifier continues with the next filesystem when one dataset cannot be
checked. It prints an `ERROR` line for each failed dataset and summarizes the
Use the same key and port options as the transfer when required: number of uncheckable filesystems at the end, so one run shows all failures
encountered along the way.
```bash
bash zfs-verify-over-ssh.sh \ Verification options:
--identity-file ~/.ssh/zfs-migration \
--ssh-port 2222 \ | Option | Description |
tank admin@new-server backup/tank | --- | --- |
``` | `-i`, `--identity-file FILE` | Use the specified SSH private key. |
| `-p`, `--ssh-port PORT` | Connect to the specified SSH port. |
Exit statuses: | `-h`, `--help` | Show command help. |
| Status | Meaning | Use the same key and port options as the transfer when required:
| --- | --- |
| `0` | All checked filesystems are identical. | ```bash
| `1` | All filesystem pairs were checked, but one or more have differences. | bash zfs-verify-over-ssh.sh \
| `2` | One or more filesystem pairs could not be checked, for example because a dataset is unavailable or unmounted, or its SSH, sudo, or `rsync` comparison failed. The script continues and lists these errors before exiting. | --identity-file ~/.ssh/zfs-migration \
--ssh-port 2222 \
For a stable result, stop writes to the source and destination while the tank admin@new-server backup/tank
verifier runs, or verify mounted clones of the same snapshots. The verifier ```
checks file content and selected filesystem metadata; it does not compare ZFS
dataset properties, snapshots, quotas, reservations, or encryption settings. Exit statuses:
## SSH Setup | Status | Meaning |
| --- | --- |
Create a dedicated key on the source server if one does not already exist: | `0` | All checked filesystems are identical. |
| `1` | All filesystem pairs were checked, but one or more have differences. |
```bash | `2` | One or more filesystem pairs could not be checked, for example because a dataset is unavailable or unmounted, or its SSH, sudo, or `rsync` comparison failed. The script continues and lists these errors before exiting. |
ssh-keygen -t ed25519 -f ~/.ssh/zfs-migration -C zfs-migration
``` For a stable result, stop writes to the source and destination while the
verifier runs, or verify mounted clones of the same snapshots. The verifier
Install its public key for the remote account. This one command may ask for the checks file content and selected filesystem metadata; it does not compare ZFS
remote account password during setup; the migration itself will not: dataset properties, snapshots, quotas, reservations, or encryption settings.
```bash ## SSH Setup
ssh-copy-id -i ~/.ssh/zfs-migration.pub admin@new-server
``` Create a dedicated key on the source server if one does not already exist:
Verify that authentication and remote ZFS access work without either password: ```bash
ssh-keygen -t ed25519 -f ~/.ssh/zfs-migration -C zfs-migration
```bash ```
ssh -i ~/.ssh/zfs-migration -o BatchMode=yes admin@new-server \
'sudo -n zfs list' Install its public key for the remote account. This one command may ask for the
``` remote account password during setup; the migration itself will not:
If this command fails, configure the remote account's `sudoers` entry for the ```bash
necessary `zfs` commands with `NOPASSWD` before running the migration. Do not ssh-copy-id -i ~/.ssh/zfs-migration.pub admin@new-server
put a password in the script or on its command line. ```
Use the exact same account, hostname, key, and port in the script as in this Verify that authentication and remote ZFS access work without either password:
successful test. For example, if `admin@new-server` succeeds, run:
```bash
```bash ssh -i ~/.ssh/zfs-migration -o BatchMode=yes admin@new-server \
bash zfs-move-over-ssh.sh \ 'sudo -n zfs list'
-i ~/.ssh/zfs-migration \ ```
tank \
admin@new-server \ If this command fails, configure the remote account's `sudoers` entry for the
backup/tank necessary `zfs` commands with `NOPASSWD` before running the migration. Do not
``` put a password in the script or on its command line.
Do not substitute `root@new-server` for `admin@new-server` unless a separate Use the exact same account, hostname, key, and port in the script as in this
non-interactive key test succeeds for `root`. SSH keys and `sudo` permissions successful test. For example, if `admin@new-server` succeeds, run:
are configured per remote user, so a key that works for `admin` may be denied
for `root`. ```bash
bash zfs-move-over-ssh.sh \
`REMOTE_HOST` must be a complete hostname or address, such as -i ~/.ssh/zfs-migration \
`root@192.0.2.10` or `admin@new-server`. `root@` is invalid because it has no tank \
host after the `@`. admin@new-server \
backup/tank
Options: ```
| Option | Description | Do not substitute `root@new-server` for `admin@new-server` unless a separate
| --- | --- | non-interactive key test succeeds for `root`. SSH keys and `sudo` permissions
| `-n`, `--dry-run` | Print planned snapshot, send, and receive commands without running them. | are configured per remote user, so a key that works for `admin` may be denied
| `-i`, `--identity-file FILE` | Use the specified SSH private key. | for `root`.
| `-p`, `--ssh-port PORT` | Connect to the specified SSH port. |
| `--destroy-source-snapshots` | Remove each local migration snapshot only after its receive succeeds. | `REMOTE_HOST` must be a complete hostname or address, such as
| `-h`, `--help` | Show command help. | `root@192.0.2.10` or `admin@new-server`. `root@` is invalid because it has no
host after the `@`.
## Recommended Procedure
Options:
1. Confirm the source datasets:
| Option | Description |
```bash | --- | --- |
zfs list -r tank | `-n`, `--dry-run` | Print planned snapshot, send, and receive commands without running them. |
``` | `-i`, `--identity-file FILE` | Use the specified SSH private key. |
| `-p`, `--ssh-port PORT` | Connect to the specified SSH port. |
2. Confirm remote privileged ZFS access: | `--destroy-source-snapshots` | Remove each local migration snapshot only after its receive succeeds. |
| `-h`, `--help` | Show command help. |
```bash
ssh admin@new-server 'sudo -n zfs list' ## Recommended Procedure
```
1. Confirm the source datasets:
3. Preview the operation. Review every destination name in the output:
```bash
```bash zfs list -r tank
bash zfs-move-over-ssh.sh --dry-run tank admin@new-server backup/tank ```
```
2. Confirm remote privileged ZFS access:
4. Run the migration:
```bash
```bash ssh admin@new-server 'sudo -n zfs list'
bash zfs-move-over-ssh.sh tank admin@new-server backup/tank ```
```
3. Preview the operation. Review every destination name in the output:
5. Mount the received datasets, then verify their data before retiring the
source. `zfs-move-over-ssh.sh` receives with `-u`, so the destination filesystems ```bash
are unmounted until you explicitly mount them: bash zfs-move-over-ssh.sh --dry-run tank admin@new-server backup/tank
```
```bash
ssh admin@new-server 'sudo zfs mount -a' 4. Run the migration:
bash zfs-verify-over-ssh.sh tank admin@new-server backup/tank
``` ```bash
bash zfs-move-over-ssh.sh tank admin@new-server backup/tank
## Safety and Recovery ```
- The script does **not** destroy source filesystems or source data. 5. Mount the received datasets, then verify their data before retiring the
- By default, the generated local snapshots remain on the source. This allows a source. `zfs-move-over-ssh.sh` receives with `-u`, so the destination filesystems
failed run to be inspected or sent again. Remove them manually only after are unmounted until you explicitly mount them:
verification, or use `--destroy-source-snapshots` once the workflow is trusted.
- The remote command is `zfs receive -uF`. `-u` leaves received filesystems ```bash
unmounted. `-F` can roll back the destination filesystem and destroy newer ssh admin@new-server 'sudo zfs mount -a'
snapshots or changes that conflict with the incoming snapshot. Do not point bash zfs-verify-over-ssh.sh tank admin@new-server backup/tank
the script at a destination containing data you need unless you have reviewed ```
the dry-run output and have a current backup.
- The script processes one filesystem at a time. If it stops partway through, ## Safety and Recovery
completed destination filesystems remain received. Re-running with the same
source can create another timestamped snapshot; inspect both source and - The script does **not** destroy source filesystems or source data.
destination state before retrying. - By default, the generated local snapshots remain on the source. This allows a
- Before each receive, the script checks whether the remote destination already failed run to be inspected or sent again. Remove them manually only after
has the matching `move-YYYYMMDD-HHMMSS` snapshot. If it does, the script asks verification, or use `--destroy-source-snapshots` once the workflow is trusted.
whether to delete that specific remote snapshot and retry the transfer. Type - The remote command is `zfs receive -uF`. `-u` leaves received filesystems
`y` only after confirming it is an incomplete or disposable migration unmounted. `-F` can roll back the destination filesystem and destroy newer
snapshot; any other response stops the script without deleting it. snapshots or changes that conflict with the incoming snapshot. Do not point
- A successful transfer is not a destructive move. Verify applications and the script at a destination containing data you need unless you have reviewed
data on the destination, then perform any source cleanup separately. the dry-run output and have a current backup.
- The script processes one filesystem at a time. If it stops partway through,
## Notes completed destination filesystems remain received. Re-running with the same
source can create another timestamped snapshot; inspect both source and
The destination root itself does not have to exist before the transfer, but destination state before retrying.
its parent pool or filesystem must exist and the remote account must have ZFS - Before each receive, the script checks whether the remote destination already
permission to create the target datasets. Dataset properties are included in has the matching `move-YYYYMMDD-HHMMSS` snapshot. If it does, the script asks
whether to delete that specific remote snapshot and retry the transfer. Type
`y` only after confirming it is an incomplete or disposable migration
snapshot; any other response stops the script without deleting it.
- A successful transfer is not a destructive move. Verify applications and
data on the destination, then perform any source cleanup separately.
## Notes
The destination root itself does not have to exist before the transfer, but
its parent pool or filesystem must exist and the remote account must have ZFS
permission to create the target datasets. Dataset properties are included in
the stream through `zfs send -p`. the stream through `zfs send -p`.
+75 -1
View File
@@ -134,6 +134,70 @@ remove_remote_snapshot() {
mapfile -t FILESYSTEMS < <(zfs list -H -o name -t filesystem -r "$SOURCE_POOL") mapfile -t FILESYSTEMS < <(zfs list -H -o name -t filesystem -r "$SOURCE_POOL")
((${#FILESYSTEMS[@]} > 0)) || die "no filesystems found in $SOURCE_POOL" ((${#FILESYSTEMS[@]} > 0)) || die "no filesystems found in $SOURCE_POOL"
# Progress UI needs a terminal and a real transfer.
UI=false
if ! $DRY_RUN && [[ -t 1 ]]; then
UI=true
exec 4>&1
fi
# Per-filesystem weights so the overall bar reflects data size, not dataset count.
declare -A WEIGHT=()
TOTAL_WEIGHT=0
if $UI; then
while IFS=$'\t' read -r fs_name fs_weight; do
[[ "$fs_weight" =~ ^[0-9]+$ ]] || fs_weight=0
WEIGHT["$fs_name"]=$fs_weight
TOTAL_WEIGHT=$((TOTAL_WEIGHT + fs_weight))
done < <(zfs list -Hp -o name,logicalreferenced -t filesystem -r "$SOURCE_POOL")
if ((TOTAL_WEIGHT == 0)); then
for fs_name in "${FILESYSTEMS[@]}"; do WEIGHT["$fs_name"]=1; done
TOTAL_WEIGHT=${#FILESYSTEMS[@]}
fi
fi
# Reads `zfs send -P -v` output on stdin and redraws a 3-line block on fd 4.
send_progress() {
local idx="$1" count="$2" name="$3" prior_w="$4" cur_w="$5" total_w="$6"
awk -F'\t' -v idx="$idx" -v count="$count" -v name="$name" \
-v prior_w="$prior_w" -v cur_w="$cur_w" -v total_w="$total_w" '
function human(b, u, i) {
split("B KB MB GB TB PB", u, " "); i = 1
while (b >= 1024 && i < 6) { b /= 1024; i++ }
return sprintf("%.1f%s", b, u[i])
}
function bar(p, n, s, i) {
if (p > 100) p = 100
n = int(p * 30 / 100); s = ""
for (i = 0; i < 30; i++) s = s (i < n ? "#" : "-")
return "[" s "]"
}
function hms(s) { return sprintf("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60) }
function render( pct, op, eta) {
pct = (est > 0) ? b * 100 / est : 0
if (pct > 100) pct = 100
op = (total_w > 0) ? (prior_w + cur_w * pct / 100) * 100 / total_w : 0
eta = (rate > 0 && est > b) ? " ETA " hms((est - b) / rate) : ""
printf "\033[3A\r\033[2K [%d/%d] %s\n", idx, count, name
printf "\033[2K Dataset %s %3d%% %s / %s %s/s%s\n", bar(pct), pct, human(b), human(est), human(rate), eta
printf "\033[2K Overall %s %3d%%\n", bar(op), op
}
BEGIN { printf "\n\n\n" }
$1 == "size" { est = $2 + 0; render(); next }
$1 == "full" || $1 == "incremental" { next }
$1 ~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/ {
split($1, t, ":"); s = t[1] * 3600 + t[2] * 60 + t[3]; b = $2 + 0
if (seen && s > ls) { inst = (b - lb) / (s - ls); rate = rate ? 0.7 * rate + 0.3 * inst : inst }
ls = s; lb = b; seen = 1; render(); next
}
{ printf "\033[3A\033[J%s\n\n\n\n", $0 }
END { if (est > 0) b = est; render() }
' >&4 3>&-
}
prior_weight=0
index_now=0
printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME" printf 'Migration snapshot: %s\n' "$SNAPSHOT_NAME"
for filesystem in "${FILESYSTEMS[@]}"; do for filesystem in "${FILESYSTEMS[@]}"; do
destination="$(map_destination "$filesystem")" destination="$(map_destination "$filesystem")"
@@ -151,7 +215,17 @@ for filesystem in "${FILESYSTEMS[@]}"; do
if remote_snapshot_exists "$remote_snapshot"; then if remote_snapshot_exists "$remote_snapshot"; then
remove_remote_snapshot "$remote_snapshot" remove_remote_snapshot "$remote_snapshot"
fi fi
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'" if $UI; then
index_now=$((index_now + 1))
{
zfs send -p -P -v "$snapshot" 2>&1 1>&3 3>&- \
| send_progress "$index_now" "${#FILESYSTEMS[@]}" "$filesystem" \
"$prior_weight" "${WEIGHT[$filesystem]:-0}" "$TOTAL_WEIGHT"
} 3>&1 | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'"
prior_weight=$((prior_weight + ${WEIGHT[$filesystem]:-0}))
else
zfs send -p "$snapshot" | ssh "${SSH_OPTIONS[@]}" "$REMOTE_HOST" "sudo -n zfs receive -uF '$destination'"
fi
fi fi
if $DESTROY_SOURCE_SNAPSHOTS; then if $DESTROY_SOURCE_SNAPSHOTS; then