script s-backup backup.sh trap

This commit is contained in:
root 2023-01-24 10:49:32 +01:00
parent c2eb2b85a4
commit abfe277180

View File

@ -1,27 +1,51 @@
#!/bin/bash
BDIR=/home/backup
SWIN=/tmp/s-win
LOCK=/tmp/s-backup.lock
#Fonction cleanup pour sortir propre dans tout les cas
cleanup()
{
rm "${LOCK}"
umount "${SWIN}"
echo "nettoyage effectue, sortie tout propre ..."
exit 3
}
#check si pas deja en cours d execution > sortie si fichier de lock existe
if [ -e "${LOCK}" ] ; then
echo "$0 : Verrouillage, deja en cours d execution"
trap cleanup 1 2 3 6
fi
#prepartion des dossiers qui vont accueillir les donnees à sauvegarder
[ -d "${BDIR}" ] || mkdir "${BDIR}"
[ -d "${BDIR}" ] || mkdir "${BDIR}/s-win"
[ -d "${BDIR}/s-win" ] || mkdir "${BDIR}/s-win"
[ -d "${SWIN}" ] || mkdir "${SWIN}"
mount -t cifs -o ro,vers=3.0,username=u-backup,password=Azerty1+ //s-win/commun "${SWIN}"
#etablissement du lock
touch "${LOCK}"
mount -t cifs -o ro,vers=3.0,username=uBackup,password=Azerty1+ //s-win/commun "${SWIN}"
if [ $? != 0 ] ; then
echo "$0 : erreur montage ${SWIN}"
exit 1
rm "${LOCK}"
trap cleanup 1 2 3 6
fi
rsync -av "${SWIN}/" "${BDIR}/s-win/commun"
umount "${SWIN}"
mount -t cifs -o ro,vers=3.0,username=u-backup,password=Azerty1+ //s-win/public "${SWIN}"
mount -t cifs -o ro,vers=3.0,username=uBackup,password=Azerty1+ //s-win/public "${SWIN}"
if [ $? != 0 ] ; then
echo "$0 : erreur montage"
exit 2
echo "$0 : erreur montage ${SWIN}"
trap cleanup 1 2 3 6
fi
rsync -av "${SWIN}/" "${BDIR}/s-win/public"
umount "${SWIN}"
#libere le verrou
rm "${LOCK}"
exit 0