From c22146b0982f159fe300402072d220c6729570a6 Mon Sep 17 00:00:00 2001 From: Matthieu THOLOT Date: Tue, 19 May 2026 08:21:58 +0000 Subject: [PATCH] feat(backup): zip and rotate uploads for inventory/sirh/lesstime Aligns the repo with the version that was running in prod. Adds INVENTORY/SIRH/LESSTIME_DOCS_DIR variables, zips each upload directory next to the DB dumps, keeps the 3 most recent zips, and splits the Discord summary into BDD/Documents sections. --- backup.sh | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/backup.sh b/backup.sh index d0d246f..6e79612 100755 --- a/backup.sh +++ b/backup.sh @@ -8,6 +8,9 @@ source .env BACKUP_DIR="./backups" DATABASES=("sirh_prod" "inventory_prod" "lesstime_prod" "starseed_prod") +INVENTORY_DOCS_DIR="/var/www/inventory/storage" +SIRH_DOCS_DIR="/var/www/sirh/uploads" +LESSTIME_DOCS_DIR="/var/www/lesstime/uploads" DATE=$(date +%Y-%m-%d_%H%M%S) LOG_FILE="${BACKUP_DIR}/backup.log" WEBHOOK_URL="${DISCORD_WEBHOOK_URL:-}" @@ -30,7 +33,7 @@ discord() { fail() { log "ERROR: $1" - discord 16711680 "Backup PostgreSQL - ECHEC" "$1" + discord 16711680 "Backup - ECHEC" "$1" exit 1 } @@ -39,10 +42,15 @@ command -v docker &>/dev/null || fail "docker n'est pas installe" # Supprimer les anciens backups (on ne garde que le dernier) rm -f "${BACKUP_DIR}"/*.sql +# Garder les 3 derniers zips de documents, supprimer les plus anciens +for PREFIX in inventory_docs sirh_docs lesstime_docs; do + ls -t "${BACKUP_DIR}"/${PREFIX}-*.zip 2>/dev/null | tail -n +4 | xargs -r rm -f || true +done log "Debut backup PostgreSQL" -DETAILS="" +DB_DETAILS="" +DOC_DETAILS="" ERRORS=0 for DB in "${DATABASES[@]}"; do @@ -50,17 +58,44 @@ for DB in "${DATABASES[@]}"; do if docker compose exec -T postgres pg_dump -U admin "$DB" > "${BACKUP_DIR}/${DB}-${DATE}.sql" 2>/dev/null; then SIZE=$(du -h "${BACKUP_DIR}/${DB}-${DATE}.sql" | cut -f1) log "${DB} sauvegarde (${SIZE})" - DETAILS="${DETAILS}\\n- **${DB}** : ${SIZE}" + DB_DETAILS="${DB_DETAILS}\\n- **${DB}** : ${SIZE}" else log "ERREUR sur ${DB}" - DETAILS="${DETAILS}\\n- **${DB}** : ERREUR" + DB_DETAILS="${DB_DETAILS}\\n- **${DB}** : ERREUR" ((ERRORS++)) fi done +# Backup des documents (Inventory, SIRH, LessTime) +declare -A DOC_DIRS=( + ["inventory_docs"]="$INVENTORY_DOCS_DIR" + ["sirh_docs"]="$SIRH_DOCS_DIR" + ["lesstime_docs"]="$LESSTIME_DOCS_DIR" +) + +for NAME in "${!DOC_DIRS[@]}"; do + DIR="${DOC_DIRS[$NAME]}" + if [[ -d "$DIR" ]]; then + log "Zipping ${NAME}..." + ZIP_FILE="${BACKUP_DIR}/${NAME}-${DATE}.zip" + if zip -rq "$ZIP_FILE" "$DIR" 2>/dev/null; then + SIZE=$(du -h "$ZIP_FILE" | cut -f1) + log "${NAME} sauvegarde (${SIZE})" + DOC_DETAILS="${DOC_DETAILS}\\n- **${NAME}** : ${SIZE}" + else + log "ERREUR sur ${NAME}" + DOC_DETAILS="${DOC_DETAILS}\\n- **${NAME}** : ERREUR" + ((ERRORS++)) + fi + else + log "WARN: Dossier ${NAME} introuvable ($DIR)" + DOC_DETAILS="${DOC_DETAILS}\\n- **${NAME}** : dossier introuvable" + fi +done + if [[ $ERRORS -gt 0 ]]; then - fail "Backup termine avec ${ERRORS} erreur(s) :${DETAILS}" + fail "Backup termine avec ${ERRORS} erreur(s) :\\n\\n**BDD :**${DB_DETAILS}\\n\\n**Documents :**${DOC_DETAILS}" fi log "Backup termine avec succes" -discord 65280 "Backup PostgreSQL - OK" "Backup **${DATE}** termine\\n${DETAILS}" +discord 65280 "Backup - OK" "Backup **${DATE}**\\n\\n**BDD :**${DB_DETAILS}\\n\\n**Documents (3 derniers conserves) :**${DOC_DETAILS}"