commit d242955f4e9808e7ab3a3372e028de19fae7aa6e Author: Tero Date: Thu Oct 1 10:42:36 2026 +0300 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..be1ef9c --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# QNAP → TrueNAS Migration + +Copies all shares from one or more QNAP NAS devices into TrueNAS SCALE datasets. +Run **on the TrueNAS box as root** — it pulls data from each QNAP (nothing to install on QNAP besides enabling the service you choose below). + +## 1. Enable access on each QNAP + +- **ssh** (recommended, fastest, preserves permissions best): Control Panel → Telnet/SSH → enable SSH. Copy a TrueNAS SSH public key into the QNAP admin user's `~/.ssh/authorized_keys` for passwordless auth. +- **smb**: make sure the shares are accessible to an admin/user account over SMB. +- **nfs**: Control Panel → Network & File Services → NFS service, and allow the TrueNAS host/IP in each share's NFS permissions. + +## 2. Configure the NAS list + +```bash +cp nas_list.conf.example nas_list.conf +``` + +Edit `nas_list.conf` — one line per QNAP, see comments in the file for the column format +(`name method host user auth shares`). Mix methods freely (e.g. one QNAP over ssh, another over smb). + +For `ssh` entries, generate the key pair(s) referenced in the `auth` column and install them on +each QNAP: + +```bash +chmod +x generate_ssh_keys.sh +./generate_ssh_keys.sh --copy # creates missing keys, prompts once per QNAP to install them +``` + +Run it again without `--copy` any time to just (re)generate missing keys without touching the QNAP. + +## 3. Dry run first + +```bash +chmod +x migrate.sh +./migrate.sh --list-only # just show discovered shares per NAS +./migrate.sh --dry-run # simulate the full rsync, no files written +``` + +## 4. Run for real + +```bash +./migrate.sh +``` + +Data lands under `/mnt/stor0/qnap///`. Per-share rsync logs are written to +`/mnt/stor0/qnap/_logs/`. + +## Options + +| Flag | Description | +|----------------|------------------------------------------------------------| +| `--config FILE`| Use a different NAS list (default `./nas_list.conf`) | +| `--dest DIR` | Destination root (default `/mnt/stor0/qnap`) | +| `--only NAME` | Only process one NAS entry by name | +| `--jobs N` | Number of NAS devices to process in parallel (default 1) | +| `--dry-run` | Pass `--dry-run` to rsync, no changes made | +| `--list-only` | Only discover and print shares, don't transfer | + +QNAP system folders (`@Recycle`, `.@__thumb`, `.system`, snapshots, etc.) are skipped automatically. +Re-running the script is safe/idempotent — rsync only copies new or changed files. diff --git a/generate_ssh_keys.sh b/generate_ssh_keys.sh new file mode 100644 index 0000000..352d0a2 --- /dev/null +++ b/generate_ssh_keys.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +# +# generate_ssh_keys.sh - Create SSH key pairs for the QNAP devices listed in +# nas_list.conf that use method "ssh", and optionally install the public key +# on each QNAP for passwordless auth. +# +# Run this ON TrueNAS, as the same user that will run migrate.sh (root). +# +# Usage: +# ./generate_ssh_keys.sh [--config FILE] [--only NAME] [--copy] [-h|--help] +# +# --config FILE NAS list to read (default ./nas_list.conf) +# --only NAME Only process one NAS entry by name +# --copy Also run ssh-copy-id to install the key on the QNAP +# (prompts once for the QNAP admin password per device) +# +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +CONFIG_FILE="${SCRIPT_DIR}/nas_list.conf" +ONLY_NAME="" +COPY_KEY=0 + +usage() { + grep '^#' "$0" | sed -n '2,15p' | sed 's/^# \{0,1\}//' + exit 1 +} + +log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; } +die() { log "ERROR: $*" >&2; exit 1; } + +while [ $# -gt 0 ]; do + case "$1" in + --config) CONFIG_FILE="$2"; shift 2 ;; + --only) ONLY_NAME="$2"; shift 2 ;; + --copy) COPY_KEY=1; shift ;; + -h|--help) usage ;; + *) die "unknown argument: $1" ;; + esac +done + +[ -f "$CONFIG_FILE" ] || die "config file not found: $CONFIG_FILE (copy nas_list.conf.example)" +command -v ssh-keygen >/dev/null || die "ssh-keygen not found" + +while IFS= read -r line || [ -n "$line" ]; do + line="${line%%#*}" + [ -z "${line// /}" ] && continue + read -r name method host user auth shares <<< "$line" + [ -n "$name" ] || continue + [ "$method" = "ssh" ] || continue + [ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue + + if [ "$auth" = "-" ]; then + log "[$name] auth is '-' in config (use ssh-agent/default key), skipping generation" + continue + fi + + mkdir -p "$(dirname "$auth")" + chmod 700 "$(dirname "$auth")" + + if [ -f "$auth" ]; then + log "[$name] key already exists at $auth, skipping generation" + else + log "[$name] generating ed25519 key at $auth" + ssh-keygen -t ed25519 -f "$auth" -N "" -C "truenas-migration-${name}" + chmod 600 "$auth" + chmod 644 "${auth}.pub" + fi + + if [ "$COPY_KEY" -eq 1 ]; then + command -v ssh-copy-id >/dev/null || die "ssh-copy-id not found, install openssh-client" + log "[$name] installing public key on ${user}@${host} (password prompt follows)" + ssh-copy-id -i "${auth}.pub" -o StrictHostKeyChecking=accept-new "${user}@${host}" \ + || log "[$name] ssh-copy-id failed, add ${auth}.pub to the QNAP manually" + fi +done < "$CONFIG_FILE" + +log "Done. Test access with: ssh -i @" diff --git a/migrate.sh b/migrate.sh new file mode 100644 index 0000000..3038940 --- /dev/null +++ b/migrate.sh @@ -0,0 +1,274 @@ +#!/usr/bin/env bash +# +# migrate.sh - Copy all shares from one or more QNAP NAS devices to TrueNAS SCALE. +# Run this script ON the TrueNAS box (as root) - it pulls data from each QNAP. +# +# Supports three transfer methods per NAS entry: +# ssh - rsync over SSH directly against the QNAP (no mount needed, QNAP SSH service must be on) +# smb - mounts the QNAP share over CIFS/SMB, then rsyncs locally +# nfs - mounts the QNAP share over NFS, then rsyncs locally +# +# NAS devices + shares are declared in a config file (see nas_list.conf.example). +# +# Usage: +# ./migrate.sh [--config FILE] [--dest DIR] [--only NAME] [--jobs N] +# [--dry-run] [--list-only] [-h|--help] +# +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +CONFIG_FILE="${SCRIPT_DIR}/nas_list.conf" +DEST_ROOT="/mnt/stor0/qnap" +ONLY_NAME="" +JOBS=1 +DRY_RUN=0 +LIST_ONLY=0 + +# QNAP system/internal folders that must never be copied or treated as real shares. +SYSTEM_SHARE_EXCLUDES=( + ".system" ".system_trash" ".qsync_trash" ".@__thumb" ".@__thumb_mobile" + "@Recycle" ".@Recycle" "@Recently-Snapshot" "@Transcode" ".streams" + ".acronis_mms" "Usage" "Network Recycle Bin" "homes" +) +# Junk left inside shares themselves, excluded from every rsync transfer. +RSYNC_EXCLUDES=( + "@Recycle/" ".@__thumb/" ".@__thumb_mobile/" "@Transcode/" ".streams/" + ".qsync_trash/" "Thumbs.db" ".DS_Store" "lost+found/" +) + +MOUNTED_POINTS=() + +usage() { + grep '^#' "$0" | sed -n '2,20p' | sed 's/^# \{0,1\}//' + exit 1 +} + +log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; } +die() { log "ERROR: $*" >&2; exit 1; } + +cleanup() { + for mp in "${MOUNTED_POINTS[@]:-}"; do + [ -n "$mp" ] || continue + umount "$mp" 2>/dev/null || umount -l "$mp" 2>/dev/null || true + rmdir "$mp" 2>/dev/null || true + done +} +trap cleanup EXIT + +is_system_share() { + local share="$1" s + for s in "${SYSTEM_SHARE_EXCLUDES[@]}"; do + [ "$share" = "$s" ] && return 0 + done + return 1 +} + +rsync_exclude_args() { + local a=() + local e + for e in "${RSYNC_EXCLUDES[@]}"; do a+=(--exclude="$e"); done + printf '%s\n' "${a[@]}" +} + +run_rsync() { + # run_rsync + local src="$1" dst="$2" logfile="$3" + mkdir -p "$dst" + local -a excludes=() + mapfile -t excludes < <(rsync_exclude_args) + local -a flags=(-aAXH --partial --info=progress2 "${excludes[@]}") + [ "$DRY_RUN" -eq 1 ] && flags+=(--dry-run) + log "rsync: $src -> $dst" + rsync "${flags[@]}" "$src" "$dst" 2>&1 | tee -a "$logfile" +} + +# ---------- SSH method ---------- + +discover_shares_ssh() { + local host="$1" user="$2" identity="$3" port="22" + local -a ssh_opts=(-o StrictHostKeyChecking=accept-new -o BatchMode=yes -p "$port") + [ "$identity" != "-" ] && ssh_opts+=(-i "$identity") + ssh "${ssh_opts[@]}" "${user}@${host}" \ + 'for d in /share/CACHEDEV*_DATA/*/; do b=$(basename "$d"); echo "$b"; done' 2>/dev/null \ + | sort -u +} + +sync_ssh() { + local name="$1" host="$2" user="$3" identity="$4" shares_csv="$5" logdir="$6" + local -a ssh_opts=(-o StrictHostKeyChecking=accept-new -p 22) + [ "$identity" != "-" ] && ssh_opts+=(-i "$identity") + local ssh_cmd="ssh ${ssh_opts[*]}" + + local -a shares=() + if [ "$shares_csv" = "ALL" ]; then + while IFS= read -r s; do + is_system_share "$s" || shares+=("$s") + done < <(discover_shares_ssh "$host" "$user" "$identity") + else + IFS=',' read -r -a shares <<< "$shares_csv" + fi + + [ "${#shares[@]}" -eq 0 ] && { log "[$name] no shares found/selected"; return; } + + for share in "${shares[@]}"; do + [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } + local dest="${DEST_ROOT}/${name}/${share}" + local logfile="${logdir}/${name}_${share}.log" + mkdir -p "$dest" + run_rsync "${user}@${host}:/share/CACHEDEV*_DATA/${share}/" "${dest}/" "$logfile" \ + || log "[$name/$share] rsync reported errors (see $logfile)" + done +} + +# ---------- SMB method ---------- + +discover_shares_smb() { + local host="$1" credsopt="$2" + smbclient -L "//${host}" "$credsopt" -g 2>/dev/null \ + | awk -F'|' '$1=="Disk"{print $2}' \ + | grep -Ev '^(IPC\$|ADMIN\$|print\$)$' +} + +smb_creds_opt() { + # Prints the smbclient/mount.cifs auth option(s), prompting if needed. + local creds_path="$1" + if [ "$creds_path" != "-" ]; then + echo "credentials=${creds_path}" + else + echo "__PROMPT__" + fi +} + +sync_smb() { + local name="$1" host="$2" user="$3" creds="$4" shares_csv="$5" logdir="$6" + local creds_file="$creds" + local tmp_creds="" + + if [ "$creds" = "-" ]; then + read -r -p "SMB password for ${user}@${host}: " -s smb_pass; echo + tmp_creds="$(mktemp)" + printf 'username=%s\npassword=%s\n' "$user" "$smb_pass" > "$tmp_creds" + chmod 600 "$tmp_creds" + creds_file="$tmp_creds" + fi + + local -a shares=() + if [ "$shares_csv" = "ALL" ]; then + while IFS= read -r s; do + is_system_share "$s" || shares+=("$s") + done < <(discover_shares_smb "$host" "-A${creds_file}") + else + IFS=',' read -r -a shares <<< "$shares_csv" + fi + + for share in "${shares[@]}"; do + [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } + local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")" + MOUNTED_POINTS+=("$mountpoint") + if ! mount -t cifs "//${host}/${share}" "$mountpoint" \ + -o "credentials=${creds_file},vers=3.0,ro,iocharset=utf8"; then + log "[$name/$share] mount failed, skipping" + rmdir "$mountpoint" 2>/dev/null || true + continue + fi + local dest="${DEST_ROOT}/${name}/${share}" + local logfile="${logdir}/${name}_${share}.log" + run_rsync "${mountpoint}/" "${dest}/" "$logfile" \ + || log "[$name/$share] rsync reported errors (see $logfile)" + umount "$mountpoint" || umount -l "$mountpoint" || true + rmdir "$mountpoint" 2>/dev/null || true + MOUNTED_POINTS=("${MOUNTED_POINTS[@]/$mountpoint}") + done + + [ -n "$tmp_creds" ] && shred -u "$tmp_creds" 2>/dev/null || rm -f "$tmp_creds" 2>/dev/null || true +} + +# ---------- NFS method ---------- + +discover_shares_nfs() { + local host="$1" + showmount -e "$host" 2>/dev/null | tail -n +2 | awk '{print $1}' | xargs -n1 basename +} + +sync_nfs() { + local name="$1" host="$2" shares_csv="$3" logdir="$4" + local -a shares=() + if [ "$shares_csv" = "ALL" ]; then + while IFS= read -r s; do + is_system_share "$s" || shares+=("$s") + done < <(discover_shares_nfs "$host") + else + IFS=',' read -r -a shares <<< "$shares_csv" + fi + + for share in "${shares[@]}"; do + [ "$LIST_ONLY" -eq 1 ] && { log "[$name] share: $share"; continue; } + local mountpoint; mountpoint="$(mktemp -d "/mnt/.qnap_${name}_XXXXXX")" + MOUNTED_POINTS+=("$mountpoint") + if ! mount -t nfs -o ro "${host}:/share/${share}" "$mountpoint" 2>/dev/null \ + && ! mount -t nfs -o ro "${host}:${share}" "$mountpoint"; then + log "[$name/$share] mount failed, skipping" + rmdir "$mountpoint" 2>/dev/null || true + continue + fi + local dest="${DEST_ROOT}/${name}/${share}" + local logfile="${logdir}/${name}_${share}.log" + run_rsync "${mountpoint}/" "${dest}/" "$logfile" \ + || log "[$name/$share] rsync reported errors (see $logfile)" + umount "$mountpoint" || umount -l "$mountpoint" || true + rmdir "$mountpoint" 2>/dev/null || true + MOUNTED_POINTS=("${MOUNTED_POINTS[@]/$mountpoint}") + done +} + +# ---------- arg parsing ---------- + +while [ $# -gt 0 ]; do + case "$1" in + --config) CONFIG_FILE="$2"; shift 2 ;; + --dest) DEST_ROOT="$2"; shift 2 ;; + --only) ONLY_NAME="$2"; shift 2 ;; + --jobs) JOBS="$2"; shift 2 ;; + --dry-run) DRY_RUN=1; shift ;; + --list-only) LIST_ONLY=1; shift ;; + -h|--help) usage ;; + *) die "unknown argument: $1" ;; + esac +done + +[ "$(id -u)" -eq 0 ] || die "must run as root (needed for mount/umount)" +[ -f "$CONFIG_FILE" ] || die "config file not found: $CONFIG_FILE (copy nas_list.conf.example)" +command -v rsync >/dev/null || die "rsync not found" + +LOG_DIR="${DEST_ROOT}/_logs" +mkdir -p "$LOG_DIR" "$DEST_ROOT" + +log "Starting migration. dest=$DEST_ROOT dry_run=$DRY_RUN list_only=$LIST_ONLY" + +running=0 +while IFS= read -r line || [ -n "$line" ]; do + line="${line%%#*}" + [ -z "${line// /}" ] && continue + read -r name method host user auth shares <<< "$line" + [ -n "$name" ] || continue + [ -n "$ONLY_NAME" ] && [ "$ONLY_NAME" != "$name" ] && continue + + ( + case "$method" in + ssh) sync_ssh "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;; + smb) sync_smb "$name" "$host" "$user" "$auth" "$shares" "$LOG_DIR" ;; + nfs) sync_nfs "$name" "$host" "$shares" "$LOG_DIR" ;; + *) log "[$name] unknown method '$method', skipping" ;; + esac + ) & + + running=$((running + 1)) + if [ "$running" -ge "$JOBS" ]; then + wait -n + running=$((running - 1)) + fi +done < "$CONFIG_FILE" + +wait +log "Migration complete. Logs in $LOG_DIR" diff --git a/nas_list.conf.example b/nas_list.conf.example new file mode 100644 index 0000000..26c7b7a --- /dev/null +++ b/nas_list.conf.example @@ -0,0 +1,19 @@ +# QNAP -> TrueNAS migration inventory +# One line per QNAP NAS. Fields are whitespace separated (use quotes for values with spaces). +# Copy this file to nas_list.conf and edit it for your environment. +# +# name method host user auth shares +# ------ ------- -------------- ------- --------------------------- ----------------- +# name : short identifier, used for dest folder + log file names (no spaces) +# method : ssh | smb | nfs +# host : hostname or IP of the QNAP NAS +# user : username to connect with (SMB/SSH). Use "-" for NFS. +# auth : ssh -> path to SSH private key, or "-" to use ssh-agent/default key +# smb -> path to a credentials file (username=.. / password=..), or "-" to be prompted +# nfs -> "-" (not used) +# shares : "ALL" to auto-discover every share, or a comma separated list of share names +# +qnap1 ssh 192.168.1.10 admin /root/.ssh/id_qnap1 ALL +qnap2 smb 192.168.1.11 admin /root/.smbcreds_qnap2 ALL +qnap3 nfs 192.168.1.12 - - ALL +# qnap4 ssh 192.168.1.13 admin - Public,Multimedia,Backups