backuppcExportOnUsbDisk/backup-usb

256 lines
4.7 KiB
Text
Raw Normal View History

2017-02-28 15:13:49 +01:00
#!/bin/bash
# Path to backuppcfs script
BACKUPPCFS_PATH="/usr/local/sbin/backuppcfs"
BACKUPPCFS_MNT="/mnt/backuppcfs"
MNT_DIR=/mnt/usb
LUKS_DM_NAME=bkp-usb
LUKS_DM_PATH=/dev/mapper/$LUKS_DM_NAME
LUKS_KEY_FILE="/root/.luks.key"
# LOCK_FILE
LOCK_FILE=/var/lock/bkp-disk.lock
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 " -d Debug mode"
echo " -c Clean mode"
echo " -l Log file path"
echo " -u Path of USB disk device"
}
DEBUG=0
CLEAN_MODE=0
LOG=""
DEVICE=""
while getopts ":dcl:u:h" opt
do
case "$opt" in
d)
DEBUG=1
;;
c)
CLEAN_MODE=1
;;
l)
LOG="$OPTARG"
;;
u)
DEVICE="$OPTARG"
;;
h)
usage
exit 0
;;
*)
echo "Invalid '$opt' parameter"
echo
usage
exit 1
esac
done
#
# Functions
#
function is_mount () {
mount | grep -c "$1 "
}
function send_mail () {
echo "$2"|mail -s "$1" -r "$MAIL_FROM" "$MAIL_TO"
}
function debug () {
d=`date "+%Y-%m-%d - %Hh%Mm%Ss"`
if [ -z "$LOG" ]
then
echo "$d -- [$1] $2"
else
echo "$d -- [$1] $2" >> $LOG 2>&1
fi
if [ "$1" == "HALT" ]
then
beep -f 300 -l 2000
fi
if [ "$3" == "send" ]
then
if [ -z "$LOG" ]
then
echo "$d -- [INFO] Send mail"
else
echo "$d -- [INFO] Send mail" >> $LOG 2>&1
fi
send_mail "[$1] Export sauvegardes Backup PC" "$2"
fi
}
function backup() {
debug INFO Start send
if [ -f $LOCK_FILE ]
then
debug ERREUR "Le fichier de lock est toujours présent ($LOCK_FILE)." send
debug HALT
exit 1
fi
debug INFO "Dépot du fichier de lock."
echo $$ > $LOCK_FILE
beep -f 1000 -r 2 -l 100
if [ -e $LUKS_DM_PATH ]
then
debug ERREUR "Le device $LUKS_DM_NAME existe deja ($LUKS_DM_PATH)." send
debug HALT
exit 1
fi
debug INFO "Ouverture du disque LUKS ..."
cryptsetup luksOpen $DEVICE $LUKS_DM_NAME --key-file $LUKS_KEY_FILE
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "La commande 'cryptsetup luksOpen' n'a pas retourne 0. Code retour : $RES" send
debug HALT
exit $RES
else
debug INFO "Fait."
fi
if [ `is_mount $LUKS_DM_PATH` -eq 1 ]
then
debug ERREUR "Le disque est déjà monté" send
debug HALT
exit 1
fi
debug INFO "Montage du disque."
mount $LUKS_DM_PATH $MNT_DIR
if [ `is_mount $LUKS_DM_PATH` -eq 0 ]
then
debug ERREUR "Problème durant le montage du disque dur." send
debug HALT
exit 1
fi
debug INFO "Disque monté."
debug INFO "Montage de BackupPC FS..."
$BACKUPPCFS_PATH $BACKUPPCFS_MNT
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "Problème durant le montage de BackupPC FS. Code retour : $RES"
umount $BACKUPPCFS_MNT
umount $LUKS_DM_PATH
cryptsetup close $LUKS_DM_NAME
debug HALT
exit 1
else
debug INFO "Fait."
fi
sleep 2
debug INFO "Export des sauvegardes ..."
for dir in $BACKUPPCFS_MNT/*
do
[ ! -d "$dir" -o ! -d "$dir/latest/" ] && continue
host="$( basename "$dir")"
debug INFO "Export de la dernière sauvegarde du serveur $host ..."
rsync -a --delete --numeric-ids "$dir/latest/" --exclude='/dev/*' "$MNT_DIR/$host/"
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "Problème durant l'export de la sauvegarde du serveur $host (Code retour : $RES)" send
else
debug INFO "Fait."
fi
done
debug INFO "Export des sauvegardes terminé"
debug INFO "Démontage de BackupPC FS..."
umount $BACKUPPCFS_MNT
RES=$?
if [ $RES -ne 0 ]
then
debug ERREUR "Problème durant le démontage de BackupPC FS (Code retour : $RES)" send
else
debug INFO "Fait."
fi
debug INFO "Démontage du disque"
umount $LUKS_DM_PATH
if [ `is_mount $LUKS_DM_PATH` -eq 1 ]
then
debug ERREUR "Problème durant le démontage du disque dur." send
debug HALT
exit 1
else
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
exit 1
fi
debug INFO "Suppression du fichier de lock"
rm -f $LOCK_FILE
debug INFO "Stop" send
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" ]
then
clean
exit 0
fi
# Else : we are in backup mode
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
if [ -z "$DEVICE" ]
then
debug FATAL "Il faut specifié le disque USB avec le paramètre -u !"
usage
exit 1
fi
backup