Factorisation et ajout des modes status, fsck et open
This commit is contained in:
parent
74f1a2319a
commit
01252853ea
1 changed files with 166 additions and 46 deletions
192
backup-usb
192
backup-usb
|
@ -17,26 +17,49 @@ MAIL_FROM=root@localhost
|
|||
MAIL_TO=root@localhost
|
||||
|
||||
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 " -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 " -u Path of USB disk device"
|
||||
}
|
||||
|
||||
DEBUG=0
|
||||
SEND_MAIL=1
|
||||
STATUS_MODE=0
|
||||
CLEAN_MODE=0
|
||||
FSCK_MODE=0
|
||||
OPEN_MODE=0
|
||||
LOG=""
|
||||
DEVICE=""
|
||||
|
||||
while getopts ":dcl:u:h" opt
|
||||
while getopts ":dscfol:u:h" opt
|
||||
do
|
||||
case "$opt" in
|
||||
d)
|
||||
DEBUG=1
|
||||
;;
|
||||
s)
|
||||
STATUS_MODE=1
|
||||
SEND_MAIL=0
|
||||
;;
|
||||
c)
|
||||
CLEAN_MODE=1
|
||||
SEND_MAIL=0
|
||||
;;
|
||||
f)
|
||||
FSCK_MODE=1
|
||||
SEND_MAIL=0
|
||||
;;
|
||||
o)
|
||||
OPEN_MODE=1
|
||||
SEND_MAIL=0
|
||||
;;
|
||||
l)
|
||||
LOG="$OPTARG"
|
||||
|
@ -64,6 +87,7 @@ function is_mount () {
|
|||
}
|
||||
|
||||
function send_mail () {
|
||||
[ $SEND_MAIL -eq 0 ] && return
|
||||
echo -e "$2"|mail -s "$1" -r "$MAIL_FROM" "$MAIL_TO"
|
||||
}
|
||||
|
||||
|
@ -81,7 +105,7 @@ function debug () {
|
|||
then
|
||||
beep -f 300 -l 2000
|
||||
fi
|
||||
if [ "$3" == "send" ]
|
||||
if [ "$3" == "send" -a $SEND_MAIL -ne 0 ]
|
||||
then
|
||||
if [ -z "$LOG" ]
|
||||
then
|
||||
|
@ -94,6 +118,7 @@ function debug () {
|
|||
}
|
||||
|
||||
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_END" ] && REPORT="${REPORT}\n\n${REPORT_DF_END}"
|
||||
if [ "$1" == "error" ]
|
||||
|
@ -112,8 +137,7 @@ function disk_info() {
|
|||
echo "$VENDOR $SIZE / UUID : $UUID"|sed 's/ \+/ /g'
|
||||
}
|
||||
|
||||
function backup() {
|
||||
debug INFO "Début de l'export des sauvegardes Backup PC sur le disque USB $DEVICE ($( disk_info ))" send
|
||||
function checkAndLock() {
|
||||
if [ -f $LOCK_FILE ]
|
||||
then
|
||||
debug ERREUR "Le fichier de lock est toujours présent ($LOCK_FILE)." send
|
||||
|
@ -122,8 +146,14 @@ function backup() {
|
|||
fi
|
||||
debug INFO "Dépot du fichier de lock."
|
||||
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 ]
|
||||
then
|
||||
debug ERREUR "Le device $LUKS_DM_NAME existe deja ($LUKS_DM_PATH)." send
|
||||
|
@ -142,8 +172,9 @@ function backup() {
|
|||
else
|
||||
debug INFO "Fait."
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function mountDevice() {
|
||||
if [ `is_mount $LUKS_DM_PATH` -eq 1 ]
|
||||
then
|
||||
debug ERREUR "Le disque est déjà monté" send
|
||||
|
@ -160,6 +191,99 @@ function backup() {
|
|||
debug HALT
|
||||
exit 1
|
||||
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 "Montage de BackupPC FS..."
|
||||
|
@ -224,39 +348,41 @@ function backup() {
|
|||
debug INFO "Fait."
|
||||
fi
|
||||
|
||||
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
|
||||
closeDevice
|
||||
|
||||
debug INFO "Suppression du fichier de lock"
|
||||
rm -f $LOCK_FILE
|
||||
unlock
|
||||
|
||||
debug INFO "Fin de l'export."
|
||||
send_report
|
||||
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
|
||||
if [ "$CLEAN_MODE" == "1" ]
|
||||
# Manage status/clean mode
|
||||
if [ "$STATUS_MODE" == "1" ]
|
||||
then
|
||||
clean
|
||||
statusMode
|
||||
exit 0
|
||||
elif [ "$CLEAN_MODE" == "1" ]
|
||||
then
|
||||
cleanMode
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -z "$DEVICE" ]
|
||||
then
|
||||
debug FATAL "Il faut specifié le disque USB avec le paramètre -u !"
|
||||
usage
|
||||
exit 1
|
||||
elif [ "$FSCK_MODE" == "1" ]
|
||||
then
|
||||
fsckMode
|
||||
exit $?
|
||||
elif [ "$OPEN_MODE" == "1" ]
|
||||
then
|
||||
openMode
|
||||
exit $?
|
||||
else
|
||||
# Else : we are in backup mode
|
||||
|
||||
if [ -n "$LOG" ]
|
||||
|
@ -273,11 +399,5 @@ then
|
|||
exec 2>&1
|
||||
fi
|
||||
|
||||
if [ -z "$DEVICE" ]
|
||||
then
|
||||
debug FATAL "Il faut specifié le disque USB avec le paramètre -u !"
|
||||
usage
|
||||
exit 1
|
||||
exportBackupMode
|
||||
fi
|
||||
|
||||
backup
|
||||
|
|
Loading…
Reference in a new issue