diff --git a/backup.sh b/backup.sh index 9fa15b6..f357803 100755 --- a/backup.sh +++ b/backup.sh @@ -25,8 +25,6 @@ function usage() { } function get_luks_password() { - log " Ask for LUKS password" - log " from API $LUKS_PASS_URL" LUKS_PASS=$(curl -s $LUKS_PASS_URL | jq -r ".password") if [ -z "$LUKS_PASS" ]; then @@ -58,7 +56,6 @@ function find_usb() { function mount_luks() { for n in `seq 0 9`; do - log " trying create /dev/mapper/crypted_usb" waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'}) if [[ wating -eq "0" ]]; then echo $1 | /usr/sbin/cryptsetup luksOpen $2 crypted_usb @@ -126,7 +123,7 @@ function sync_files() { --no-group \ --include ".*" \ --delete \ - "$SRC_DIR" "$DST_DIR" | tee -a $LOG + "$SRC_DIR" "$DST_DIR" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG else rsync -av \ --stats \ @@ -136,7 +133,7 @@ function sync_files() { --human-readable \ --no-owner \ --no-group \ - "$SRC_DIR" "$DST_DIR" | tee -a $LOG + "$SRC_DIR" "$DST_DIR" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG fi cd /root log "------------ RSYNC ENDED -----------" @@ -165,11 +162,14 @@ function luks_close() { waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'}) if [[ waiting -eq "0" ]]; then cryptsetup luksClose /dev/mapper/crypted_usb - return 0 + if [ $? = 0 ]; then + log "[ OK ] luksClose /dev/mapper/crypted_usb" + return 0 + fi fi sleep 2 done - echo "R1" + log "[FAIL] luksClose /dev/mapper/crypted_usb" return 1 } @@ -228,18 +228,16 @@ function send_mail() { function main () { rm $LOG -rf - log "" - log "SOURCE : $SRC" - log "DESTINATION : $USB_MOUNT$DST" - log "WITH DELETE : ${DELETE:-"No (default)"}" - log "" + log "SOURCE : $SRC" + log "DESTINATION : $USB_MOUNT$DST" + log "DELETE : ${DELETE:-"No (default)"}" if get_luks_password; then if find_usb; then if mount_usb; then sync_files + statistics_usb + umount_usb fi - statistics_usb - umount_usb send_mail fi fi diff --git a/smbbackup.sh b/smbbackup.sh new file mode 100755 index 0000000..c3c9138 --- /dev/null +++ b/smbbackup.sh @@ -0,0 +1,142 @@ +#!/bin/bash + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd $SCRIPT_DIR + +. ./backup.config + +function log() { + echo -e "\e[96m`date +'%d/%m/%Y %H:%M:%S'` \e[39m| $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 mount_usb() { + mount_luks $LUKS_PASS $USB_PARTITION + 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 umount_usb() { + if /usr/bin/umount $USB_MOUNT; then + if [ $? = 0 ]; then + log "[ OK ] umount usb disk" + luks_close + 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 mount() { + if find_usb; then + if get_luks_password; then + mount_usb + fi + fi + exit +} + +function umount() { + umount_usb + exit +} + +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'} ) + TRANSFERRED=$( cat ${LOG} | grep "Number of regular files transferred:" | awk {'print $6'} ) + SUBJECT="${COPIED} files copied, ${DELETED} files deleted, ${TRANSFERRED} files transferred" + echo "Backup Report" | mutt -s $SUBJECT -F /etc/muttrc $MAIL_RECIPIENT -a $LOG +} + +function main () { + rm $LOG -rf + log "" + log "SOURCE : $SRC" + log "DESTINATION : $USB_MOUNT$DST" + log "WITH DELETE : ${DELETE:-"No (default)"}" + log "" + if get_luks_password; then + if find_usb; then + if mount_usb; then + sync_files + fi + statistics_usb + umount_usb + send_mail + 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 +