Script Bash : Rsync avec Zenity
Voici un script bash de sauvegarde de données avec rsync, et zenity...
(c'est un exemple parmi tant d'autres ;-) ...)
#!/bin/bash
IP="***.***.***.***"
LOGIN="***"
MODULE="***"
RSYNC_ERROR=/tmp/rsync_errors.log
RSYNC_LOG=/tmp/rsync.log
RSYNC_OPTIONS="-acRHKz --delete --delete-during --delete-excluded --ignore-errors --force --progress --stats"
LOCAL[1]="/media/SONY4/"
LOCAL[2]="/media/CORSAIR/"
LOCAL[3]="/media/ADATA/"
LOCAL[4]="/home/***/Documents/"
MSSG[1]="La clé USB SONY4"
MSSG[2]="La clé USB CORSAIR"
MSSG[3]="La clé usb ADATA"
MSSG[4]="Mon répertoire personnel"
NAME[1]="SONY"
NAME[2]="CORSAIR"
NAME[3]="ADATA"
NAME[4]="HOME"
count=${#LOCAL[@]}
i=1
nb=0
### Fin de declaration des variables necessaires...
function afficheTaille() {
taille=$(sed '/\./! s/^.*$/&.00/' <<< $1)
i=1
while [[ ${#taille} -ge 7 ]]; do
taille=$(bc <<< "scale=2;$taille/1024")
((i+=1))
done
case $i in
1) taille+=" octets" ;;
2) taille+=" Ko" ;;
3) taille+=" Mo" ;;
4) taille+=" Go" ;;
esac
echo $taille
}
function display_bad_choice() {
zenity --error --title="Choix incorrect !" \
--text="<big><span color=\"red\">Ce choix n'est pas valide !</span>\n Boo ... :-(</big>"
display_goodbye;
}
function display_bad_mount() {
zenity --error --title="Erreur de montage" \
--text="<big>${MSSG[$choice]} (${NAME[$choice]}) n'est pas montée !</big>\n<b>Au revoir :(</b>"
}
function display_goodbye() {
zenity --notification --title="Au Revoir !" --timeout=7 \
--text="*** Au revoir ! ***"
exit;
}
function display_progress() {
fichier=""
nbFile=
while read uneLigne
do
[ "${uneLigne}" ] && {
[[ "${uneLigne}" =~ [[:digit:]\.]+[A-Z]\ *[[:digit:]]+% ]] && {
infos=( ${uneLigne} )
transfere=${infos[0]}
evolution=${infos[1]%\%}
indice=2
[[ "${transfere}" = *100% ]] &&
{
transfere=${transfere%100%}
evolution=100
indice=1
}
debit=${infos[${indice}]}
tps=${infos[((${indice}+1))]}
[[ "${uneLigne}" = *to-check* ]] && {
indice=$((${#infos[@]}-1))
nbFile="${infos[${indice}]}"
nbFile=${nbFile%)}
nbFile=${nbFile#*=}
((indice--))
numFichier="${infos[${indice}]}"
numFichier=${numFichier#*#}
numFichier=${numFichier%,}
}
echo "#Fichier : ${fichier}\nTransféré : ${transfere}\nDébit : ${debit}\nTemps restant : ${tps}$([ "${nbFile}" ] && echo "\n\nNb fich. transféré(s) : ${numFichier}\nNb fich. restant(s) : ${nbFile}")"
echo ${evolution}
} || {
{
[ -f "${uneLigne}" ] && {
fichier="${uneLigne}"
echo "Fichier : ${fichier}"
}
} || {
[[ "${uneLigne}" = sent*received*/* ]] && {
infos=( ${uneLigne} )
echo 100
echo "#Fin du transfert : ${infos[1]} ${infos[2]} transférés\nDébit moyen : ${infos[6]}"
}
}
}
}
done > >(zenity --progress --width=580 --title="Début de la sauvegarde" --text="Initialisation de la sauvegarde..." --percentage=0 --auto-close --auto-kill)
}
function display_save() {
zenity --question --text="Es-tu sûr de vouloir sauvegarder <b>${NAME[$choice]}</b> ?!"
if [ $? -eq 1 ]; then display_goodbye; fi
}
function display_save_running() {
zenity --progress --percentage=0 --auto-close --auto-kill \
--title="Synchronisation Rsync en cours" \
--text="La sauvegarde de <b>${NAME[$choice]}</b> est en cours.\nPatiente... ;-)"
}
function display_save_ok() {
zenity --notification --title="Sauvegarde terminée" --timeout=7 \
--text="*** La sauvegarde <b>${NAME[$choice]}</b> est terminée ! :D"
}
function display_srvr_boo() {
zenity --error --text="<span color='red'>Le serveur de sauvegarde n'est pas joignable !</span>\nLa sauvegarde ne peut avoir lieu :( \n <b>Avertir le Responsable Informatique !!!</b>" --title="Erreur de sauvegarde !" --width=250
}
function display_sync_ending() {
DEBUT=$1
FIN=$(date +%s)
TEMPS=$(($FIN-$DEBUT))
TP_HEU=$(sed 's/^.$/0&/' <<< $(($TEMPS/3600)))
TP_TMP=$(($TEMPS%3600))
TP_MIN=$(sed 's/^.$/0&/' <<< $(($TP_TMP/60)))
TP_SEC=$(sed 's/^.$/0&/' <<< $(($TP_TMP%60)))
TP=$(echo "$TP_HEU:$TP_MIN:$TP_SEC")
data=( $(awk '/^Number of files transferred/ {print $5}' $RSYNC_LOG) )
NB_FICH=${data[0]}
data=( $(awk '/^sent/ {print $2,$5,$7}' $RSYNC_LOG) )
ENVOI=$(afficheTaille ${data[0]})
RECU=$(afficheTaille ${data[1]})
VITESSE=$(afficheTaille ${data[2]})
zenity --info --title="Sauvergarde terminée correctement" --text="Sauvegarde du répertoire\n<b>${NAME[$choice]}</b>\n<span color=\"green\">Effectuée avec succès !</span>\n$NB_FICH fichiers synchronisés\n\nEnvoyé:\t$ENVOI\nReçu:\t$RECU\nTemps:\t$TP\nTransfert:\t$VITESSE/sec"
}
function display_sync_now() {
zenity --info --title="Début de la synchronisation" --timeout=7 \
--text=" La synchronisation va commencer ! "
}
function display_welcome() {
choice=$( echos_welcome | zenity --list --title="Sauvegarde" \
--text="Veuillez indiquer le média de sauvegarde :" \
--column="Choix" --column="Nom" --column="Description" );
if [ $? -eq 1 ]; then display_goodbye; fi
}
function echos_welcome() {
while [ "$i" -le "$count" ]; do
echo "$i";
echo "${NAME[$i]}";
echo "${MSSG[$i]}";
let "i = $i + 1";
done
unset i;
}
function synchronizer() {
CP_LOCAL="${LOCAL[$choice]}"
DEBUT=$(date +%s)
display_sync_now;
rsync $RSYNC_OPTIONS $CP_LOCAL $LOGIN@$IP::$MODULE 2>> $RSYNC_ERROR | tee $RSYNC_LOG > >(awk ' BEGIN { RS="\n|\r" } { print; fflush() }' > >(display_progress))
if [ -s $RSYNC_ERROR ]; then
display_srvr_boo;
cat $RSYNC_ERROR | zenity --text-info --width=400 --title="Problème de synchronisation"
else
display_sync_ending $DEBUT;
fi
if [ -e $RSYNC_ERROR ]; then rm $RSYNC_ERROR; fi
if [ -e $RSYNC_LOG ]; then rm $RSYNC_LOG; fi
}
function test_choice() {
case $choice in
1|2|3)
test_mount;
display_save;
;;
4) display_save ;;
*) display_bad_choice ;;
esac
}
function test_mount() {
if [ ! -d ${LOCAL[$choice]} ]; then
display_bad_mount;
#sleep 3;
exit;
fi
}
display_welcome;
test_choice;
synchronizer;
Vous pouvez obtenir cet exemple !
Adaptez-le, à vos besoins ; exécutez-le avec vos droits utilisateurs ...
<<| Page : Bash : script : rsync_zenity : |