Factorisation et ajout des modes status, fsck et open

This commit is contained in:
Benjamin Renard 2017-04-21 10:50:49 +02:00
parent 74f1a2319a
commit 01252853ea

View file

@ -17,26 +17,49 @@ MAIL_FROM=root@localhost
MAIL_TO=root@localhost MAIL_TO=root@localhost
function usage() { function usage() {
echo "Usage : backup-usb [-d] [-c] [-l /path/to/log.file] [-u /dev/sdX]" echo "Usage : backup-usb [-d] [-s|-c|-f|-o] [-l /path/to/log.file] [-u /dev/sdX]"
echo " -d Debug mode" echo " -d Debug mode"
echo " -c Clean mode" echo " -d Status mode : print status mode"
echo " -c Clean status mode : try to :"
echo " - umount device and BackupPC-FS"
echo " - close LUKS device"
echo " - remove lock file"
echo " -f FSCK mode : Run FSCK on device"
echo " -o Open mode : lock, open and mount device"
echo " -l Log file path" echo " -l Log file path"
echo " -u Path of USB disk device" echo " -u Path of USB disk device"
} }
DEBUG=0 DEBUG=0
SEND_MAIL=1
STATUS_MODE=0
CLEAN_MODE=0 CLEAN_MODE=0
FSCK_MODE=0
OPEN_MODE=0
LOG="" LOG=""
DEVICE="" DEVICE=""
while getopts ":dcl:u:h" opt while getopts ":dscfol:u:h" opt
do do
case "$opt" in case "$opt" in
d) d)
DEBUG=1 DEBUG=1
;; ;;
s)
STATUS_MODE=1
SEND_MAIL=0
;;
c) c)
CLEAN_MODE=1 CLEAN_MODE=1
SEND_MAIL=0
;;
f)
FSCK_MODE=1
SEND_MAIL=0
;;
o)
OPEN_MODE=1
SEND_MAIL=0
;; ;;
l) l)
LOG="$OPTARG" LOG="$OPTARG"
@ -64,6 +87,7 @@ function is_mount () {
} }
function send_mail () { function send_mail () {
[ $SEND_MAIL -eq 0 ] && return
echo -e "$2"|mail -s "$1" -r "$MAIL_FROM" "$MAIL_TO" echo -e "$2"|mail -s "$1" -r "$MAIL_FROM" "$MAIL_TO"
} }
@ -81,7 +105,7 @@ function debug () {
then then
beep -f 300 -l 2000 beep -f 300 -l 2000
fi fi
if [ "$3" == "send" ] if [ "$3" == "send" -a $SEND_MAIL -ne 0 ]
then then
if [ -z "$LOG" ] if [ -z "$LOG" ]
then then
@ -94,6 +118,7 @@ function debug () {
} }
function send_report () { function send_report () {
[ $DEBUG -eq 1 -o $SEND_MAIL -eq 0 ] && return 0
[ -n "$REPORT_DF_START" ] && REPORT="${REPORT}\n\n${REPORT_DF_START}" [ -n "$REPORT_DF_START" ] && REPORT="${REPORT}\n\n${REPORT_DF_START}"
[ -n "$REPORT_DF_END" ] && REPORT="${REPORT}\n\n${REPORT_DF_END}" [ -n "$REPORT_DF_END" ] && REPORT="${REPORT}\n\n${REPORT_DF_END}"
if [ "$1" == "error" ] if [ "$1" == "error" ]
@ -112,8 +137,7 @@ function disk_info() {
echo "$VENDOR $SIZE / UUID : $UUID"|sed 's/ \+/ /g' echo "$VENDOR $SIZE / UUID : $UUID"|sed 's/ \+/ /g'
} }
function backup() { function checkAndLock() {
debug INFO "Début de l'export des sauvegardes Backup PC sur le disque USB $DEVICE ($( disk_info ))" send
if [ -f $LOCK_FILE ] if [ -f $LOCK_FILE ]
then then
debug ERREUR "Le fichier de lock est toujours présent ($LOCK_FILE)." send debug ERREUR "Le fichier de lock est toujours présent ($LOCK_FILE)." send
@ -122,8 +146,14 @@ function backup() {
fi fi
debug INFO "Dépot du fichier de lock." debug INFO "Dépot du fichier de lock."
echo $$ > $LOCK_FILE echo $$ > $LOCK_FILE
beep -f 1000 -r 2 -l 100 }
function unlock() {
debug INFO "Suppression du fichier de lock"
rm -f $LOCK_FILE
}
function openDevice() {
if [ -e $LUKS_DM_PATH ] if [ -e $LUKS_DM_PATH ]
then then
debug ERREUR "Le device $LUKS_DM_NAME existe deja ($LUKS_DM_PATH)." send debug ERREUR "Le device $LUKS_DM_NAME existe deja ($LUKS_DM_PATH)." send
@ -142,8 +172,9 @@ function backup() {
else else
debug INFO "Fait." debug INFO "Fait."
fi fi
}
function mountDevice() {
if [ `is_mount $LUKS_DM_PATH` -eq 1 ] if [ `is_mount $LUKS_DM_PATH` -eq 1 ]
then then
debug ERREUR "Le disque est déjà monté" send debug ERREUR "Le disque est déjà monté" send
@ -160,6 +191,99 @@ function backup() {
debug HALT debug HALT
exit 1 exit 1
fi fi
}
function closeDevice() {
debug INFO "Fermeture du disque LUKS..."
cryptsetup close $LUKS_DM_NAME
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "Problème durant la fermeture du disque LUKS" send
debug HALT
send_report error
exit 1
fi
}
function statusMode () {
if [ -f $LOCK_FILE ]
then
debug INFO "Fichier de lock present"
if [ $( ps xa|grep rsync|grep -c "$BACKUPPCFS_MNT" ) -gt 0 ]
then
debug INFO "Un export de sauvegarde est en cours (commande rsync détectée)"
else
debug ERREUR "Aucun export de sauvegarde détecté (pas de commande rsync détectée).\n\nSi le problème predure depuis plusieurs limite, lancer la commande '$0 -c'"
fi
else
debug INFO "Fichier de lock absent"
fi
if [ -e $LUKS_DM_PATH ]
then
debug INFO "Disque LUKS ouvert"
if [ `is_mount $LUKS_DM_PATH` -eq 1 ]
then
debug INFO "Disque LUKS monté"
df -h $LUKS_DM_PATH
else
debug INFO "Disque LUKS non-monté"
fi
else
debug INFO "Disque LUKS non-ouvert"
fi
if [ `is_mount $BACKUPPCFS_MNT` -eq 1 ]
then
debug INFO "BackupPC-FS monté"
else
debug INFO "BackupPC-FS non-monté"
fi
}
function cleanMode () {
umount $MNT_DIR
cryptsetup close $LUKS_DM_NAME
umount $BACKUPPCFS_MNT
rm -f $LOCK_FILE
}
function fsckMode() {
debug INFO "Début du test FSCK du disque USB $DEVICE ($( disk_info ))"
checkAndLock
beep -f 1000 -r 2 -l 100
openDevice
fsck $LUKS_DM_PATH
RET=$?
if [ $RET -eq 0 ]
then
closeDevice
unlock
fi
exit $RET
}
function openMode() {
debug INFO "Ouverture et montage du disque USB $DEVICE ($( disk_info ))"
checkAndLock
beep -f 1000 -r 2 -l 100
openDevice
mountDevice
exit 0
}
function exportBackupMode() {
debug INFO "Début de l'export des sauvegardes Backup PC sur le disque USB $DEVICE ($( disk_info ))" send
checkAndLock
beep -f 1000 -r 2 -l 100
openDevice
mountDevice
debug INFO "Disque monté. Espace sur le disque USB avant l'export :\n\n$( df -hP $MNT_DIR )\n" debug INFO "Disque monté. Espace sur le disque USB avant l'export :\n\n$( df -hP $MNT_DIR )\n"
debug INFO "Montage de BackupPC FS..." debug INFO "Montage de BackupPC FS..."
@ -224,53 +348,25 @@ function backup() {
debug INFO "Fait." debug INFO "Fait."
fi fi
debug INFO "Fermeture du disque LUKS..." closeDevice
cryptsetup close $LUKS_DM_NAME
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "Problème durant la fermeture du disque LUKS" send
debug HALT
send_report error
exit 1
fi
debug INFO "Suppression du fichier de lock" unlock
rm -f $LOCK_FILE
debug INFO "Fin de l'export." debug INFO "Fin de l'export."
send_report send_report
beep -f 1000 -l 2000 beep -f 1000 -l 2000
} }
function clean () {
umount $MNT_DIR
cryptsetup close $LUKS_DM_NAME
umount $BACKUPPCFS_MNT
rm -f $LOCK_FILE
}
# Manage clean mode # Manage status/clean mode
if [ "$CLEAN_MODE" == "1" ] if [ "$STATUS_MODE" == "1" ]
then then
clean statusMode
exit 0 exit 0
fi elif [ "$CLEAN_MODE" == "1" ]
# Else : we are in backup mode
if [ -n "$LOG" ]
then then
# Close STDOUT file descriptor cleanMode
exec 1<&- exit 0
# Close STDERR FD
exec 2<&-
# Open STDOUT as $LOG file for read and write.
exec 1<>$LOG
# Redirect STDERR to STDOUT
exec 2>&1
fi fi
if [ -z "$DEVICE" ] if [ -z "$DEVICE" ]
@ -278,6 +374,30 @@ then
debug FATAL "Il faut specifié le disque USB avec le paramètre -u !" debug FATAL "Il faut specifié le disque USB avec le paramètre -u !"
usage usage
exit 1 exit 1
fi elif [ "$FSCK_MODE" == "1" ]
then
fsckMode
exit $?
elif [ "$OPEN_MODE" == "1" ]
then
openMode
exit $?
else
# Else : we are in backup mode
backup if [ -n "$LOG" ]
then
# Close STDOUT file descriptor
exec 1<&-
# Close STDERR FD
exec 2<&-
# Open STDOUT as $LOG file for read and write.
exec 1<>$LOG
# Redirect STDERR to STDOUT
exec 2>&1
fi
exportBackupMode
fi