Add colored logging with shared common.sh library
- Create scripts/common.sh with reusable log() function and color definitions - Refactor backup.sh to use common.sh for consistent logging - Add color support: ERROR (red), WARN (yellow), SUCCESS (green), INFO (normal) - Colors only appear in terminal, plain text in log files - Improve code organization and DRY principle - Fix shellcheck spacing warnings in backup.sh 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,54 +1,63 @@
|
||||
#!/bin/bash
|
||||
# scripts/backup.sh - Backup complet Nextcloud
|
||||
# ============================================
|
||||
# Script de sauvegarde Nextcloud
|
||||
# ============================================
|
||||
# Ce script crée une sauvegarde complète de:
|
||||
# - Données utilisateur
|
||||
# - Données utilisateur
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Variables globales
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
if [ -f .env ]; then
|
||||
set -a
|
||||
source .env
|
||||
set +a
|
||||
else
|
||||
echo -e "${RED}Erreur: Fichier .env non trouvé${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Variables
|
||||
DATE=$(date +%Y%m%d_%H%M%S)
|
||||
MAINTENANCE_ENABLED=false
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
cd "$PROJECT_ROOT"
|
||||
|
||||
LOCK_FILE="/tmp/nextcloud_backup.lock"
|
||||
LOG_DIR="./logs"
|
||||
|
||||
LOG_DIR="${LOG_DESTINATION:-./logs}"
|
||||
LOG_FILE="$LOG_DIR/backup_$(date +%Y%m%d_%H%M%S).log"
|
||||
MAINTENANCE_ENABLED=false
|
||||
|
||||
# Créer le dossier de logs
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Fonction de logging
|
||||
log() {
|
||||
local level="$1"
|
||||
shift
|
||||
local message="$*"
|
||||
local timestamp
|
||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "[$timestamp] [$level] $message" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Charger les variables d'environnement
|
||||
if [ ! -f .env ]; then
|
||||
log "ERROR" "Fichier .env introuvable"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Charger .env de manière sécurisée
|
||||
set -a
|
||||
# shellcheck disable=SC1091
|
||||
source .env
|
||||
set +a
|
||||
|
||||
# Vérifier les variables requises
|
||||
: "${MYSQL_USER:?Variable MYSQL_USER non définie}"
|
||||
: "${MYSQL_PASSWORD:?Variable MYSQL_PASSWORD non définie}"
|
||||
: "${MYSQL_DATABASE:?Variable MYSQL_DATABASE non définie}"
|
||||
|
||||
BACKUP_DIR="${BACKUP_DESTINATION:-./backups}"
|
||||
BACKUP_RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-7}"
|
||||
DATE=$(date +%Y%m%d_%H%M%S)
|
||||
BACKUP_NAME="nextcloud_backup_$DATE"
|
||||
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"
|
||||
|
||||
source "$SCRIPT_DIR/common.sh"
|
||||
|
||||
if [ -z "${MYSQL_DATABASE:-}" ] || [ -z "${MYSQL_USER:-}" ] || [ -z "${MYSQL_PASSWORD:-}" ]; then
|
||||
log "ERROR" "Variables MysqlSQL non définies dans .env"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Vérifier qu'un backup n'est pas déjà en cours
|
||||
if [ -f "$LOCK_FILE" ]; then
|
||||
log "ERROR" "Une sauvegarde est déjà en cours. Si aucune sauvegarde n'est en cours, supprimez le fichier: $LOCK_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
touch "$LOCK_FILE"
|
||||
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Fonction de nettoyage en cas d'erreur
|
||||
cleanup() {
|
||||
local exit_code=$?
|
||||
@@ -83,15 +92,6 @@ cleanup() {
|
||||
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
# Vérifier qu'un backup n'est pas déjà en cours
|
||||
if [ -f "$LOCK_FILE" ]; then
|
||||
log "ERROR" "Un backup est déjà en cours (lock file: $LOCK_FILE)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Créer le lock file
|
||||
echo $$ > "$LOCK_FILE"
|
||||
|
||||
log "INFO" "=== Démarrage du backup: $BACKUP_NAME ==="
|
||||
log "INFO" "Log file: $LOG_FILE"
|
||||
|
||||
|
||||
33
scripts/common.sh
Normal file
33
scripts/common.sh
Normal file
@@ -0,0 +1,33 @@
|
||||
# scripts/common.sh - Fonctions communes à tous les scripts
|
||||
|
||||
# Couleurs
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
# Fonction de logging
|
||||
log() {
|
||||
local level="$1"
|
||||
shift
|
||||
local message="$*"
|
||||
local timestamp
|
||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
local color=""
|
||||
case "$level" in
|
||||
ERROR) color="${RED}" ;;
|
||||
WARN) color="${YELLOW}" ;;
|
||||
SUCCESS) color="${GREEN}" ;;
|
||||
INFO) color="" ;;
|
||||
esac
|
||||
|
||||
echo "[$timestamp] [$level] $message" >>"${LOG_FILE:-/tmp/script.log}"
|
||||
|
||||
if [ -t 1 ]; then
|
||||
echo -e "${color}[$timestamp] [$level] $message${NC}"
|
||||
else
|
||||
echo "[$timestamp] [$level] $message"
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user