borg-zenity/profile-manager.sh

148 lines
5.0 KiB
Bash
Executable File

#! /bin/bash
# Fonctions et variables
###########################################################
W="--width=600"
H="--height=500"
ZenityPulsate () {
zenity $W --window-icon=borg --progress --pulsate --auto-close
}
TestBorgRepo () {
if [ ! -f "config" ]; then
zenity $W --error --text="Impossible de trouver le fichier de configuration du dépôt."
TestBorgErr="true"
else
TestBorgErr="false"
fi
if [ ! -d "data" ]; then
zenity $W --error --text="Impossible de trouver le dossier des données du dépôt."
TestBorgErr="true"
else
TestBorgErr="false"
fi
}
dossierprofil="$HOME/.config/borg/profils-borg-zenity/"
mkdir -p "$dossierprofil" && cd "$dossierprofil" || exit
# Affichage du menu
###########################################################
choixmenu=$(echo -e "\
cp\nCréer un nouveau profil/dépôt\n\
ajd\nCréer un nouveau profil et le lier à un dépôt existant\n\
ap\nAfficher les profils\n\
mp\nModifier un profil (connaissance en ligne de commande borg requise)\
" | zenity $H $W --window-icon=borg --list --column "id" --column "choix" --hide-column=1 --hide-header)
# Gestion des choix du menu
###########################################################
### Créer un nouveau profil/dépôt ###
if [ "$choixmenu" = "cp" ];then
borg_repo=$(zenity --title "Dossier du dépôt" --file-selection --directory)
if [ ! -z "$(ls -A "$borg_repo")" ]; then
zenity $W --error --text="Erreur, le dossier n'est pas vide."
exit
fi
zenity $W --question --text "Chiffrer le dépôt ?\n\n\
Si vous chiffrez le dépôt, le fichier-clé du dépôt sera stocké dans le dossier <tt>$HOME/.config/borg/keys/</tt>.\n\
Veuillez à sauvegarder ce dossier dans un endroit sûr.\n\
Si cette clé ne peut être lue les données ne seront pas récupérables. Notez qu'il n'existe aucun mécanisme de récupération de clé.\n\n\
Si vous ne chiffrez pas le dépôt, les données qu'il contient seront librement accessible par toute personne ayant accès aux fichiers du dépôt.
"
if [ "$?" = "0" ];then
export BORG_PASSPHRASE=""
borg init --encryption=keyfile "$borg_repo" | ZenityPulsate
zenity $W --info --text "Un nouveau dépôt chiffré a été généré dans <tt>$borg_repo</tt>"
repo_status="chiffré"
elif [ "$?" = "1" ];then
borg init --encryption=none "$borg_repo" | ZenityPulsate
zenity $W --info --text "Un nouveau dépôt en clair a été généré dans <tt>$borg_repo</tt>"
repo_status="non chiffré"
fi
borg_dir=$(zenity --title "Dossier à sauvegarder" --file-selection --directory)
profilename=$(zenity --entry --title "Nom de profil" --text "Nom de profil (pas d'espace ni caractères spéciaux)")
nomsauvegarde=$(echo "$profilename")
if [ "$profilename" = "" ];then zenity $W --error --text "Pas de nom de profil";exit;fi
if [ "$nomsauvegarde" = "" ];then zenity $W --error --text "Pas de nom de sauvegarde";exit;fi
cat << EOF >> "$dossierprofil""$profilename".conf
nomsauvegarde="$nomsauvegarde"
borg_repo="$borg_repo"
borg_archive="\$(date +%d_%B_%Y_%H-%M)"
borg_dir="$borg_dir"
borg_excludes=""
borg_compress=""
dryrun=""
EOF
zenity $W --info --text "\
Le profil «$profilename» a été créé avec ces paramètres\n\n\
Nom de profil : $profilename\n\
Chemin à sauvegarder : $borg_dir\n\
Chemin du dépôt : $borg_repo\n\
Statut du dépôt : $repo_status"
### Ajouter un dépôt existant ###
elif [ "$choixmenu" = "ajd" ];then
TestBorgErr="true"
while [ "$TestBorgErr" = "true" ];do
borg_repo_test=$(zenity --title "Dossier du dépôt" --file-selection --directory)
if [ $? = "1" ];then exit;fi
cd "$borg_repo_test" && TestBorgRepo
if [ "$TestBorgErr" = "false" ];then borg_repo=$borg_repo_test;fi
done
borg_dir=$(zenity --title "Dossier à sauvegarder" --file-selection --directory)
profilename=$(zenity --entry --title "Nom de profil" --text "Nom de profil (pas d'espace ni caractères spéciaux)")
nomsauvegarde=$(echo "$profilename")
cat << EOF >> "$dossierprofil""$profilename".conf
nomsauvegarde="$nomsauvegarde"
borg_repo="$borg_repo"
borg_archive="\$(date +%d_%B_%Y_%H-%M)"
borg_dir="$borg_dir"
borg_excludes=""
borg_compress=""
dryrun=""
EOF
zenity $W --info --text "Le dépôt à bien été ajouté.\nLancez «Borg Zenity Sauvegarde» pour faire une sauvegarde."
### Afficher les profils ###
elif [ "$choixmenu" = "ap" ];then
fichierprofil=$(ls -1 *.conf | zenity $H $W --window-icon=borg --list --title "Liste des profils" --text "Choisir un profil dans la liste" --hide-header --column "profil")
if [ "$fichierprofil" != "" ];then
zenity $W $H --info --text="<tt>$(cd "$dossierprofil" && cat "$fichierprofil")</tt>"
fi
### Modifier un profil ###
elif [ "$choixmenu" = "mp" ];then
fichierprofil=$(ls -1 *.conf | zenity $H $W --window-icon=borg --list --title "Liste des profils" --text "Choisir un profil dans la liste" --hide-header --column "profil")
cp "$fichierprofil" "$fichierprofil.tmp"
return=$(zenity $W $H --text-info --editable --title="profil $fichierprofil" --filename "$fichierprofil.tmp")
if [ "$return" = "" ];then
rm "$fichierprofil.tmp"
exit
else
echo "$return" > "$fichierprofil.tmp"
mv "$fichierprofil.tmp" "$fichierprofil"
fi
elif [ "$choixmenu" = "" ];then
exit
fi