259 lines
6.5 KiB
Bash
Executable File
259 lines
6.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
cd $SCRIPT_DIR
|
|
|
|
. ./backup.config
|
|
|
|
function log() {
|
|
echo -e "`date +'%d/%m/%Y %H:%M:%S'` | $1"
|
|
echo -e "`date +'%d/%m/%Y %H:%M:%S'` | $1" >> $LOG
|
|
}
|
|
|
|
function usage() {
|
|
echo
|
|
echo -e "./backup.sh \e[93m--help \e[39m"
|
|
echo -e "./backup.sh \e[93m--mount \e[39m"
|
|
echo -e "./backup.sh \e[93m--umount \e[39m"
|
|
echo -e "./backup.sh \e[92m--src=\e[39m/mnt/data \e[96m--dst=\e[39m/backup"
|
|
echo -e "./backup.sh \e[92m--src=\e[39m/mnt/vmachines/storage \e[96m--dst=\e[39m/backup/data/VMachines"
|
|
echo -e "./backup.sh \e[92m--src=\e[39m/mnt/projects \e[96m--dst=\e[39m/backup \e[91m--delete=\e[39myes"
|
|
echo
|
|
echo -e " $1"
|
|
echo
|
|
exit
|
|
}
|
|
|
|
function get_luks_password() {
|
|
LUKS_PASS=$(curl -s $LUKS_PASS_URL | jq -r ".password")
|
|
if [ -z "$LUKS_PASS" ]; then
|
|
log "[FAIL] to get LUKS password"
|
|
return 1
|
|
else
|
|
log "[ OK ] get LUKS password"
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
function find_usb() {
|
|
for DISK in `ls -l /dev/disk/by-id/usb* | grep -v part | awk -F/ '{print $NF}'`; do
|
|
DEV="/dev/$DISK"
|
|
for PARTITION in `ls -1 $DEV* | grep "[0-9]$"`; do
|
|
cryptsetup isLuks $PARTITION
|
|
if [ $? = 0 ]; then
|
|
USB_PARTITION=$PARTITION
|
|
return 0
|
|
fi
|
|
done
|
|
log "[FAIL] to find encrypted USB partition"
|
|
return 1
|
|
done
|
|
log "[FAIL] to find encrypted USB disk"
|
|
return 1
|
|
}
|
|
|
|
function mount_usb() {
|
|
echo $LUKS_PASS | cryptsetup luksOpen $USB_PARTITION crypted_usb
|
|
if [ $? = 0 ]; then
|
|
log "[ OK ] $USB_PARTITION decrypted"
|
|
/usr/bin/mount /dev/mapper/crypted_usb /mnt/usb
|
|
if [ $? = 0 ]; then
|
|
log "[ OK ] $USB_PARTITION mounted"
|
|
return 0
|
|
else
|
|
log "[FAIL] $USB_PARTITION mounted"
|
|
return 1
|
|
fi
|
|
else
|
|
log "[FAIL] $USB_PARTITION decrypted"
|
|
return 1
|
|
fi
|
|
exit
|
|
}
|
|
|
|
function check_space () {
|
|
src_size=$(du -s "$SRC" | awk '{print $1}')
|
|
dst_size=$(df $MNT | grep "^/" | awk {'print $4'})
|
|
log "$SRC usage size: $src_size"
|
|
log "$MNT$DIR available size: $dst_size"
|
|
if [ "$dst_size" -gt "$src_size" ]; then
|
|
log "[ OK ] enought available space"
|
|
return 0
|
|
else
|
|
log "[FAIL] not enought available space"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
function sync_files() {
|
|
SRC_PATH=`dirname "$SRC"`
|
|
SRC_DIR=`basename "$SRC"`
|
|
DST_DIR=$USB_MOUNT$DST
|
|
log "[ OK ] source : ${SRC_DIR}"
|
|
log "[ OK ] destination: ${DST_DIR}/${SRC_DIR}"
|
|
log "------------ RSYNC STARTED ---------"
|
|
cd $SRC_PATH
|
|
if [ "$DELETE" == "yes" ]; then
|
|
rsync -av --stats \
|
|
--temp-dir=/tmp \
|
|
--links \
|
|
--human-readable \
|
|
--no-owner \
|
|
--no-group \
|
|
--include ".*" \
|
|
--delete \
|
|
"$SRC_DIR" "$DST_DIR" | tee -a $LOG
|
|
else
|
|
rsync -av \
|
|
--stats \
|
|
--temp-dir=/tmp \
|
|
--include ".*" \
|
|
--links \
|
|
--human-readable \
|
|
--no-owner \
|
|
--no-group \
|
|
"$SRC_DIR" "$DST_DIR" | tee -a $LOG
|
|
fi
|
|
cd /root
|
|
log "------------ RSYNC ENDED -----------"
|
|
return 0
|
|
}
|
|
|
|
|
|
function check_copy () {
|
|
cd ${SRC}
|
|
find * -type f -exec ls -s {} \; > /root/backup-src.log
|
|
cd ${DST_DIR}/${SRC_DIR}
|
|
find * -type f -exec ls -s {} \; > /root/backup-dst.log
|
|
cd
|
|
|
|
if diff -r /root/backup-src.log /root/backup-dst.log; then
|
|
log "[ OK ] files check"
|
|
else
|
|
log "[FAIL] files check"
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
function umount_usb() {
|
|
if /usr/bin/umount $USB_MOUNT; then
|
|
if [ $? = 0 ]; then
|
|
log "[ OK ] umount usb disk"
|
|
cryptsetup luksClose /dev/mapper/crypted_usb
|
|
if [ $? = 0 ]; then
|
|
log "[ OK ] close disk encryption"
|
|
return 0
|
|
else
|
|
log "[FAIL] close disk encryption"
|
|
return 1
|
|
fi
|
|
else
|
|
log "[FAIL] umount usb disk"
|
|
fi
|
|
else
|
|
log "[FAIL] to find mounted usb disk"
|
|
fi
|
|
}
|
|
|
|
function statistics_usb() {
|
|
USBTOTAL=$(df -h | grep $USB_MOUNT | awk '{print $2}')
|
|
USBUSAGE=$(df -h | grep $USB_MOUNT | awk '{print $5}')
|
|
USBUSED=$(df -h | grep $USB_MOUNT | awk '{print $3}')
|
|
USBFREE=$(df -h | grep $USB_MOUNT | awk '{print $4}')
|
|
log "USB DISK Total : ${USBTOTAL}"
|
|
log "USB DISK Usage : ${USBUSAGE}"
|
|
log "USB DISK Used : ${USBUSED}"
|
|
log "USB DISK Free : ${USBFREE}"
|
|
}
|
|
|
|
function mount() {
|
|
if find_usb; then
|
|
if get_luks_password; then
|
|
mount_usb
|
|
fi
|
|
fi
|
|
exit
|
|
}
|
|
|
|
function umount() {
|
|
umount_usb
|
|
exit
|
|
}
|
|
|
|
function check_depentencies() {
|
|
commands=(rsync cryptsetup curl jq)
|
|
for command in "${commands[@]}"
|
|
do
|
|
if ! command -v ${command} > /dev/null; then
|
|
log "Command $command not found"
|
|
exit
|
|
fi
|
|
done
|
|
}
|
|
|
|
function send_mail() {
|
|
COPIED=$( cat ${LOG} | grep "Number of created files:" | awk {'print $5'} )
|
|
DELETED=$( cat ${LOG} | grep "Number of deleted files:" | awk {'print $5'} )
|
|
SUBJECT="${COPIED} files copied, ${DELETED} files deleted"
|
|
echo "Backup Report" | mutt -s $SUBJECT -F /etc/muttrc $MAIL_RECIPIENT -a $LOG
|
|
}
|
|
|
|
function main () {
|
|
rm $LOG -rf
|
|
log ""
|
|
log "BACKUP STARTED ===================="
|
|
log "SOURCE : $SRC"
|
|
log "DESTINATION : $USB_MOUNT$DST"
|
|
log "WITH DELETE : ${DELETE:-"No (default)"}"
|
|
log ""
|
|
if check_depentencies; then
|
|
if get_luks_password; then
|
|
if find_usb; then
|
|
if mount_usb; then
|
|
sync_files
|
|
fi
|
|
statistics_usb
|
|
umount_usb
|
|
send_mail
|
|
fi
|
|
fi
|
|
fi
|
|
}
|
|
|
|
for i in "$@"
|
|
do
|
|
case $i in
|
|
--help)
|
|
usage
|
|
;;
|
|
--mount)
|
|
mount
|
|
;;
|
|
--umount)
|
|
umount
|
|
;;
|
|
--src=*)
|
|
SRC="${i#*=}"
|
|
shift
|
|
;;
|
|
--dst=*)
|
|
DST="${i#*=}"
|
|
shift
|
|
;;
|
|
--delete=*)
|
|
DELETE="${i#*=}"
|
|
shift
|
|
;;
|
|
*)
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
[ -z "$SRC" ] && usage "option \e[92m'--src'\e[39m is missing"
|
|
[ -z "$DST" ] && usage "option \e[92m'--dst'\e[39m is missing"
|
|
|
|
main
|
|
|