Il y a 389 articles et 19 utilisateurs inscrits (ce bandeau est défini dans MediaWiki:Sitenotice)
Analyse rsync transfert
Optimisation du transfert Rsync (Chat complet)
[modifier | modifier le wikicode]=== Contexte initial === L’utilisateur dispose d’un script de transfert Rsync fonctionnel (transfert_rsync.sh) et de son fichier de configuration (config.sh).
Objectif : corriger certains points manquants, notamment la génération des fichiers de hash, la suppression des erreurs rsync error: code 20, et l’ajout de rapports complets.
=== Étape 1 — Analyse initiale du fonctionnement === L’assistant a procédé à une analyse complète des scripts, sans simplification ni modification des chemins.
Voici les principaux constats :
- Le transfert est bien supervisé par une fenêtre horaire dynamique.
- Les logs sont cohérents et correctement horodatés.
- La fonction
check_time_window()interrompt Rsync par un signalSIGINTviapkill, provoquant unrsync error: code 20. - Les fichiers
/tmp/rsync_hashes_latest.txtetrsync_raw...ne sont jamais remplis. - Aucun rapport JSON ou texte n’est généré.
- Le message
Killedprovient de la terminaison violente du processus Rsync.
=== Étape 2 — Recommandation sur le kill === L’assistant explique la différence entre kill, pkill, et pkill -P $$ :
killagit sur un PID spécifique.pkillagit par nom et peut tuer tous les Rsync du système.pkill -P $$agit sur les processus fils du script en cours.
Pour éviter le rsync error code 20 et le message Killed, il est recommandé d’utiliser un arrêt propre :
if kill -0 “$RSYNC_PID” 2>/dev/null; then echo “[INFO] Fin de la fenêtre horaire — arrêt propre du rsync PID <math display="inline">RSYNC_PID" >> "</math>LOG_FILE” kill -2 “<math display="inline">RSYNC_PID" sleep 5 if kill -0 "</math>RSYNC_PID” 2>/dev/null; then echo “[WARN] Rsync toujours actif, envoi SIGKILL.” >> “<math display="inline">LOG_FILE" kill -9 "</math>RSYNC_PID” fi fi
=== Étape 3 — Cahier des charges de la version optimisée === La version suivante devait intégrer : * Génération correcte du fichier de hash. * Suppression des erreurs de signal Rsync. * Création de rapports JSON et TXT dans /reports. * Gestion du code retour Rsync global. * Maintien de la compatibilité totale.
=== Étape 4 — Script final optimisé ===
#!/bin/bash # ================================================== # TRANSFERT RSYNC SERVEUR A → SERVEUR B (PUSH) # Version optimisée : arrêt propre + rapports enrichis # ==================================================
source /opt/rsync_sync/config.sh
DATE_TAG=<math display="inline">(date +%F_%H-%M) LOG_FILE="</math>LOG_DIR/rsync_transfer_<math display="inline">DATE_TAG.log" JSON_REPORT="</math>REPORT_DIR/rsync_report_<math display="inline">DATE_TAG.json" TABLE_REPORT="</math>REPORT_DIR/rsync_summary_$DATE_TAG.txt”
session_id=“rsync_<math display="inline">{DATE_TAG}_</math>$”
echo “==== DÉBUT TRANSFERT <math display="inline">(date '+%d/%m/%Y %H:%M:%S') ====" >> "</math>LOG_FILE”
<span id="section"></span>
= ————————————————– =
<span id="fonction-contrôle-de-la-fenêtre-horaire"></span>
= Fonction : contrôle de la fenêtre horaire =
<span id="section-1"></span>
= ————————————————– =
check_time_window() { current_day=<math display="inline">(date +%a) current_time=</math>(date +%H:%M)
if [ “<math display="inline">current_day" == "</math>RSYNC_WINDOW_END_DAY” ] && [[ “<math display="inline">current_time" > "</math>RSYNC_WINDOW_END_TIME” ]]; then echo “[INFO] Fin de la fenêtre horaire de transfert : arrêt automatique à <math display="inline">(date)" >> "</math>LOG_FILE” if kill -0 “$RSYNC_PID” 2>/dev/null; then echo “[INFO] Fin de la fenêtre horaire — arrêt propre du rsync PID <math display="inline">RSYNC_PID" >> "</math>LOG_FILE” kill -2 “<math display="inline">RSYNC_PID" sleep 5 if kill -0 "</math>RSYNC_PID” 2>/dev/null; then echo “[WARN] Rsync toujours actif, envoi SIGKILL.” >> “<math display="inline">LOG_FILE" kill -9 "</math>RSYNC_PID” fi fi echo “==== FIN TRANSFERT <math display="inline">(date '+%d/%m/%Y %H:%M:%S') ====" >> "</math>LOG_FILE” exit 0 fi
if [ “<math display="inline">current_day" != "</math>RSYNC_WINDOW_START_DAY” ] && [ “<math display="inline">current_day" != "</math>RSYNC_WINDOW_END_DAY” ]; then if [[ ! “<math display="inline">current_day" =~ (Sat|Sun) ]]; then echo "[INFO] Fenêtre horaire non active (</math>current_day <math display="inline">current_time) — arrêt automatique." >> "</math>LOG_FILE” if kill -0 “$RSYNC_PID” 2>/dev/null; then echo “[INFO] Arrêt propre du rsync PID <math display="inline">RSYNC_PID" >> "</math>LOG_FILE” kill -2 “<math display="inline">RSYNC_PID" sleep 5 if kill -0 "</math>RSYNC_PID” 2>/dev/null; then echo “[WARN] Rsync toujours actif, envoi SIGKILL.” >> “<math display="inline">LOG_FILE" kill -9 "</math>RSYNC_PID” fi fi echo “==== FIN TRANSFERT <math display="inline">(date '+%d/%m/%Y %H:%M:%S') ====" >> "</math>LOG_FILE” exit 0 fi fi }]]; then num=“<math display="inline">{BASH_REMATCH[1]}"; unit="</math>{BASH_REMATCH[3]}” total_bytes_int=<math display="inline">(awk -v n="</math>num” -v u=“<math display="inline">unit" 'BEGIN{ b = n; if(u=="K") b*=1024; else if(u=="M") b*=1048576; else if(u=="G") b*=1073741824; printf("%.0f", b); }') else total_bytes_int=0 fi STATS["taille"]=</math>((STATS[“taille”] + total_bytes_int)) bytes_transferred=$((bytes_transferred + total_bytes_int))
avg_speed_raw=<math display="inline">(echo "</math>rsync_output” | grep “bytes/sec” | awk ’{print <math display="inline">(NF-1)}') avg_speed_unit=</math>(echo “<math display="inline">avg_speed_raw" | grep -o '[KMG]</math>‘) avg_speed_num=<math display="inline">(echo "</math>avg_speed_raw” | sed ’s/[KMG]<math display="inline">//') case "</math>avg_speed_unit” in K) avg_speed=<math display="inline">(awk -v n="</math>avg_speed_num” ’BEGIN{ printf(“%.2f”, n/1024) }’);; M) avg_speed=”<math display="inline">avg_speed_num";; G) avg_speed=</math>(awk -v n=“<math display="inline">avg_speed_num" 'BEGIN{ printf("%.2f", n*1024) }');; *) avg_speed=</math>(awk -v n=”<math display="inline">avg_speed_num" 'BEGIN{ printf("%.2f", n/1048576) }');; esac STATS["vitesse"]="</math>avg_speed” done
sort -u “<math display="inline">HASH_RAW" > "</math>HASH_FILE”
end_time=<math display="inline">(date +%s) end_iso=</math>(date -Iseconds) end_human=<math display="inline">(date '+%d/%m/%Y %H:%M:%S') duration=</math>((end_time - start_time)) duration_human=$(printf “%02d:%02d:%02d” $((duration/3600)) $((duration%3600/60)) <math display="inline">((duration%60))) total_size_GB=</math>(awk -v b=“${STATS[”taille”]}” ‘BEGIN{ printf(“%.2f”, b/1073741824) }’)
if [[ $global_rc -eq 0 ]]; then status=“success” else status=“partial_or_failed” fi
{ echo “RAPPORT RSYNC — Session <math display="inline">{session_id}" echo "=============================================" printf "Début : %s\n" "</math>start_iso” printf “Fin : %s” “<math display="inline">end_iso" printf "Durée : %ss (%s)\n" "</math>duration” “<math display="inline">duration_human" printf "Statut : %s (rc=%s)\n" "</math>status” “<math display="inline">global_rc" printf "Sources : %s\n" "</math>{DIRS_TO_COPY[*]}” printf “Destination: %s@%s:%s” “<math display="inline">RSYNC_USER" "</math>RSYNC_HOST” “<math display="inline">DST" printf "Fichiers transférés : %s\n" "</math>files_transferred” printf “Octets transférés : %s” “<math display="inline">bytes_transferred" printf "Taille totale (Go) : %s\n" "</math>total_size_GB” printf “Vitesse moyenne (Mo/s) : %s” “<math display="inline">{STATS["vitesse"]}" echo "Logs :" printf " - App : %s\n" "</math>LOG_FILE” printf ” - Hash : %s” “<math display="inline">HASH_FILE" } >"</math>TABLE_REPORT”
cat >“<math display="inline">JSON_REPORT" <<JSON { "session_id": "</math>{session_id}”, “started_at”: “<math display="inline">{start_iso}", "ended_at": "</math>{end_iso}”, “duration_s”: <math display="inline">{duration}, "status": "</math>{status}“,”rsync_exit_code”: ${global_rc}, “files_transferred”: ${files_transferred}, “bytes_transferred”: ${bytes_transferred}, “src_list”: <math display="inline">(printf '%s\n' "</math>{DIRS_TO_COPY[@]}” | jq -R . | jq -s . 2>/dev/null || printf ‘[]’), “dst”: “<math display="inline">{RSYNC_USER}@</math>{RSYNC_HOST}:<math display="inline">{DST}", "logs": {"app_log": "</math>{LOG_FILE}”, “hash_raw”: “<math display="inline">{HASH_RAW}", "hash_latest": "</math>{HASH_FILE}”} } JSON
=== Étape 5 — Conclusion === Cette version résout les problèmes précédents : * Plus d’erreur SIGINT Rsync. * Hashes et rapports disponibles dans /tmp et /reports. * Journalisation inchangée. * Base prête pour la future validation des hash distants.