Il y a 389 articles et 19 utilisateurs inscrits (ce bandeau est défini dans MediaWiki:Sitenotice)

Script rsync avec copie sur période définies et rapports JSON

De AJLSM
Aller à la navigation Aller à la recherche
  • Transfert rsync avec authentification SSH par clé
  • Fenêtres horaires :
  • Rsync : vendredi 18h → lundi 08h
  • Validation hash : lundi 08h01 → vendredi 17h59
  • Reprise automatique des fichiers en erreur
  • Rapports enrichis JSON + tableau lisible
  • Notifications email à plusieurs destinataires
  • Compatible Systemd Timer pour planification automatique

1️⃣ Fichier de configuration : config.sh

#!/bin/bash
# /opt/rsync_sync/config.sh

# Serveur source et destination
SRC="user@serveurA:/path/source/"
DST="/path/destination/"

# Logs et rapports
LOG_DIR="/opt/rsync_sync/logs"
mkdir -p "$LOG_DIR"
ERROR_FILE="$LOG_DIR/rsync_errors.txt"
EMAIL_TO="admin1@domain.com,admin2@domain.com"
MAIL_SUBJECT="Rapport Rsync $(date +%F)"

# Rsync et SSH
RSYNC_OPTS="-avz --partial --progress"
BWLIMIT=50000                     # KB/s pour le transfert
RSYNC_SSH_KEY="$HOME/.ssh/rsync_key"  # Clé SSH sans mot de passe
RSYNC_SSH_OPTS="-i $RSYNC_SSH_KEY"

# Validation hash
PARALLEL_JOBS=8

# Fenêtres horaires (timestamps)
RSYNC_END=$(date -d 'next monday 08:00' +%s)
VALIDATION_END=$(date -d 'friday 17:59' +%s)

2️⃣ Script transfert rsync : transfert_rsync.sh

#!/bin/bash
source /opt/rsync_sync/config.sh

LOG_FILE="$LOG_DIR/rsync_transfer_$(date +%F).log"
HASH_FILE="$LOG_DIR/rsync_hashes_$(date +%F).txt"

echo "==== DÉBUT TRANSFERT $(date) ====" >> "$LOG_FILE"

# Fonction rsync avec contrôle de la fenêtre
run_rsync() {
    while [ $(date +%s) -lt $RSYNC_END ]; do
        rsync $RSYNC_OPTS -e "ssh $RSYNC_SSH_OPTS" "$SRC" "$DST"
        break
    done
}

# 1. Reprise des erreurs
if [ -f "$ERROR_FILE" ] && [ -s "$ERROR_FILE" ]; then
    rsync $RSYNC_OPTS -e "ssh $RSYNC_SSH_OPTS" --files-from="$ERROR_FILE" "$SRC" "$DST" >> "$LOG_FILE" 2>&1
    > "$ERROR_FILE"
fi

# 2. Transfert principal
run_rsync >> "$LOG_FILE" 2>&1

# 3. Liste des fichiers copiés pour validation hash
grep "^>" "$LOG_FILE" | awk '{print $2}' > "$HASH_FILE"

echo "==== FIN TRANSFERT $(date) ====" >> "$LOG_FILE"

3️⃣ Script validation hash : validation_hash.sh

#!/bin/bash
source /opt/rsync_sync/config.sh

REPORT_JSON="$LOG_DIR/rsync_report_$(date +%F).json"
REPORT_TABLE="$LOG_DIR/rsync_report_$(date +%F).txt"
HASH_FILES="$LOG_DIR/rsync_hashes_*.txt"

# Initialisation compteurs
TOTAL_COPIED=$(grep "^>" "$LOG_DIR/rsync_transfer_*.log" | wc -l)
> "$ERROR_FILE"

echo "==== DÉBUT VALIDATION $(date) ====" > "$REPORT_TABLE"
echo "RAPPORT DE VALIDATION RSYNC" >> "$REPORT_TABLE"
echo "==============================" >> "$REPORT_TABLE"

echo "{" > "$REPORT_JSON"
echo '  "date": "'$(date +%F)'",' >> "$REPORT_JSON"
echo '  "total_copied": '"$TOTAL_COPIED"',' >> "$REPORT_JSON"
echo '  "errors": [' >> "$REPORT_JSON"

validate_file() {
    local file="$1"
    SRC_HASH=$(ssh -i "$RSYNC_SSH_KEY" user@serveurA "sha256sum '$file'" | awk '{print $1}')
    DST_HASH=$(sha256sum "$DST/$file" 2>/dev/null | awk '{print $1}')
    if [ "$SRC_HASH" != "$DST_HASH" ]; then
        echo "$file" >> "$ERROR_FILE"
        echo "ERREUR: $file" >> "$REPORT_TABLE"
        echo '    {"file":"'"$file"'"}' >> "$REPORT_JSON"
    fi
}

for hf in $HASH_FILES; do
    while read -r file; do
        if [ $(date +%s) -ge $VALIDATION_END ]; then
            echo "Fin de la fenêtre de validation."
            break 2
        fi
        validate_file "$file"
    done < "$hf"
done

# Clôture JSON
echo "  ]" >> "$REPORT_JSON"
echo "}" >> "$REPORT_JSON"

# Informations supplémentaires
TOTAL_ERRORS=$(wc -l < "$ERROR_FILE")
TOTAL_SIZE=$(du -sh "$DST" | awk '{print $1}')
START_TIME=$(date -d "$(head -1 "$LOG_DIR/rsync_transfer_*.log")" +%s)
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))

# Tableau lisible
{
echo "Date: $(date)"
echo "Total fichiers copiés: $TOTAL_COPIED"
echo "Total fichiers en erreur: $TOTAL_ERRORS"
echo "Taille totale copiée: $TOTAL_SIZE"
echo "Durée du transfert: $DURATION secondes"
} >> "$REPORT_TABLE"

# Email avec les deux rapports
mailx -s "$MAIL_SUBJECT" -a "$REPORT_JSON" -a "$REPORT_TABLE" "$EMAIL_TO" < <(echo "Rapport Rsync attaché")

Ordonnancement

4️⃣ Systemd Services et Timers

a) Service rsync : /etc/systemd/system/rsync_transfer.service

[Unit]
Description=Transfert rsync du serveur A vers B

[Service]
Type=oneshot
ExecStart=/opt/rsync_sync/transfert_rsync.sh

b) Timer rsync : /etc/systemd/system/rsync_transfer.timer

[Unit]
Description=Timer pour lancer rsync chaque weekend

[Timer]
OnCalendar=Fri 18:00
Persistent=true

[Install]
WantedBy=timers.target

c) Service validation hash : /etc/systemd/system/rsync_validation.service

[Unit]
Description=Validation hash Rsync

[Service]
Type=oneshot
ExecStart=/opt/rsync_sync/validation_hash.sh

d) Timer validation hash : /etc/systemd/system/rsync_validation.timer

[Unit]
Description=Timer validation hash Rsync

[Timer]
OnCalendar=Mon 08:01
Persistent=true

[Install]
WantedBy=timers.target

e) Activer les timers

sudo systemctl daemon-reload
sudo systemctl enable --now rsync_transfer.timer
sudo systemctl enable --now rsync_validation.timer

Crontab

1️⃣ Planification rsync via cron (vendredi 18h → lundi 08h)

  1. Ouvrir le crontab pour l’utilisateur qui exécute le script :
    crontab -e
    
  2. Ajouter la ligne suivante pour lancer le transfert vendredi 18h :
    0 18 * * 5 /opt/rsync_sync/transfert_rsync.sh
    
    0 18 * * 5 → vendredi à 18h00
  3. Ajouter une ligne optionnelle pour assurer le démarrage lundi 08h (au cas où le transfert serait interrompu ou pour reprise) :
    0 8 * * 1 /opt/rsync_sync/transfert_rsync.sh
    
    0 8 * * 1 → lundi 08h00

Les scripts incluent déjà un contrôle de fin de fenêtre (RSYNC_END) pour arrêter le transfert lundi 08h00.

2️⃣ Planification validation hash via cron (lundi 08h01 → vendredi 17h59)

  1. Toujours dans le crontab :
    1 8 * * 1 /opt/rsync_sync/validation_hash.sh
    
    Lancement lundi 08h01
  2. Optionnel : relancer la validation toutes les heures pendant la semaine, pour traiter les fichiers par lots si nécessaire :
    0 9-17 * * 1-5 /opt/rsync_sync/validation_hash.sh
    
    De 9h à 17h du lundi au vendredi, toutes les heures. Le script s’arrête automatiquement si la fenêtre VALIDATION_END (vendredi 17h59) est dépassée

3️⃣ Avantages de cette version cron

Compatible tous serveurs Linux/Unix

  • Même logique que Systemd :
  • Reprise automatique des erreurs
  • Fenêtres horaires respectées
  • Rapports JSON + tableau lisible
  • Notifications email

Facile à tester sans redémarrer le daemon systemd