simplification du code et gestion des erreurs

This commit is contained in:
Djan GICQUEL 2022-06-25 19:46:50 +02:00
parent be321e0ca8
commit bad3277f97
1 changed files with 24 additions and 25 deletions

View File

@ -1,14 +1,12 @@
#! /bin/bash #! /bin/bash
# tests de base # tests de base
which zenity if ! which zenity ; then
if [ "$?" != "0" ]; then
echo "Zenity n'est pas installé.";exit echo "Zenity n'est pas installé.";exit
exit exit
fi fi
which borg if ! which borg ; then
if [ "$?" != "0" ]; then
echo "borgbackup n'est pas installé.";exit echo "borgbackup n'est pas installé.";exit
exit exit
fi fi
@ -26,22 +24,19 @@ iconborg="--window-icon=/usr/share/icons/borg.svg"
################################################################################# #################################################################################
TestBorgRepo () { TestBorgRepo () {
if [ ! -f "config" ]; then if [ ! -f "config" ]; then
TestBorgErr=1 infoerr="Impossible de trouver le fichier de configuration borg."
infoerr=$(echo "Impossible de trouver le fichier de configuration borg.")
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
fi fi
if [ ! -d "data" ]; then if [ ! -d "data" ]; then
TestBorgErr=1 infoerr="Impossible de trouver le fichier de configuration borg."
infoerr=$(echo "Impossible de trouver le fichier de configuration borg.")
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
fi fi
if [ -d "lock.exclusive" ]; then if [ -d "lock.exclusive" ]; then
TestBorgErr=1 infoerr="Le dépôt est verrouillé, une opération est peut-être en cours"
infoerr=$(echo "Le dépôt est bloqué, une opération est peut-être en cours")
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
fi fi
@ -54,7 +49,7 @@ bobainfoa () {
DossierPresent () { DossierPresent () {
if [ ! -d "$1" ]; then if [ ! -d "$1" ]; then
inforerror=$(echo "Erreur, le dossier $1 est absent.") inforerror="Erreur, le dossier $1 est absent."
zenity $W --error --text="$inforerror" zenity $W --error --text="$inforerror"
VarDossierPresent="false" VarDossierPresent="false"
fi fi
@ -76,7 +71,7 @@ BorgFilter () {
# Profils et tests # Profils et tests
################################################################################# #################################################################################
cd "$dossierprofil" cd "$dossierprofil" || exit
fichierprofil=$(ls -1 *.conf | cut -d. -f1 | \ fichierprofil=$(ls -1 *.conf | cut -d. -f1 | \
zenity $W $H $iconborg --list \ zenity $W $H $iconborg --list \
--title "Liste des profils" \ --title "Liste des profils" \
@ -85,21 +80,26 @@ zenity $W $H $iconborg --list \
) && . "$dossierprofil"/"$fichierprofil".conf ) && . "$dossierprofil"/"$fichierprofil".conf
if [ "$fichierprofil" = "" ]; then if [ "$fichierprofil" = "" ]; then
infoerr=$(echo "Aucun profil choisi.") infoerr="Aucun profil choisi."
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
elif [ "$nomsauvegarde" = "" ]; then elif [ "$nomsauvegarde" = "" ]; then
infoerr=$(echo "Nom de sauvegarde non défini.") infoerr="Nom de sauvegarde non défini."
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
elif [ "$borg_repo" = "" ]; then elif [ "$borg_repo" = "" ]; then
infoerr=$(echo "Variable \$borg_repo absente") infoerr="Variable \$borg_repo absente"
zenity $W --error --text="$infoerr" zenity $W --error --text="$infoerr"
exit exit
fi fi
cd "$borg_repo"
TestBorgRepo if ! cd "$borg_repo" ;then
zenity $W --error --text="Impossible de trouver de dépôt borg."
exit
else
TestBorgRepo
fi
################################################################################# #################################################################################
@ -116,6 +116,7 @@ fi
################################################################################# #################################################################################
while [ "$quitter" != "yes" ];do while [ "$quitter" != "yes" ];do
cd "$borg_repo" || exit
# si des archives sont montées afficher les entrées de menu «démonter» # si des archives sont montées afficher les entrées de menu «démonter»
# si des archives sont montés, cacher les entrées «supprimer et renommer» # si des archives sont montés, cacher les entrées «supprimer et renommer»
@ -130,7 +131,7 @@ MenuDemonter=""
MenuSupprimer="ss\nSupprimer une archive\nsss\nSupprimer des archives\n" MenuSupprimer="ss\nSupprimer une archive\nsss\nSupprimer des archives\n"
MenuRenommer="ra\nRenommer une archive\n" MenuRenommer="ra\nRenommer une archive\n"
MenuInfo="is\nInformation archive\n" MenuInfo="is\nInformation archive\n"
MenuCreer="cs\nCréer l'archive : $(echo $borg_archive)\n" MenuCreer="cs\nCréer l'archive : $borg_archive\n"
fi fi
# afficher l'espace disque sous condition, la variable vient du fichier borg-zenity.conf # afficher l'espace disque sous condition, la variable vient du fichier borg-zenity.conf
@ -190,18 +191,16 @@ fi
TestBorgRepo TestBorgRepo
DossierPresent "$borg_repo" DossierPresent "$borg_repo"
DossierPresent "$borg_dir" DossierPresent "$borg_dir" && cd "$borg_dir"
borg list --short "$borg_repo" | grep $borg_archive if borg list --short "$borg_repo" | grep $borg_archive ;then
if [ "$?" = "0" ];then
zenity --warning $W $iconborg --text "L'archive existe déjà." zenity --warning $W $iconborg --text "L'archive existe déjà."
else else
if [ "$VarDossierPresent" != "false" ];then if [ "$VarDossierPresent" != "false" ];then
cd "$borg_dir" log="/tmp/borg-zenity-$borg_archive.log"
log="$HOME/borg-zenity-$borg_archive.log" borg create $borg_excludes $borg_compress "$borg_repo"::"$borg_archive" ./ 2> "$log" | ZenityPulsate
borg create $borg_excludes $borg_compress "$borg_repo"::"$borg_archive" ./ 2>> "$log" | ZenityPulsate
chmod 777 "$log" chmod 777 "$log"
if ls "$log" ;then if [ $(wc -c "$log" | cut -f1 -d " ") -gt 1 ] ;then
zenity $W --error --title "borg-zenity erreur" --text "$(cat $log)" zenity $W --error --title "borg-zenity erreur" --text "$(cat $log)"
else else
sync | ZenityPulsate sync | ZenityPulsate