alpinux-static/scripts/pull-assets.sh
Alpinux 7c70e904f3 fix: supprime --delete, exclut wiki/ et fichiers ISPConfig
Push : additive uniquement (pas de suppression sur le serveur)
Pull : idem, wiki/ exclu des deux côtés
Excludes communs : .git/, scripts/, README.md, .env*, .gitignore, wiki/
Correction bug bash set -e avec ((VAR++)) → VAR=$(( VAR + 1 ))

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 18:22:45 +02:00

106 lines
4 KiB
Bash
Executable file

#!/usr/bin/env bash
# pull-assets.sh — récupère les assets depuis static.alpinux.org vers le répertoire local
#
# Usage :
# ./pull-assets.sh # aperçu des changements + confirmation
# ./pull-assets.sh -y # récupère sans confirmation
# ./pull-assets.sh -n # dry-run seulement (aucune modification)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/../.env"
# ── Couleurs ────────────────────────────────────────────────────────
RED='\033[0;31m'; YELLOW='\033[1;33m'; GREEN='\033[0;32m'
CYAN='\033[0;36m'; BOLD='\033[1m'; RESET='\033[0m'
# ── Config ──────────────────────────────────────────────────────────
if [ ! -f "$ENV_FILE" ]; then
echo -e "${RED}Erreur : fichier .env introuvable.${RESET}"
echo "Copier scripts/../.env.example en .env et remplir les valeurs."
exit 1
fi
# shellcheck source=/dev/null
source "$ENV_FILE"
LOCAL_DIR="${LOCAL_ASSETS_DIR:-/tmp/alpinux-static-assets}"
REMOTE_USER="${STATIC_USER:?Variable STATIC_USER manquante dans .env}"
REMOTE_HOST="${STATIC_HOST:?Variable STATIC_HOST manquante dans .env}"
REMOTE_PATH="${STATIC_PATH:?Variable STATIC_PATH manquante dans .env}"
REMOTE="${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/"
# ── Arguments ───────────────────────────────────────────────────────
DRY_RUN=false
AUTO_YES=false
for arg in "$@"; do
case "$arg" in
-n|--dry-run) DRY_RUN=true ;;
-y|--yes) AUTO_YES=true ;;
-h|--help)
sed -n '2,6p' "$0" | sed 's/^# //'
exit 0 ;;
esac
done
mkdir -p "$LOCAL_DIR"
# ── Analyse des changements ─────────────────────────────────────────
echo -e "${BOLD}Analyse des changements…${RESET}"
echo -e " Source : ${CYAN}$REMOTE${RESET}"
echo -e " Cible : ${CYAN}$LOCAL_DIR/${RESET}"
echo ""
EXCLUDES=(--exclude='.git/' --exclude='scripts/' --exclude='README.md'
--exclude='.env' --exclude='.env.example' --exclude='.gitignore'
--exclude='wiki/')
DIFF=$(rsync -rlcz --dry-run --itemize-changes \
--rsync-path="sudo rsync" \
"${EXCLUDES[@]}" \
"$REMOTE" "$LOCAL_DIR/" 2>&1)
NEW=0; CHANGED=0
while IFS= read -r line; do
item="${line:0:11}"
file="${line:12}"
[ -z "$file" ] && continue
if [[ "$item" =~ ^\>f\+{6,} || "$item" =~ ^\.f\+{6,} ]]; then
echo -e " ${GREEN}nouveau ${RESET} $file"
NEW=$(( NEW + 1 ))
elif [[ "$item" =~ ^\>f || "$item" =~ ^\.f ]]; then
echo -e " ${YELLOW}modifié ${RESET} $file"
CHANGED=$(( CHANGED + 1 ))
fi
done <<< "$DIFF"
TOTAL=$((NEW + CHANGED))
if [ "$TOTAL" -eq 0 ]; then
echo -e "${GREEN}Tout est à jour — aucun fichier à récupérer.${RESET}"
exit 0
fi
echo ""
echo -e " ${GREEN}+$NEW nouveau(x)${RESET} ${YELLOW}~$CHANGED modifié(s)${RESET}"
echo ""
if $DRY_RUN; then echo -e "${CYAN}Mode dry-run — aucune modification effectuée.${RESET}"; exit 0; fi
# ── Confirmation ────────────────────────────────────────────────────
if ! $AUTO_YES; then
read -rp "Récupérer ces fichiers depuis $REMOTE_HOST ? [o/N] " confirm
[[ "$confirm" =~ ^[oOyY]$ ]] || { echo "Annulé."; exit 0; }
fi
# ── Synchronisation ─────────────────────────────────────────────────
echo ""
echo -e "${BOLD}Récupération en cours…${RESET}"
rsync -rlcz --human-readable --progress \
--rsync-path="sudo rsync" \
"${EXCLUDES[@]}" \
"$REMOTE" "$LOCAL_DIR/"
echo ""
echo -e "${GREEN}✓ Fichiers récupérés dans : $LOCAL_DIR${RESET}"