#1 Le 08/07/2007, à 19:35
- RedLemon
Script de backup auto sur dvd/cd multisession
Bon apres 3 jours de travail voici mon premier Bebe en bash !
Ce script permet de grave sur cd/dvd en multisession, d'envoye un email, etc ...
Je suis ouvert a tous commentaire et modification.
#!/bin/bash
#
#################################################################################################################
### Merci à http://linus.mccabe.nu/?/article/articleview/Bash%20scripts/1398&Linus=e8376a34908f&themex=public ###
#################################################################################################################
#
#Variables :
PATH_DIR=(/home /etc)
PATH_ARCHIVE="/var/backup"
PATH_TMP="/tmp"
MD5_TEST="true"
NAME_OF_BACKUP="Backup"
PATH_DEV_CDROM="/dev/cdrom"
CDDEV="1,0,0" # a modifier en tapant 'cdrecord -scanbus'
BRUN_TYPE="none" #(dvd,cd ou none)
DIRECTORY_OF_DVD="/media/cdrom0"
SPEED="32"
SENDMAIL="false"
TO_EMAIL="Monsieur Test <test@test.lan>"
FORM_EMAIL="server@test.lan"
NB_DAY="1" #nombre de jours ou l'on garde les fichiers crée dans PATH_ARCHIVE
#fonctions
############
function compression()
{
echo -n "Compression de '${2}' vers '${1}' : "
tar cvfpzSP $1 $2 >> /dev/null
testfile ${1}
return $?
}
function testfile()
{
if [ -e ${1} ] ; then
echo "OK"
else
echo "Erreur !!"
error
fi
return $ERR
}
function error()
{
echo "Attention arret du backup suite à une erreur !!!"
exit 0
}
#Script
echo
echo "Initialisation..."
echo
echo -n "Le reperoire '${PATH_ARCHIVE}' existe t'il ? : "
testfile ${PATH_ARCHIVE}
rm -fr ${PATH_TMP}/backup.iso
rm -fr ${PATH_TMP}/mkiso
mkdir -p ${PATH_TMP}/mkiso
echo -n "Le repertoire '${PATH_TMP}/mkiso' est il bien crée ? : "
testfile ${PATH_TMP}/mkiso
typeset -i USEDSIZE
#start
echo
echo "Démarrage..."
#Compress
echo
echo "Compression..."
echo
MAX=${#PATH_DIR[*]}-1
for ((I=0;I<=$MAX;I++))
do
echo -n "Le fichier ou repertoire '${PATH_DIR[$I]}' existe t'il ? : "
testfile ${PATH_DIR[$I]}
TEMP=$(echo ${PATH_DIR[$I]} | tr -t '/' '-')
TEMP=${TEMP:1}-$(date +%d%m%Y)-$(date +%H%M%S)
compression ${PATH_ARCHIVE}/${TEMP[*]:0}.tar.gz ${PATH_DIR[$I]}
ARCHIVE[$I]=${TEMP[*]:0}.tar.gz
done
#MD5SUM
echo
echo "Création du Md5cheksum..."
echo
MAX=${#ARCHIVE[*]}-1
if [ ${MD5_TEST} == "true" ] ; then
for ((I=0;I<=$MAX;I++)) ; do
md5sum ${PATH_ARCHIVE}/${ARCHIVE[$I]} | awk '{ print($1) }' >> "${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5"
echo -n "Md5cheksum de '${ARCHIVE[$I]}' : "
testfile "${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5"
done
fi
#creation image ISO
echo
echo "Make ISO in ${PATH_TMP}/mkiso ..."
echo
MAX=${#ARCHIVE[*]}-1
SIZE=0
TOTAL_SIZE=0
for ((I=0;I<=$MAX;I++)) ; do
cp ${PATH_ARCHIVE}/${ARCHIVE[$I]} ${PATH_TMP}/mkiso/
echo -n "Copie ${PATH_ARCHIVE}/${ARCHIVE[$I]} vers ${PATH_TMP}/mkiso : "
testfile "${PATH_ARCHIVE}/${ARCHIVE[$I]}"
SIZE=`du --block-size=1000 --summarize ${PATH_ARCHIVE}/${ARCHIVE[$I]} | sed "s/\\/.*//g"`
TOTAL_SIZE=$(($TOTAL_SIZE+$SIZE))
if [ ${MD5_TEST} == "true" ] ; then
cp ${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5 ${PATH_TMP}/mkiso/
echo -n "Copie ${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5 vers ${PATH_TMP}/mkiso : "
testfile "${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5"
SIZE=`du --block-size=1000 --summarize ${PATH_ARCHIVE}/${ARCHIVE[$I]}.md5 | sed "s/\\/.*//g"`
TOTAL_SIZE=$(($TOTAL_SIZE+$SIZE))
fi
done
echo
SIZE=${TOTAL_SIZE}
case $BRUN_TYPE in
"cd")
echo "Creation de l'image ISO temporaire ... "
umount ${DIRECTORY_OF_DVD} >> /dev/null
eject ${DIRECTORY_OF_DVD}
mount ${DIRECTORY_OF_DVD} >> /dev/null
DISKINFO=`cdrecord dev=${CDDEV} -toc 2>&1 | grep "Cannot read TOC header"`
if [ "$DISKINFO" != "" ]; then
echo "Blank cd in drive"
USEDSIZE=0
else
GETSIZE=`cdrecord dev=${CDDEV} -toc 2>&1 | grep track:lout | awk '{ print($3) }'`
if [ "$GETSIZE" = "" ]; then
echo "Pas de ${BRUN_TYPE} present dans le lecteur"
exit 0
else
if [ "$BRUN_TYPE" = "cd" ] ; then
let USEDSIZE=$GETSIZE*4*512/1024/1024
else
USEDSIZE=`du --block-size=1000 --summarize ${DIRECTORY_OF_DVD} | sed "s/\\/.*//g"`
fi
fi
fi
echo "Taille des fichiers à sauvgarder : " ${TOTAL_SIZE}
echo "Espace ocuppé sur le cd : " ${USEDSIZE}
echo "Taille restante estimée :" $((630000 - $USEDSIZE))
echo
FITS=`echo "$SIZE + $USEDSIZE < 630000" | bc -i -q `
MULTI=`echo "$SIZE + $USEDSIZE > 600000" | bc -i -q `
if [ "$FITS" = "1" ] ; then
echo "Gravure sur ${BRUN_TYPE} en cour..."
if [ "$MULTI" = "1" ] ; then
#MULTI="-fix"
echo "Finalisation d'un disque multisession aprés gravure"
DISK_FULL="1"
else
MULTI="-multi"
echo "Creation d'un disque multisession"
fi
if [ $USEDSIZE -eq 0 ] ; then
mkisofs -quiet -R -r -J -V ${NAME_OF_BACKUP} -joliet -o ${PATH_TMP}/backup.iso ${PATH_TMP}/mkiso/ >> /dev/null
else
mkisofs -quiet -R -r -J -V ${NAME_OF_BACKUP} -joliet -C `cdrecord dev=$CDDEV -msinfo` -M $CDDEV -o ${PATH_TMP}/backup.iso ${PATH_TMP}/mkiso/ >> /dev/null
fi
cdrecord speed=${SPEED} dev=${CDDEV} gracetime=2 ${MULTI} driveropts=burnfree ${PATH_TMP}/backup.iso >> /dev/null
else
echo "Pas assez d'espace disponible sur le ${BRUN_TYPE}!"
DISK_FULL="1"
fi
if [ "${MD5_TEST}" = "true" ] ; then
umount ${DIRECTORY_OF_DVD} >> /dev/null
eject ${DIRECTORY_OF_DVD}
mount ${DIRECTORY_OF_DVD} >> /dev/null
for ((I=0;I<=$MAX;I++)) ; do
echo -n "Test md5 de ${ARCHIVE[$I]} : "
MD5ORIGN=$(md5sum ${PATH_ARCHIVE}/${ARCHIVE[$I]} | awk '{ print($1) }' )
MD5BRUN=$(md5sum ${DIRECTORY_OF_DVD}/${ARCHIVE[$I]} | awk '{ print($1) }' )
if [ "$MD5BRUN" == "$MD5ORIGN" ] ; then
echo "OK"
else
echo "Erreur"
echo
echo "Erreur de gravure, remplacé le ${BRUN_TYPE} est recommencé !"
if [ "SENDMAIL" = "true" ] ; then
LOG=${PATH_TMP}/mail
exec &>$LOG
# Les entêtes
echo "To: ${TO_EMAIL}"
echo "From: Serveur $(hostname) <$(hostname)@test.lan"
echo "Subject: Erreur de type MD5 $(date +%D)"
echo ""
date
echo "Mail Automatique du server Fujimmo"
echo
echo "Suite a une erreur de gravure, il faut remplacer le ${BRUN_TYPE}"
echo
echo "Merci"
sendmail -f ${FORM_EMAIL} -ti ${TO_EMAIL} < /tmp/mail
exec -cl
fi
exit 0
fi
done
fi
;;
"dvd")
echo "Creation de l'image ISO temporaire ... "
umount ${DIRECTORY_OF_DVD} >> /dev/null
eject ${DIRECTORY_OF_DVD}
mount ${DIRECTORY_OF_DVD} >> /dev/null
DISKINFO=`cdrecord dev=${CDDEV} -toc 2>&1 | grep "Cannot read TOC header"`
if [ "$DISKINFO" != "" ]; then
echo "Blank cd in drive"
USEDSIZE=0
else
GETSIZE=`cdrecord dev=${CDDEV} -toc 2>&1 | grep track:lout | awk '{ print($3) }'`
if [ "$GETSIZE" = "" ]; then
echo "Pas de ${BRUN_TYPE} present dans le lecteur"
exit 0
else
if [ "$BRUN_TYPE" = "cd" ] ; then
let USEDSIZE=$GETSIZE*4*512/1024/1024
else
USEDSIZE=`du --block-size=1000 --summarize ${DIRECTORY_OF_DVD} | sed "s/\\/.*//g"`
fi
fi
fi
echo "Taille des fichiers à sauvgarder : " ${TOTAL_SIZE}
echo "Espace ocuppé sur le cd : " ${USEDSIZE}
echo "Taille restante estimée :" $((4230000-$USEDSIZE))
echo
FITS=`echo "$SIZE + $USEDSIZE < 4230000" | bc -i -q `
MULTI=`echo "$SIZE + $USEDSIZE > 4200000" | bc -i -q `
if [ "$FITS" = "1" ] ; then
echo "Gravure sur ${BRUN_TYPE} en cour..."
if [ "$USEDSIZE" = "0" ] ; then
MULTI="-Z"
echo "Creation d'un disque multisession"
else
MULTI="-M"
echo "Creation de la session suivante"
fi
growisofs -quiet -speed=${SPEED} ${MULTI} ${PATH_DEV_CDROM} -R -J -allow-multidot -iso-level 2 -joliet-long -l -max-iso9660-filenames -udf -r "${PATH_TMP}/mkiso/" >> /dev/null
else
echo "Pas assez d'espace disponible sur le ${BRUN_TYPE}!"
DISK_FULL="1"
fi
if [ "${MD5_TEST}" = "true" ] ; then
umount ${DIRECTORY_OF_DVD} >> /dev/null
eject ${DIRECTORY_OF_DVD}
mount ${DIRECTORY_OF_DVD} >> /dev/null
for ((I=0;I<=$MAX;I++)) ; do
echo -n "Test md5 de ${ARCHIVE[$I]} : "
MD5ORIGN=$(md5sum ${PATH_ARCHIVE}/${ARCHIVE[$I]} | awk '{ print($1) }' )
MD5BRUN=$(md5sum ${DIRECTORY_OF_DVD}/${ARCHIVE[$I]} | awk '{ print($1) }' )
if [ "$MD5BRUN" == "$MD5ORIGN" ] ; then
echo "OK"
else
echo "Erreur"
echo
echo "Erreur de gravure, remplacé le ${BRUN_TYPE} est recommencé !"
if [ "SENDMAIL" = "true" ] ; then
LOG=${PATH_TMP}/mail
exec &>$LOG
# Les entêtes
echo "To: ${TO_EMAIL}"
echo "From: Serveur $(hostname) <$(hostname)@test.lan"
echo "Subject: Erreur de type MD5 $(date +%D)"
echo ""
date
echo "Mail Automatique du server Fujimmo"
echo
echo "Suite a une erreur de gravure, il faut remplacer le ${BRUN_TYPE}"
echo
echo "Merci"
sendmail -f ${FORM_EMAIL} -ti ${TO_EMAIL} < /tmp/mail
exec -cl
fi
exit 0
fi
done
fi
;;
"none")
echo "Pas de gravure demandée..."
;;
*)
echo "Mauvais paramétre..."
;;
esac
echo
if [ "$DISK_FULL" = "1" ] ; then
umount ${DIRECTORY_OF_DVD} >> /dev/null
eject ${DIRECTORY_OF_DVD}
echo "Remplacer le ${BRUN_TYPE} et relancer le script !"
echo
if [ "SENDMAIL" = "true"] ; then
LOG=${PATH_TMP}/mail
exec &>$LOG
# Les entêtes
echo "To: ${TO_EMAIL}"
echo "From: Serveur $(hostname) <$(hostname)@fufimmo.be"
echo "Subject: Remplacé le ${BRUN_TYPE} $(date +%D)"
echo ""
date
echo "Mail Automatique du server Fujimmo"
echo
echo "Attention il faut remplacé le ${BRUN_TYPE}"
echo
echo "Merci"
sendmail -f ${FORM_EMAIL} -ti ${TO_EMAIL} < /tmp/mail
exec -cl
fi
else
umount ${DIRECTORY_OF_DVD} >> /dev/null
#mount ${DIRECTORY_OF_DVD} >> /dev/null
fi
echo "Effacement des fichiers temporaires ..."
if [ "$NB_DAY" != "0" ] ; then
find ${PATH_ARCHIVE} -type f -mtime +${NB_DAY} -exec rm -f {] \;
fi
rm -fr ${PATH_TMP}/mail
rm -fr ${PATH_TMP}/backup.iso
rm -fr ${PATH_TMP}/mkiso
echo
echo "END !"
echo
Dernière modification par RedLemon (Le 15/07/2007, à 09:27)
« Je suis probablement l'inventeur de la combinaison Ctrl-Alt-Suppr, mais c'est Bill Gates qui l'a rendue célèbre ».
« I may have invented Control-Alt-Delete, but Bill Gates made it famous »
David Bradley
Mes wikis : http://doc.ubuntu-fr.org/utilisateurs/redlemon
Hors ligne