#! /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 [ ! -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 } DossierPresent () { if [ ! -d "$1" ]; then inforerror="Erreur, le dossier $1 est absent." zenity $W --error --text="$inforerror" VarDossierPresent="false" fi } ZenityPulsate () { zenity $W $iconborg --title "borg-zenity" --progress --pulsate --auto-close --text "$1" } ZenityNotification () { zenity $iconborg --notification --text "$1" } 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") 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") 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" \ --hide-header \ ) && . "$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 TestBorgRepo ################################################################################# # 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="" MenuSupprimer1="ss\nSupprimer une archive\n" MenuSupprimerX="sss\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 # notification si espace disque faible NotificationEspaceDisque () { if [ $EspaceDisqueUtil -gt $seuil_alerte_espace_disque ];then ZenityNotification "borg-zenity\nEspace disque faible, $EspaceDisqueUtil% occupé" fi } MenuCompacter="co\nCompacter le dépôt\n" 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" MenuQuitter="qu\nQUITTER" ################################################################################# # Affichage du menu ################################################################################# if [ $menu_avance = "true" ];then NotificationEspaceDisque # menu avancé choixmenu=$(echo -e "\ $(echo $MenuCreer)\ $(echo $MenuMonter)\ $(echo $MenuDemonter)\ $(echo $MenuSupprimer1)\ $(echo $MenuSupprimerX)\ $(echo $MenuRenommer)\ $(echo $MenuCompacter)\ $(echo $MenuInfo)\ $(echo $MenuFiltre)\ $(echo $MenuBarreHorizontale)\ $(echo $MenuInfoDepot)\ $(echo $MenuAfficherProfil)\ $(echo $MenuBarreHorizontale)\ $(echo $MenuEspaceDisque)\ $(echo $MenuQuitter)\ " | zenity $iconborg --list $H $W --title "$nomsauvegarde" --hide-header --hide-column=1 --column "id" --column "choix") else NotificationEspaceDisque # menu simple choixmenu=$(echo -e "\ $(echo $MenuCreer)\ $(echo $MenuMonter)\ $(echo $MenuDemonter)\ $(echo $MenuSupprimer1)\ $(echo $MenuEspaceDisque)\ $(echo $MenuQuitter)\ " | 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-$(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 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 "Montage de l'archive..." 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 "Démontage de l'archive..." sleep 2 | ZenityPulsate "" rmdir "$ptnmontage" | ZenityPulsate "Suppression du point du montage..." fi ZenityNotification "borg-zenity\nDémontage de $ptnmontage" 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)" ZenityNotification "borg-zenity\nDé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 "Suppresion de l'archive..." 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 "Suppression de l'archive..." 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 elif [ "$choixmenu" = "is" ];then # Information archive BorgFilter if [ ! "$Borg_Archive" = "" ];then zenity $W --info --text="$(borg info "$borg_repo"::"$Borg_Archive")" | ZenityPulsate "Récupération des informations..." fi elif [ "$choixmenu" = "fi" ];then #Filtre filter=$(zenity $W --entry --entry-text "$filter") elif [ "$choixmenu" = "id" ];then # Information sur le dépôt zenity $W --info --text="$(borg info "$borg_repo")" | ZenityPulsate "Récupération des informations..." 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