Files
reHDD-Bootable/root/root/export_reHDD_logs.sh
T
localhorst 9d929bd0ad Add log file export script (#14)
use the script /root/export_reHDD_logs.sh to export the reHDD.log to an USB drive.

Reviewed-on: #14
Co-authored-by: localhorst <localhorst@mosad.xyz>
Co-committed-by: localhorst <localhorst@mosad.xyz>
2026-05-03 10:07:47 +02:00

198 lines
7.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# export_reHDD_logs.sh
# Exports reHDD logs to a selected drive with optional ext4 formatting.
set -euo pipefail
# ─── Colors ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'
YEL='\033[1;33m'
GRN='\033[0;32m'
CYN='\033[0;36m'
BLD='\033[1m'
RST='\033[0m'
# ─── Privilege check ──────────────────────────────────────────────────────────
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}Error: This script must be run as root.${RST}" >&2
exit 1
fi
# ─── Dependency check ─────────────────────────────────────────────────────────
for cmd in lsblk mkfs.ext4 mount umount sync; do
if ! command -v "$cmd" &>/dev/null; then
echo -e "${RED}Error: Required command '${cmd}' not found.${RST}" >&2
exit 1
fi
done
SOURCE_LOG="/root/reHDD/reHDD.log"
if [[ ! -f "$SOURCE_LOG" ]]; then
echo -e "${RED}Error: Log file not found: ${SOURCE_LOG}${RST}" >&2
exit 1
fi
# ─── 1. Discover drives ───────────────────────────────────────────────────────
echo -e "\n${BLD}${CYN}=== reHDD Log Exporter ===${RST}\n"
echo -e "${BLD}Scanning for block devices...${RST}\n"
# Collect drives (disk type, not partitions/loops/rom)
# Temporarily disable errexit so an empty result doesn't kill the script
DRIVES=()
while IFS= read -r line; do
[[ -n "$line" ]] && DRIVES+=("$line")
done < <(lsblk -dpno NAME,TYPE 2>/dev/null | awk '$2=="disk"{print $1}' || true)
if [[ ${#DRIVES[@]} -eq 0 ]]; then
echo -e "${RED}No drives found.${RST}" >&2
exit 1
fi
# Build display table
declare -a DRIVE_PATHS DRIVE_SIZES DRIVE_FS
idx=0
for drv in "${DRIVES[@]}"; do
# NVMe uses /dev/nvme0n1p1, SATA/USB use /dev/sda1
if [[ "$drv" =~ nvme ]]; then
part="${drv}p1"
else
part="${drv}1"
fi
# Human-readable size of the whole disk
size=$(lsblk -dno SIZE "$drv" 2>/dev/null || echo "N/A")
# Filesystem on first partition (if it exists)
if [[ -b "$part" ]]; then
fs=$(lsblk -no FSTYPE "$part" 2>/dev/null | head -1 || true)
fs=${fs:-"<unknown>"}
else
fs="<no partition>"
fi
DRIVE_PATHS[$idx]="$drv"
DRIVE_SIZES[$idx]="$size"
DRIVE_FS[$idx]="$fs"
(( idx++ )) || true
done
# Print table
printf " ${BLD}%-4s %-12s %-10s %-15s${RST}\n" "No." "Path" "Size" "FS (part1)"
printf " %-4s %-12s %-10s %-15s\n" "----" "------------" "----------" "---------------"
for i in "${!DRIVE_PATHS[@]}"; do
printf " ${GRN}%-4s${RST} %-12s %-10s %-15s\n" \
"$((i+1))" "${DRIVE_PATHS[$i]}" "${DRIVE_SIZES[$i]}" "${DRIVE_FS[$i]}"
done
echo ""
# ─── 2. Drive selection ───────────────────────────────────────────────────────
while true; do
read -rp "$(echo -e "${BLD}Select drive [1-${#DRIVE_PATHS[@]}]:${RST} ")" sel
if [[ "$sel" =~ ^[0-9]+$ ]] && (( sel >= 1 && sel <= ${#DRIVE_PATHS[@]} )); then
SELECTED_IDX=$((sel-1))
break
fi
echo -e "${YEL}Invalid selection, try again.${RST}"
done
DRIVE="${DRIVE_PATHS[$SELECTED_IDX]}"
# NVMe partitions use 'p1' suffix, SATA/USB use '1'
if [[ "$DRIVE" =~ nvme ]]; then
PARTITION="${DRIVE}p1"
else
PARTITION="${DRIVE}1"
fi
CURRENT_FS="${DRIVE_FS[$SELECTED_IDX]}"
echo -e "\nSelected: ${BLD}${DRIVE}${RST} (${DRIVE_SIZES[$SELECTED_IDX]}, fs: ${CURRENT_FS})\n"
# ─── 3. Create ext4 if needed ─────────────────────────────────────────────────
if [[ "$CURRENT_FS" != "ext4" ]]; then
echo -e "${YEL}Warning:${RST} Partition ${PARTITION} does not have an ext4 filesystem."
echo -e " Current: ${BLD}${CURRENT_FS}${RST}"
echo -e "${RED} All existing data on ${PARTITION} will be DESTROYED.${RST}\n"
read -rp "$(echo -e "${BLD}Create ext4 filesystem on ${PARTITION}? [yes/NO]: ${RST}")" confirm
if [[ "$confirm" != "yes" ]]; then
echo -e "${YEL}Aborted by user.${RST}"
exit 0
fi
echo -e "\n${CYN}Creating ext4 filesystem on ${PARTITION}...${RST}"
# If there is no partition table / first partition, create one first
if [[ ! -b "$PARTITION" ]]; then
echo -e "${CYN}No first partition found creating a single primary partition...${RST}"
echo -e "o\nn\np\n1\n\n\nw" | fdisk "$DRIVE" >/dev/null 2>&1 || true
partprobe "$DRIVE" 2>/dev/null || true
sleep 1
fi
mkfs.ext4 -F "$PARTITION"
echo -e "${GRN}ext4 filesystem created successfully.${RST}\n"
fi
# ─── 4. Mount partition ───────────────────────────────────────────────────────
RAND_HEX=$(tr -dc 'a-f0-9' </dev/urandom | head -c 16 || true)
MNT_DIR="/mnt/${RAND_HEX}_reHDD_Log_Exporter"
echo -e "${CYN}Mounting ${PARTITION} at ${MNT_DIR} ...${RST}"
mkdir -p "$MNT_DIR"
# Ensure cleanup on unexpected exit
cleanup() {
local exit_code=$?
if mountpoint -q "$MNT_DIR" 2>/dev/null; then
umount "$MNT_DIR" 2>/dev/null || true
fi
if [[ -d "$MNT_DIR" ]]; then
rmdir "$MNT_DIR" 2>/dev/null || true
fi
exit $exit_code
}
trap cleanup ERR INT TERM
mount "$PARTITION" "$MNT_DIR"
echo -e "${GRN}Mounted successfully.${RST}\n"
# ─── 5. Prefix input ─────────────────────────────────────────────────────────
while true; do
read -rp "$(echo -e "${BLD}Enter a prefix (no spaces, e.g. shredderpc42): ${RST}")" PREFIX
if [[ -z "$PREFIX" ]]; then
echo -e "${YEL}Prefix must not be empty.${RST}"
elif [[ "$PREFIX" =~ [[:space:]] ]]; then
echo -e "${YEL}Prefix must not contain spaces.${RST}"
else
break
fi
done
# ─── 6. Copy log file ─────────────────────────────────────────────────────────
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
DEST_FILE="${MNT_DIR}/${PREFIX}_reHDD_${TIMESTAMP}.log"
echo -e "\n${CYN}Copying log to ${DEST_FILE} ...${RST}"
cp "$SOURCE_LOG" "$DEST_FILE"
echo -e "${GRN}Log copied successfully.${RST}\n"
# ─── 7. Sync ──────────────────────────────────────────────────────────────────
echo -e "${CYN}Syncing buffers to disk...${RST}"
sync
echo -e "${GRN}Sync complete.${RST}\n"
# ─── 8. Unmount & cleanup ─────────────────────────────────────────────────────
echo -e "${CYN}Unmounting ${MNT_DIR} ...${RST}"
umount "$MNT_DIR"
echo -e "${GRN}Unmounted.${RST}"
echo -e "${CYN}Removing mount directory...${RST}"
rmdir "$MNT_DIR"
echo -e "${GRN}Done.${RST}\n"
# Disable trap — clean exit
trap - ERR INT TERM
echo -e "${BLD}${GRN}✓ Log exported successfully to ${DRIVE} as:${RST}"
echo -e " ${BLD}${PREFIX}_reHDD_${TIMESTAMP}.log${RST}\n"