Przeglądaj źródła

[2.1] better display and error handling

Penta 3 miesięcy temu
rodzic
commit
6ffa58691f
1 zmienionych plików z 38 dodań i 9 usunięć
  1. 38 9
      entrypoint.sh

+ 38 - 9
entrypoint.sh

@@ -8,29 +8,58 @@ RETENTION_DAYS=${RETENTION_DAYS:-14}
 # Array contenant les dossiers à sauvegarder (split des espaces)
 DIRECTORIES_ARRAY=($DIRECTORIES)
 
-# Création du dossier de backup s'il n'existe pas
-if [ ! -d "$BACKUP_DIR" ]; then
-    echo "ERROR: Backup directory does not exist!"
+# Message de démarrage
+echo "INFO: Starting backup script (v2.1) with the following variables"
+echo "INFO: - BACKUP_DIR: $BACKUP_DIR"
+echo "INFO: - DIRECTORIES: $DIRECTORIES"
+echo "INFO: - RETENTION_DAYS: $RETENTION_DAYS"
+
+# Fonction de sortie sur erreur
+exit_with_error() {
+    echo "ERROR: $1"
     exit 1
+}
+
+# Vérification du dossier de backup
+if [ ! -d "$BACKUP_DIR" ]; then
+    exit_with_error "Backup directory '$BACKUP_DIR' does not exist!"
 fi
 
 # Boucle pour chaque dossier dans l'array
 for dir in "${DIRECTORIES_ARRAY[@]}"; do
     # Vérification si le dossier existe
     if [ ! -d "$dir" ]; then
-        echo "WARNING: Source directory $dir does not exist."
+        echo "WARNING: Source directory $dir does not exist. Skipping..."
         continue
     fi
     
     # Création du nom de fichier de backup avec la date
     base_name=$(basename "$dir")
-    file_name="${BACKUP_DIR}/${base_name}_$(date +%Y%m%d).tar.gz"
+    file_name="${BACKUP_DIR}/${base_name}_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
+    
+    echo "INFO: Backuping '$dir'..."
     
     # Création du backup zip
-    tar -czf "$file_name" "$dir"
-    echo "INFO: Backup of '$dir' successful: $file_name"
+    output=$(tar -czf "$file_name" "$dir" 2>&1)
+    
+    # Affichage du message de succès ou d'erreur
+    if [ $? -eq 0 ]; then
+        echo "INFO: Backup of '$dir' successful: $file_name"
+    else
+        echo "ERROR: Failed to backup '$dir'"
+        echo "ERROR: $output"
+        continue
+    fi
 done
 
 # Suppression des backups plus vieux que ${RETENTION_DAYS} jours
-find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +${RETENTION_DAYS} -delete
-echo "INFO: Cleaning of old backups done."
+echo "INFO: Checking for backups older than ${RETENTION_DAYS} days..."
+old_files=$(find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +${RETENTION_DAYS})
+if [ -n "$old_files" ]; then
+    echo "INFO: Deleting the following files: $old_files"
+    
+    # Suppression réelle
+    echo "$old_files" | xargs rm -f
+else
+    echo "INFO: No old backups to delete."
+fi