Myze Posted July 9, 2018 (edited) Bonjour, Je sollicite votre aide pour la mise en place d'un script en crontab, avec ma commande rsync (qui fonctionne). Je souhaiterais ajouter l'envoi d'un mail quand la synchronisation est fini avec le résultat voir l'affichage complet de celle ci. Pour information j'ai commencé un .sh: #!/bin/bash rsync -avzruh -delete-after --progress --no-owner --no-group /var/www/a_backuper -e "ssh -p 2018" user@adresse_ip:/mnt/backup/backup_srvxxx Maintenant je ne sais pas trop quoi faire pour recevoir un mail avec le résultat de la commande. Merci d'avance pour l'aide, Edited July 11, 2018 by Myze Share this post Link to post Share on other sites
refuznik Posted July 9, 2018 (edited) Rapidement si tu rajoutes : if [ $? -eq 0 ] then /usr/lib/sendmail -v "rsync backup termoiné" tonmail@xxxx.com<<EOF rsync backup Terminé @ $(date) for $(hostname) EOF else /usr/lib/sendmail -v tonmail@xxx.com<<EOF "rsync backup loupé" @ $(date) for $(hostname) EOF fi Edited July 9, 2018 by refuznik Share this post Link to post Share on other sites
Myze Posted July 10, 2018 Merci du retour, cela donnerai donc: #!/bin/bash rsync -avzruh -delete-after --progress --no-owner --no-group /var/www/a_backuper -e "ssh -p 2018" user@adresse_ip:/mnt/backup/backup_srvxxx if [ $? -eq 0 ] then /usr/lib/sendmail -v "rsync backup terminé" tonmail@mail.com<<EOF rsync backup Terminé @ $(date) for $(hostname) EOF else /usr/lib/sendmail -v tonmail@mail.com<<EOF "rsync backup loupé" @ $(date) for $(hostname) EOF fi Share this post Link to post Share on other sites
L33thium Posted July 10, 2018 pour info $? est le code de sortie de la commande précédente et si elle est > 0 c'est qu'il y a eu une erreur, elle quitte en échec. ce script vaut rsync blabla && echo "réussite" || echo "échec" Version plus simple à mon goût, incluant le log : #!/bin/bash $logfile = $(mktemp /tmp/rsync.XXXXXXXX.log) rsync -avzruh -delete-after --progress --no-owner --no-group /var/www/a_backuper -e "ssh -p 2018" user@adresse_ip:/mnt/backup/backup_srvxxx >$logfile 2>&1 if [ $? -eq 0 ]; then mail -s "rsync backup terminé @ $(date) for $(hostname)" mail@example.com < $logfile else mail -s "rsync backup erreur @ $(date) for $(hostname)" mail@example.com < $logfile fi rm -f $logfile exit 0 Share this post Link to post Share on other sites
Myze Posted July 10, 2018 Merci, j'ai un retour en erreur au lancement du script: user@srvxxx:~# bash /home/jeanmi/scripts/backup_rsync.sh /home/jeanmi/scripts/backup_rsync.sh: ligne 2: = : commande introuvable /home/jeanmi/scripts/backup_rsync.sh: ligne 4: $logfile : redirection ambiguë /home/jeanmi/scripts/backup_rsync.sh: ligne 9: $logfile : redirection ambiguë Share this post Link to post Share on other sites
CryoGen Posted July 10, 2018 Je pense qu'il te manque la commande "mktemp", regarde comment l'ajouter à ta distribution. Share this post Link to post Share on other sites
Myze Posted July 10, 2018 (edited) Pourtant elle existe bien, ~# mktemp --version mktemp (GNU coreutils) 8.25 Copyright © 2016 Free Software Foundation, Inc. License GPLv3+ : GNU GPL version 3 ou ultérieure <http://gnu.org/licenses/gpl.html> C'est logiciel libre, vous êtes libre de le modifier et de le redistribuer. Ce logiciel n'est accompagné d'ABSOLUMENT AUCUNE GARANTIE, dans les limites autorisées par la loi applicable. Écrit par Jim Meyering et Eric Blake. Après test, j'ai supprimé l'espace entre le "=" et le "$" maintenant j'ai ce message la: :~# bash /home/user/scripts/backup_rsync.sh /home/jeanmi/scripts/backup_rsync.sh: ligne 2: =/tmp/rsync.ljMv2omi.log: Aucun fichier ou dossier de ce type /home/jeanmi/scripts/backup_rsync.sh: ligne 4: $logfile : redirection ambiguë /home/jeanmi/scripts/backup_rsync.sh: ligne 9: $logfile : redirection ambiguë Edited July 10, 2018 by Myze Share this post Link to post Share on other sites
RaphAstronome Posted July 10, 2018 Remplace $logfile = $(mktemp /tmp/rsync.XXXXXXXX.log) par logfile=$(mktemp /tmp/rsync.XXXXXXXX.log) Pour assigner une variable il ne faut pas de $, le $ c'est lorsque l'on veut l'utiliser. Share this post Link to post Share on other sites
L33thium Posted July 11, 2018 oups déformation python ^^ Share this post Link to post Share on other sites
Myze Posted July 11, 2018 Super c'est ok. Merci à tous pour votre coup de main Pour info le script définitif est: #!/bin/bash logfile=$(mktemp /tmp/rsync.XXXXXXXX.log) rsync -avzruh -delete-after --progress --no-owner --no-group /var/www/a_backuper -e "ssh -p 2018" user@adresse_ip:/mnt/backup/backup_srvxxx >$logfile 2>&1 if [ $? -eq 0 ]; then mail -s "rsync backup terminé @ $(date) for $(hostname)" mail@example.com < $logfile else mail -s "rsync backup erreur @ $(date) for $(hostname)" mail@example.com < $logfile fi rm -f $logfile exit 0 Share this post Link to post Share on other sites
L33thium Posted July 11, 2018 exit 0 force le script a sortir sans code d'erreur quoi qu'il se passe afin d'éviter de perturber cron même si ça lui poserait normalement aucun problème, c'est une mesure de précaution. Share this post Link to post Share on other sites
Myze Posted July 13, 2018 Ok merci de la précision. Share this post Link to post Share on other sites
Krapace Posted August 14, 2018 Je te conseille de rajouter un test pour t'assurer qu'un rsync ne tourne pas déjà, sinon tu va surcharger le lien réseau et le/les disque/s Share this post Link to post Share on other sites
L33thium Posted August 15, 2018 en effet, pas compliqué (version un poil bourrinne) : pidof rsync >/dev/null 2>&1 || rsync -avzruh ... Share this post Link to post Share on other sites
Krapace Posted August 15, 2018 ou if [ -z "$(pgrep rsync)" ] then rsync -rtluvp --exclude=\*.part user@server:/home/user/ /home/user/ else echo -e "Synchro en cours" exit 0 fi Share this post Link to post Share on other sites
L33thium Posted August 15, 2018 mais avec tous ces contrôles on est presque à la limite d'écrire un daemon là ^^ Share this post Link to post Share on other sites
Krapace Posted August 15, 2018 Ben c'est un peu la base du scripting, il faut poser des controles. Tu a vite fait de faire de la merde et de freezer ta machine ou la ralentir avec un script qui fait une boucle. J'ai un script de ce genre qui tourner a 1h et cron m'envoie le résultat par mail quand c'est finit :) Il y a des fois ou mettre ce contrôle m'a bien servi puisque la synchro a durée 43h xD Share this post Link to post Share on other sites
CryoGen Posted August 16, 2018 @L33thium bah si tu fais un script que tu lances toi même de temps à autre tu peux le faire simple. Mais un script automatique qui s’exécute sans surveillance, tu as intérêt à blinder un peu Share this post Link to post Share on other sites