Fix reorg-backup.sh camera scoping — prevent 2026/ dir misidentified as cam

Restructure find loop to iterate known CAMERAS+sunrise in the main shell
so cam_name is visible in the while body. Previously the for loop ran in
a process substitution subshell, making cam_name unavailable and causing
top-level year dirs (e.g. 2026/) to be treated as camera names.

https://claude.ai/code/session_01C4jbd3waXG3eKZYbGUjLUQ
This commit is contained in:
Claude
2026-04-20 18:49:34 +00:00
parent da35dc862c
commit 507ecf32c8
+57 -57
View File
@@ -49,67 +49,67 @@ moved=0
skipped=0 skipped=0
errors=0 errors=0
# Find all mp4 files in Season/MvtN directories (not already in astro-* dirs) # Iterate over known cameras only — avoids stray season-shaped paths elsewhere
while IFS= read -r -d '' src_file; do # in the backup (e.g. a top-level 2026/ dir that would be mistaken for a cam).
filename="$(basename "$src_file")" for cam_name in "${CAMERAS[@]}" sunrise; do
src_root="$BACKUP_ROOT/$cam_name"
[ -d "$src_root" ] || continue
echo "--- $cam_name ---"
# Extract date: handles YYYY-MM-DD_ (underscore) prefix while IFS= read -r -d '' src_file; do
date_str="${filename%%_*}" filename="$(basename "$src_file")"
if ! [[ "$date_str" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
echo "SKIP (unrecognised filename): $src_file"
(( skipped++ )) || true
continue
fi
# Get ASTRO_YEAR for this date # Extract date from YYYY-MM-DD_ prefix
_info="$(python3 "$SCRIPT_DIR/season_info.py" "$date_str" 2>/dev/null)" || { date_str="${filename%%_*}"
echo "SKIP (season_info.py failed for $date_str): $filename" if ! [[ "$date_str" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
(( skipped++ )) || true echo "SKIP (unrecognised filename): $filename"
continue (( skipped++ )) || true
} continue
eval "$_info"
# Reconstruct target path:
# <backup_root>/astro-YEAR/<cam>/<Season>/Mvt<N>/<filename>
# Source path is:
# <backup_root>/<cam>/<Season>/Mvt<N>/<filename>
rel_path="${src_file#$BACKUP_ROOT/}" # e.g. north/Winter/Mvt1/2025-01-06_...
cam_and_below="${rel_path#*/}" # e.g. Winter/Mvt1/2025-01-06_... (strip cam)
cam_name="${rel_path%%/*}" # e.g. north
dest_dir="$BACKUP_ROOT/astro-${ASTRO_YEAR}/$cam_name/$(dirname "$cam_and_below")"
dest_file="$dest_dir/$filename"
if [ "$src_file" = "$dest_file" ]; then
(( skipped++ )) || true
continue
fi
if [ -f "$dest_file" ]; then
echo "EXISTS — skip: astro-${ASTRO_YEAR}/$cam_name/$cam_and_below"
(( skipped++ )) || true
continue
fi
echo "MOVE astro-${ASTRO_YEAR}/$cam_name/$cam_and_below"
if ! $DRY_RUN; then
mkdir -p "$dest_dir"
if mv "$src_file" "$dest_file"; then
(( moved++ )) || true
else
echo " ERROR: mv failed"
(( errors++ )) || true
fi fi
else
(( moved++ )) || true
fi
done < <(find "$BACKUP_ROOT" \ # Get ASTRO_YEAR for this date
-not -path "*/astro-*" \ _info="$(python3 "$SCRIPT_DIR/season_info.py" "$date_str" 2>/dev/null)" || {
-regextype posix-extended \ echo "SKIP (season_info.py failed for $date_str): $filename"
-regex ".*/[A-Z][a-z]+/Mvt[0-9]+/[0-9]{4}-[0-9]{2}-[0-9]{2}_.*\.mp4" \ (( skipped++ )) || true
-print0 | sort -z) continue
}
eval "$_info"
rel_path="${src_file#$src_root/}" # e.g. Winter/Mvt1/2025-01-06_...
dest_dir="$BACKUP_ROOT/astro-${ASTRO_YEAR}/$cam_name/$(dirname "$rel_path")"
dest_file="$dest_dir/$filename"
if [ "$src_file" = "$dest_file" ]; then
(( skipped++ )) || true
continue
fi
if [ -f "$dest_file" ]; then
echo "EXISTS — skip: astro-${ASTRO_YEAR}/$cam_name/$rel_path"
(( skipped++ )) || true
continue
fi
echo "MOVE astro-${ASTRO_YEAR}/$cam_name/$rel_path"
if ! $DRY_RUN; then
mkdir -p "$dest_dir"
if mv "$src_file" "$dest_file"; then
(( moved++ )) || true
else
echo " ERROR: mv failed"
(( errors++ )) || true
fi
else
(( moved++ )) || true
fi
done < <(find "$src_root" \
-not -path "*/astro-*" \
-regextype posix-extended \
-regex ".*/[A-Z][a-z]+/Mvt[0-9]+/[0-9]{4}-[0-9]{2}-[0-9]{2}_.*\.mp4" \
-print0 | sort -z)
done
echo "" echo ""
echo "────────────────────────────────────────" echo "────────────────────────────────────────"