Show numbered backup list, let user pick — no more wrong-file guessing

This commit is contained in:
2026-04-26 20:44:02 +10:00
parent 09f43d3b0a
commit e17394bb15
+22 -37
View File
@@ -68,7 +68,7 @@ mkdir -p "${WORK_DIR}" "${RESTORE_STAGING}"
step "PHASE 1 — Pull NPM backup from HA via Samba"
echo -e "${BOLD}HA Samba credentials needed${RESET}"
echo -e " Share: \\\\${HA_HOST}\\backup"
echo -e " Share: //${HA_HOST}/backup"
echo ""
read -rp " Samba username [${HA_SAMBA_USER}]: " INPUT_USER
@@ -77,47 +77,32 @@ read -rp " Samba username [${HA_SAMBA_USER}]: " INPUT_USER
read -rsp " Samba password for '${HA_SAMBA_USER}': " HA_SAMBA_PASS
echo ""
info "Listing HA backup share to find the NPM backup..."
BACKUP_FILE=$(smbclient "//$(echo ${HA_HOST})/backup" \
-U "${HA_SAMBA_USER}%${HA_SAMBA_PASS}" \
-c "ls" 2>/dev/null \
| awk '{print $1}' \
| grep '\.tar$' \
| head -20 | tr '\n' '\n' || true)
SMB_CMD="smbclient //${HA_HOST}/backup -U ${HA_SAMBA_USER}%${HA_SAMBA_PASS}"
if [[ -z "${BACKUP_FILE}" ]]; then
die "Could not list the HA backup share at //${HA_HOST}/backup\n\n" \
" Check:\n" \
" 1. Samba NAS add-on is running in HA\n" \
" 2. Username/password are correct (set in Samba add-on config)\n" \
" 3. The 'backup' folder is enabled in the Samba add-on options\n\n" \
" Alternatively, go to HA → Settings → System → Backups,\n" \
" find 'NPM Migration Backup', download it manually to this machine\n" \
" and re-run this script with SUPERVISOR_BACKUP set to that file path."
fi
info "Listing HA backup share..."
LISTING=$(${SMB_CMD} -c "ls" 2>/dev/null) || \
die "Could not list //${HA_HOST}/backup\n Check: Samba NAS add-on running, credentials correct, 'backup' folder enabled."
# Build indexed list of .tar files only
mapfile -t TAR_FILES < <(echo "${LISTING}" | awk '{print $1}' | grep '\.tar$')
[[ ${#TAR_FILES[@]} -gt 0 ]] || die "No .tar backup files found on the share."
echo ""
info "Backup files found on share:"
echo "${BACKUP_FILE}"
echo -e "${BOLD}Backups found on share:${RESET}"
for i in "${!TAR_FILES[@]}"; do
printf " %2d) %s\n" "$((i+1))" "${TAR_FILES[$i]}"
done
echo ""
warn "Look for the file named like 'NPM_Migration_Backup_*.tar' or the most recent hex-named .tar"
warn "If unsure, check HA → Settings → System → Backups → 'NPM Migration Backup' to confirm the date"
echo ""
read -rp " Enter number of the NPM Migration backup to download: " PICK
[[ "${PICK}" =~ ^[0-9]+$ ]] && [[ "${PICK}" -ge 1 ]] && [[ "${PICK}" -le "${#TAR_FILES[@]}" ]] \
|| die "Invalid selection."
# Find the right backup — look for the most recent one (the one we just created)
# HA backup filenames are the backup slug (8-char hex), e.g. a1b2c3d4.tar
info "Fetching the most recently modified .tar from the share..."
LATEST_TAR=$(smbclient "//$(echo ${HA_HOST})/backup" \
-U "${HA_SAMBA_USER}%${HA_SAMBA_PASS}" \
-c "ls" 2>/dev/null \
| grep '\.tar' \
| sort -k3,4 \
| tail -1 \
| awk '{print $1}')
[[ -n "${LATEST_TAR}" ]] || die "Could not identify the latest backup tar file."
info "Downloading: ${LATEST_TAR}${SUPERVISOR_BACKUP}"
smbclient "//$(echo ${HA_HOST})/backup" \
-U "${HA_SAMBA_USER}%${HA_SAMBA_PASS}" \
-c "get ${LATEST_TAR} ${SUPERVISOR_BACKUP}" 2>/dev/null
CHOSEN_TAR="${TAR_FILES[$((PICK-1))]}"
info "Downloading: ${CHOSEN_TAR}${SUPERVISOR_BACKUP}"
${SMB_CMD} -c "get ${CHOSEN_TAR} ${SUPERVISOR_BACKUP}" 2>/dev/null
[[ -f "${SUPERVISOR_BACKUP}" ]] || die "Download failed — file not found at ${SUPERVISOR_BACKUP}"