#! /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 # 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" iconborg="--window-icon=/usr/share/icons/borg.svg" # FONCTIONS ################################################################################# TestBorgRepo () { if [ ! -f "config" ]; then infoerr="Impossible de trouver le fichier de configuration borg." zenity $W --error --text="$infoerr" exit fi if [ ! -d "data" ]; then infoerr="Impossible de trouver le fichier de configuration borg." zenity $W --error --text="$infoerr" exit fi if [ -d "lock.exclusive" ]; then infoerr="Le dépôt est verrouillé, une opération est peut-être en cours" zenity $W --error --text="$infoerr" exit fi } bobainfoa () { TestBorgRepo borg info ./::"$borg_repo" } DossierPresent () { if [ ! -d "$1" ]; then inforerror="Erreur, le dossier $1 est absent." zenity $W --error --text="$inforerror" VarDossierPresent="false" fi } ZenityPulsate () { zenity $W $iconborg --progress --pulsate --auto-close } BorgFilter () { if [ "$filter" = "" ];then Borg_Archive=$(borg list --short "$borg_repo" | zenity $H $W $iconborg --list --title "Listes des archives" --column "Archive") else Borg_Archive=$(borg list --short "$borg_repo" | grep "$filter" | zenity $H $W $iconborg --list --title "Listes des archives" --column "Archive") fi } ################################################################################# # 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"\ ) && . "$dossierprofil"/"$fichierprofil".conf 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" exit elif [ "$borg_repo" = "" ]; then infoerr="Variable \$borg_repo absente" zenity $W --error --text="$infoerr" exit fi if ! cd "$borg_repo" ;then zenity $W --error --text="Impossible de trouver de dépôt borg." exit else TestBorgRepo fi ################################################################################# # Fichier de configuration globale ################################################################################# if [ -f $HOME/.config/borg/borg-zenity.conf ];then . $HOME/.config/borg/borg-zenity.conf fi ################################################################################# # MENU PRINCIPAL Gestion des options du menu ################################################################################# 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 MenuDemonter="dm\nDémonter une archive\ndmt\nDémonter toutes les archives\n" MenuSupprimer="" MenuRenommer="" MenuInfo="" MenuCreer="" else MenuDemonter="" MenuSupprimer="ss\nSupprimer une archive\nsss\nSupprimer des archives\n" 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 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 ################################################################################# # Affichage du menu ################################################################################# if [ $menu_avance = "true" ];then choixmenu=$(echo -e "\ $(echo $MenuCreer)\ ms\nMonter une archive\n\ $(echo $MenuDemonter)\ $(echo $MenuSupprimer)\ $(echo $MenuRenommer)\ $(echo $MenuInfo)\ fi\nFiltre : "$filter"\n\ --\n------------------------------------------\n\ id\nInformation sur le dépôt\n\ ap\nAfficher le profil\n\ --\n------------------------------------------\n\ $(echo $MenuEspaceDisque)\ qu\nQUITTER\ " | zenity $iconborg --list $H $W --title "$nomsauvegarde" \ --hide-header --hide-column=1 --column "id" --column "choix") else choixmenu=$(echo -e "\ $(echo $MenuCreer)\ ms\nMonter une archive\n\ $(echo $MenuDemonter)\ $(echo $MenuSupprimer)\ $(echo $MenuInfo)\ $(echo $MenuEspaceDisque)\ qu\nQUITTER\ " | 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 "" elif [ "$choixmenu" = "cs" ];then # Créer une archive TestBorgRepo 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 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.log" borg create ${borg_excludes} ${borg_compress} "$borg_repo"::"$borg_archive" "." >> "$log" | ZenityPulsate chmod 777 "$log" if [ $(wc -c "$log" | cut -f1 -d " ") -gt 1 ] ;then zenity $W --error --title "borg-zenity erreur" --text "$(cat $log)" else sync | ZenityPulsate zenity $iconborg --notification --text "Sauvegarde $nomsauvegarde terminée" zenity $W --info --text "Sauvegarde $nomsauvegarde terminée" fi fi fi elif [ "$choixmenu" = "ms" ];then # Monter une archive BorgFilter if [ ! "$Borg_Archive" = "" ];then mkdir $HOME/"$Borg_Archive" borg mount "$borg_repo"::"$Borg_Archive" $HOME/"$Borg_Archive" | ZenityPulsate zenity $W --info --text="La sauvegarde est disponible dans le dossier\n$HOME/$Borg_Archive" fi elif [ "$choixmenu" = "dm" ];then # 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 sleep 2 | ZenityPulsate rmdir "$ptnmontage" | ZenityPulsate fi elif [ "$choixmenu" = "dmt" ];then # 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)" zenity --notification $iconborg --text="Démontage de $ptnmontage" fusermount -uz "$ptnmontage" && sleep 2 && rmdir "$ptnmontage" done elif [ "$choixmenu" = "ss" ];then # Supprimer une archive BorgFilter if [ ! "$Borg_Archive" = "" ];then borg delete "$borg_repo"::"$Borg_Archive" | ZenityPulsate zenity $W --info --text="Suppresion de l'archive $Borg_Archive terminée" fi elif [ "$choixmenu" = "sss" ];then # Supprimer des archives if [ "$filter" = "" ];then 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 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 zenity $W --info --text="Suppression des archives terminée.\nArchives supprimées : $(echo "$Borg_Archive" | sed s/\|/\ /g)" fi elif [ "$choixmenu" = "ra" ];then # renommer une archive BorgFilter if [ ! "$Borg_Archive" = "" ];then borg_nouveaunom=$(zenity $W --entry --text "Nouveau nom" --entry-text="$Borg_Archive") borg rename "$borg_repo"::"$Borg_Archive" "$borg_nouveaunom" | ZenityPulsate fi elif [ "$choixmenu" = "is" ];then # Information archive BorgFilter if [ ! "$Borg_Archive" = "" ];then zenity $W --info --text="$(borg info "$borg_repo"::"$Borg_Archive")" | ZenityPulsate fi elif [ "$choixmenu" = "fi" ];then #Filtre filter=$(zenity $W --entry) elif [ "$choixmenu" = "id" ];then # Information sur le dépôt zenity $W --info --text="$(borg info "$borg_repo")" | ZenityPulsate elif [ "$choixmenu" = "ap" ];then # Afficher le profil zenity $W $H --info --text="$(cd "$dossierprofil" && cat "$fichierprofil".conf)" 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