borg-zenity/borg-gui.sh

354 lines
12 KiB
Bash
Raw Normal View History

2021-03-07 14:52:30 +01:00
#! /bin/bash
# tests de base
if ! which zenity ; then
echo "Zenity n'est pas installé.";exit
exit
fi
if ! which borg ; then
echo "borgbackup n'est pas installé.";exit
exit
fi
2021-03-07 14:52:30 +01:00
# META VARIABLES
#################################################################################
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
dossierprofil="$HOME/.config/borg/profils-borg-zenity/"
W="--width=600"
H="--height=500"
2022-04-18 17:40:13 +02:00
iconborg="--window-icon=/usr/share/icons/borg.svg"
2021-03-07 14:52:30 +01:00
# FONCTIONS
#################################################################################
TestBorgRepo () {
if [ ! -d "$borg_repo" ];then
zenity $W --error --text="Impossible de trouver de dépôt borg."
exit
else
echo "[OK] Le dépôt borg existe"
fi
if ! ls "$borg_repo";then
zenity $W --error --text="Impossible d'accéder au dépôt borg.\nIl s'agit probablement d'un problème de droits"
exit
else
echo "[OK] L'accès au dépôt borg est possible"
fi
if ! ls "$borg_repo/data";then
zenity $W --error --text="Impossible d'accéder aux données du dépôt borg.\nIl s'agit probablement d'un problème de droits"
exit
else
echo "[OK] L'accès aux données du dépôt borg est possible"
fi
if [ ! -f "$borg_repo/config" ]; then
infoerr="Impossible de trouver le fichier de configuration borg."
zenity $W --error --text="$infoerr"
exit
else
echo "[OK] Le fichier de configuration a été trouvé"
fi
if [ -d "$borg_repo/lock.exclusive" ]; then
infoerr="Le dépôt est verrouillé, une opération est peut-être en cours"
zenity $W --error --text="$infoerr"
exit
else
echo "[OK] Le dépôt n'est pas verrouillé"
fi
2021-03-07 14:52:30 +01:00
}
DossierPresent () {
if [ ! -d "$1" ]; then
inforerror="Erreur, le dossier $1 est absent."
zenity $W --error --text="$inforerror"
VarDossierPresent="false"
fi
2021-03-07 14:52:30 +01:00
}
ZenityPulsate () {
zenity $W $iconborg --title "borg-zenity" --progress --pulsate --auto-close --text "$1"
}
ZenityNotification () {
zenity $iconborg --notification --text "$1"
}
2021-08-04 20:32:21 +02:00
BorgFilter () {
if [ "$filter" = "" ];then
ZenityNotification "borg-zenity\nGénération de la liste des archives en cours..."
BorgArchiveList=$(borg list --short "$borg_repo")
Borg_Archive=$(echo -e "$BorgArchiveList" | zenity $H $W $iconborg --list --title "Listes des archives" --column "Archive")
2021-08-04 20:32:21 +02:00
else
BorgArchiveList=$(borg list --short "$borg_repo")
Borg_Archive=$(echo -e "$BorgArchiveList" | grep "$filter" | zenity $H $W $iconborg --list --title "Listes des archives" --column "Archive")
2021-08-04 20:32:21 +02:00
fi
}
#################################################################################
2021-03-07 14:52:30 +01:00
# Profils et tests
#################################################################################
cd "$dossierprofil" || exit
fichierprofil=$(ls -1 *.conf | cut -d. -f1 | \
zenity $W $H $iconborg --list \
--title "Liste des profils" \
--text "Choisir un profil dans la liste" \
--column "Fichier de profil" \
--hide-header \
) && . "$dossierprofil"/"$fichierprofil".conf
2021-03-07 14:52:30 +01:00
if [ "$fichierprofil" = "" ]; then
infoerr="Aucun profil choisi."
zenity $W --error --text="$infoerr"
exit
elif [ "$nomsauvegarde" = "" ]; then
infoerr="Nom de sauvegarde non défini."
zenity $W --error --text="$infoerr"
2021-03-07 14:52:30 +01:00
exit
elif [ "$borg_repo" = "" ]; then
infoerr="Variable \$borg_repo absente"
zenity $W --error --text="$infoerr"
2021-03-07 14:52:30 +01:00
exit
fi
TestBorgRepo
#################################################################################
# Fichier de configuration globale
#################################################################################
if [ -f $HOME/.config/borg/borg-zenity.conf ];then
. $HOME/.config/borg/borg-zenity.conf
fi
2021-03-07 14:52:30 +01:00
#################################################################################
# MENU PRINCIPAL Gestion des options du menu
2021-03-07 14:52:30 +01:00
#################################################################################
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és, cacher les entrées «supprimer et renommer»
if [ "$(mount | grep borgfs)" != "" ];then
2022-06-23 20:28:55 +02:00
MenuDemonter="dm\nDémonter une archive\ndmt\nDémonter toutes les archives\n"
MenuSupprimer=""
MenuRenommer=""
MenuInfo=""
MenuCreer=""
else
MenuDemonter=""
MenuSupprimer1="ss\nSupprimer une archive\n"
MenuSupprimerX="sss\nSupprimer des archives\n"
2022-06-23 20:28:55 +02:00
MenuRenommer="ra\nRenommer une archive\n"
MenuInfo="is\nInformation archive\n"
MenuCreer="cs\nCréer l'archive : $borg_archive\n"
fi
# afficher l'espace disque sous condition, la variable vient du fichier borg-zenity.conf
2022-06-23 17:56:25 +02:00
EspaceDisqueUtil=$(df -k --output=pcent "$borg_repo" | tail -n +2 | cut -d% -f1)
if [ $EspaceDisqueUtil -gt $seuil_alerte_espace_disque ];then
MenuEspaceDisque="ed\n/!\ Espace disque utilisé : $EspaceDisqueUtil %\n"
else
MenuEspaceDisque=""
fi
2022-10-13 18:00:50 +02:00
# notification si espace disque faible
NotificationEspaceDisque () {
if [ $EspaceDisqueUtil -gt $seuil_alerte_espace_disque ];then
ZenityNotification "borg-zenity\nEspace disque faible, $EspaceDisqueUtil% occupé"
2022-10-13 18:00:50 +02:00
fi
}
MenuCompacter="co\nCompacter le dépôt\n"
2022-10-13 18:00:50 +02:00
MenuMonter="ms\nMonter une archive\n"
MenuFiltre="fi\nFiltre : "$filter"\n"
MenuBarreHorizontale="--\n------------------------------------------\n"
MenuInfoDepot="id\nInformation sur le dépôt\n"
MenuAfficherProfil="ap\nAfficher le profil\n"
2022-11-06 09:14:58 +01:00
MenuQuitter="qu\nQUITTER"
#################################################################################
# Affichage du menu
#################################################################################
if [ $menu_avance = "true" ];then
2022-08-11 18:07:41 +02:00
2022-10-13 18:00:50 +02:00
NotificationEspaceDisque
2022-08-11 18:07:41 +02:00
# menu avancé
2021-03-07 14:52:30 +01:00
choixmenu=$(echo -e "\
$(echo $MenuCreer)\
2022-10-13 18:00:50 +02:00
$(echo $MenuMonter)\
$(echo $MenuDemonter)\
$(echo $MenuSupprimer1)\
$(echo $MenuSupprimerX)\
$(echo $MenuRenommer)\
$(echo $MenuCompacter)\
$(echo $MenuInfo)\
2022-10-13 18:00:50 +02:00
$(echo $MenuFiltre)\
$(echo $MenuBarreHorizontale)\
$(echo $MenuInfoDepot)\
$(echo $MenuAfficherProfil)\
$(echo $MenuBarreHorizontale)\
$(echo $MenuEspaceDisque)\
2022-10-13 18:00:50 +02:00
$(echo $MenuQuitter)\
2022-11-06 09:14:58 +01:00
" | zenity $iconborg --list $H $W --title "$nomsauvegarde" --hide-header --hide-column=1 --column "id" --column "choix")
else
2022-08-11 18:07:41 +02:00
2022-10-13 18:00:50 +02:00
NotificationEspaceDisque
2022-08-11 18:07:41 +02:00
# menu simple
choixmenu=$(echo -e "\
$(echo $MenuCreer)\
2022-10-13 18:00:50 +02:00
$(echo $MenuMonter)\
$(echo $MenuDemonter)\
$(echo $MenuSupprimer1)\
$(echo $MenuEspaceDisque)\
2022-10-13 18:00:50 +02:00
$(echo $MenuQuitter)\
2022-11-06 09:14:58 +01:00
" | zenity $iconborg --list $H $W --title "BORG GUI : $nomsauvegarde" --hide-header --hide-column=1 --column "id" --column "choix")
fi
#################################################################################
# Gestion des choix du menu
#################################################################################
if [ "$choixmenu" = "" ];then
echo ""
2021-07-21 17:45:46 +02:00
elif [ "$choixmenu" = "cs" ];then
# Créer une archive
TestBorgRepo
2022-06-23 17:56:25 +02:00
DossierPresent "$borg_repo"
DossierPresent "$borg_dir"
cd "$borg_dir"
if [ ! "$?" = "0" ] ;then
zenity $W --error --text "Impossible de se rendre dans le dossier de sauvegarde."
exit
fi
2022-07-21 21:22:04 +02:00
if borg list --short "$borg_repo" | grep "$borg_archive" ;then
zenity --warning $W $iconborg --text "L'archive existe déjà."
else
if [ "$VarDossierPresent" != "false" ];then
log="/tmp/borg-zenity-$borg_archive-$(date +%H-%M-%S).log"
borg create ${borg_excludes} ${borg_compress} "$borg_repo"::"$borg_archive" . 2> "$log" | zenity $W $iconborg --progress --pulsate --auto-close --no-cancel --text "Sauvegarde en cours..." --title "borg-zenity"
chmod 777 "$log"
if [ $(wc -c "$log" | cut -f1 -d " ") -gt 1 ] ;then
zenity $W --error --title "borg-zenity erreur" --text "$(cat $log)"
fi
sync | ZenityPulsate "Synchronisation des écritures..."
ZenityNotification "borg-zenity\nSauvegarde $nomsauvegarde terminée"
zenity $W --info --text "Sauvegarde $nomsauvegarde terminée"
fi
fi
2021-03-07 14:52:30 +01:00
elif [ "$choixmenu" = "ms" ];then
# Monter une archive
2021-08-04 20:32:21 +02:00
BorgFilter
if [ ! "$Borg_Archive" = "" ];then
2022-06-23 17:56:25 +02:00
mkdir $HOME/"$Borg_Archive"
borg mount "$borg_repo"::"$Borg_Archive" $HOME/"$Borg_Archive" | ZenityPulsate "Montage de l'archive..."
zenity $W --info --text="La sauvegarde est disponible dans le dossier\n<tt>$HOME/$Borg_Archive</tt>"
fi
2021-07-04 21:54:04 +02:00
elif [ "$choixmenu" = "dm" ];then
2022-04-15 11:34:24 +02:00
# Démonter une archive
ptnmontage=$(mount | grep borgfs | cut -d" " -f3 | zenity $W $H $iconborg --list --title "Liste des points de montages" --column "Archive")
if [ ! "$ptnmontage" = "" ];then
fusermount -u "$ptnmontage" | ZenityPulsate "Démontage de l'archive..."
sleep 2 | ZenityPulsate ""
rmdir "$ptnmontage" | ZenityPulsate "Suppression du point du montage..."
fi
ZenityNotification "borg-zenity\nDémontage de $ptnmontage"
2021-03-07 14:52:30 +01:00
elif [ "$choixmenu" = "dmt" ];then
2022-04-15 11:34:24 +02:00
# Démonter toutes les archives
while [ $(mount | grep borgfs | cut -d" " -f3 | head -n 1) ];do
ptnmontage="$(mount | grep borgfs | cut -d" " -f3 | head -n 1)"
ZenityNotification "borg-zenity\nDémontage de $ptnmontage"
2022-07-21 21:22:04 +02:00
fusermount -uz "$ptnmontage" && sleep 2 && rmdir "$ptnmontage"
done
elif [ "$choixmenu" = "ss" ];then
2022-04-15 11:34:24 +02:00
# Supprimer une archive
2021-08-04 20:32:21 +02:00
BorgFilter
if [ ! "$Borg_Archive" = "" ];then
borg delete "$borg_repo"::"$Borg_Archive" | ZenityPulsate "Suppresion de l'archive..."
zenity $W --info --text="Suppresion de l'archive $Borg_Archive terminée"
fi
elif [ "$choixmenu" = "sss" ];then
2022-04-15 11:34:24 +02:00
# Supprimer des archives
if [ "$filter" = "" ];then
2022-07-12 21:43:17 +02:00
Borg_Archive=$(borg list --short "$borg_repo" | sed s/^/\\n/g | zenity $H $W $iconborg --list --checklist --title "Listes des archives" --column "Sel" --column "Archive")
else
2022-07-12 21:43:17 +02:00
Borg_Archive=$(borg list --short "$borg_repo" | grep "$filter" | sed s/^/S\\n/g | zenity $H $W $iconborg --list --checklist --title "Listes des archives" --column "S" --column "Archive")
fi
if [ ! "$Borg_Archive" = "" ];then
borg delete "$borg_repo"::$(echo "$Borg_Archive" | sed s/\|/\ /g) | ZenityPulsate "Suppression de l'archive..."
2022-04-15 11:34:24 +02:00
zenity $W --info --text="Suppression des archives terminée.\nArchives supprimées : $(echo "$Borg_Archive" | sed s/\|/\ /g)"
fi
elif [ "$choixmenu" = "co" ];then
# Compacter le dépôt
borg compact --verbose "$borg_repo" 2> /tmp/borg-compact.log | ZenityPulsate "Compactage du dépôt..."
zenity $W --info --text "$(cat /tmp/borg-compact.log)"
elif [ "$choixmenu" = "ra" ];then
# renommer une archive
BorgFilter
if [ ! "$Borg_Archive" = "" ];then
borg_nouveaunom=$(zenity $W --entry --text "Nouveau nom" --entry-text="$Borg_Archive")
if [ "$borg_nouveaunom" = "$Borg_Archive" ];then
ZenityNotification "Renommage annulé"
elif [ "$borg_nouveaunom" = "" ];then
ZenityNotification "Renommage annulé"
elif [ "$Borg_Archive" != "$borg_nouveaunom" ];then
borg rename "$borg_repo"::"$Borg_Archive" "$borg_nouveaunom" | ZenityPulsate "Renommage de l'archive..."
ZenityNotification "Renommage terminé\n"$Borg_Archive" -> "$borg_nouveaunom""
fi
fi
2021-03-07 14:52:30 +01:00
elif [ "$choixmenu" = "is" ];then
2021-08-04 20:32:21 +02:00
# Information archive
BorgFilter
if [ ! "$Borg_Archive" = "" ];then
zenity $W --info --text="<tt>$(borg info "$borg_repo"::"$Borg_Archive")</tt>" | ZenityPulsate "Récupération des informations..."
fi
elif [ "$choixmenu" = "fi" ];then
2021-08-04 20:32:21 +02:00
#Filtre
2022-07-23 12:52:41 +02:00
filter=$(zenity $W --entry --entry-text "$filter")
2021-03-07 14:52:30 +01:00
elif [ "$choixmenu" = "id" ];then
2021-08-04 20:32:21 +02:00
# Information sur le dépôt
zenity $W --info --text="<tt>$(borg info "$borg_repo")</tt>" | ZenityPulsate "Récupération des informations..."
2021-03-07 14:52:30 +01:00
elif [ "$choixmenu" = "ap" ];then
2021-08-04 20:32:21 +02:00
# Afficher le profil
2022-04-29 19:45:49 +02:00
zenity $W $H --info --text="<tt>$(cd "$dossierprofil" && cat "$fichierprofil".conf)</tt>"
elif [ "$choixmenu" = "qu" ];then
if [ "$(mount | grep borgfs)" = "" ];then
quitter="yes"
else
zenity $W --warning --text="Une ou des archives sont montées.\nDémontez-le afin de laisser le dépôt dans un état cohérent."
fi
fi
done