configs
This commit is contained in:
parent
1f7f84f090
commit
7074822870
@ -4,7 +4,7 @@ PID=$$
|
||||
NOW=$(date +"%Y-%m-%d_%H:%M")
|
||||
LOG="/root/backup_$NOW.log"
|
||||
|
||||
LUKS_PASS_URL="192.168.1.199:9999/andthepasswordis"
|
||||
LUKS_PASS_URL="192.168.2.202:9999/andthepasswordis"
|
||||
LUKS_PASS=""
|
||||
|
||||
USB_DISK=""
|
||||
|
||||
212
backup.sh
212
backup.sh
@ -6,8 +6,8 @@ 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
|
||||
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() {
|
||||
@ -15,6 +15,7 @@ function usage() {
|
||||
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[93m--format=\e[39m/dev/sda"
|
||||
echo
|
||||
echo "Local directories"
|
||||
echo -e "./backup.sh \e[92m--src=\e[39m/mnt/data \e[96m--dst=\e[39m/backup"
|
||||
@ -30,49 +31,48 @@ function usage() {
|
||||
}
|
||||
|
||||
function mount_smb() {
|
||||
if mkdir /mnt/$PID; then
|
||||
log "[ OK ] creating /mnt/$PID"
|
||||
exit
|
||||
if mount -t cifs //$HOST/$SHARE /mnt/$PID -o username=$USER,password=$PASS; then
|
||||
log "[ OK ] mount //$HOST/$SHARE to /mnt/$PID"
|
||||
if mkdir /mnt/${PID}; then
|
||||
log "[ OK ] creating /mnt/${PID}"
|
||||
if mount -t cifs //${HOST}/${SHARE} /mnt/${PID} -o username=${USER},password=${PASS}; then
|
||||
log "[ OK ] mount //${HOST}/${SHARE} to /mnt/${PID}"
|
||||
return 0
|
||||
else
|
||||
log "[FAIL] mount //$HOST/$SHARE to /mnt/$PID"
|
||||
log "[FAIL] mount //${HOST}/${SHARE} to /mnt/${PID}"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log "[FAIL] to create /mnt/$PID"
|
||||
log "[FAIL] to create /mnt/${PID}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function mount_ssh() {
|
||||
if mkdir /mnt/$PID; then
|
||||
if ssh -q -o BatchMode=yes -o ConnectTimeout=10 $USER@$HOST exit; then
|
||||
if ssh $USER@$HOST ls -l $DIR 2> /dev/null; then
|
||||
if sshfs -o ro $USER@$HOST:$SOURCE /mnt/$PID; then
|
||||
if mkdir /mnt/${PID}; then
|
||||
if ssh -q -o BatchMode=yes -o ConnectTimeout=10 ${USER}@${HOST} exit; then
|
||||
if ssh ${USER}@${HOST} ls -l ${DIR} 2> /dev/null; then
|
||||
if sshfs -o ro ${USER}@${HOST}:${SOURCE} /mnt/${PID}; then
|
||||
return 0
|
||||
else
|
||||
log "[FAIL] mount $USER@$HOST:$SOURCE to /mnt/$SRC"
|
||||
log "[FAIL] mount ${USER}@${HOST}:${SOURCE} to /mnt/${SRC}"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log "[FAIL] remote dir $DIR not found"
|
||||
log "[FAIL] remote dir ${DIR} not found"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log "[FAIL] ssh $USER@$HOST"
|
||||
log "[FAIL] ssh ${USER}@${HOST}"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log "[FAIL] to create /mnt/$PID"
|
||||
log "[FAIL] to create /mnt/${PID}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function get_luks_password() {
|
||||
LUKS_PASS=$(curl -s $LUKS_PASS_URL | jq -r ".password")
|
||||
if [ -z "$LUKS_PASS" ]; then
|
||||
LUKS_PASS=$(curl --connect-timeout 10 -s ${LUKS_PASS_URL} | jq -r ".password")
|
||||
if [ -z "${LUKS_PASS}" ]; then
|
||||
log "[FAIL] to get LUKS password"
|
||||
return 1
|
||||
else
|
||||
@ -81,20 +81,16 @@ function get_luks_password() {
|
||||
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
|
||||
/usr/sbin/cryptsetup isLuks $PARTITION
|
||||
if [ $? = 0 ]; then
|
||||
USB_PARTITION=$PARTITION
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
log "[FAIL] to find encrypted USB partition"
|
||||
return 1
|
||||
function find_luks() {
|
||||
for DEV in `ls -1 /dev/disk/by-id/usb*`; do
|
||||
if cryptsetup isLuks ${DEV}; then
|
||||
USB_DEV=${DEV}
|
||||
LOGDEV=$(echo $DEV | cut -c21- )
|
||||
log "[ OK ] find encrypted ${LOGDEV}"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
log "[FAIL] to find encrypted USB disk"
|
||||
log "[FAIL] to find encrypted disk"
|
||||
return 1
|
||||
}
|
||||
|
||||
@ -103,46 +99,37 @@ function mount_luks() {
|
||||
waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'})
|
||||
if [[ wating -eq "0" ]]; then
|
||||
for n in `seq 0 9`; do
|
||||
echo $1 | /usr/sbin/cryptsetup luksOpen $2 crypted_usb
|
||||
echo $1 | cryptsetup luksOpen $2 crypted_usb
|
||||
if [ $? = 0 ]; then
|
||||
log "[ OK ] created /dev/mapper/crypted_usb"
|
||||
log "[ OK ] luksOpen crypted_usb"
|
||||
return 0
|
||||
fi
|
||||
sleep 60
|
||||
done
|
||||
log "[FAIL] to create /dev/mapper/crypted_usb (error)"
|
||||
log "[FAIL] luksOpen crypted_usb (error)"
|
||||
return 1
|
||||
fi
|
||||
sleep 60
|
||||
done
|
||||
log "[FAIL] to create /dev/mapper/crypted_usb (timeout)"
|
||||
log "[FAIL] luksOpen crypted_usb (timeout)"
|
||||
return 1
|
||||
}
|
||||
|
||||
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
|
||||
if mount /dev/mapper/crypted_usb /mnt/usb; then
|
||||
log "[ OK ] mount crypted_usb to /mnt/usb"
|
||||
return 0
|
||||
else
|
||||
log "[FAIL] $USB_PARTITION decrypted"
|
||||
log "[FAIL] to mount crypted_usb to /mnt/usb"
|
||||
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"
|
||||
src_size=$(du -s /mnt/${PID} | awk '{print $1}')
|
||||
dst_size=$(df /mnt/usb | grep "^/" | awk {'print $4'})
|
||||
log "USB usage size: $src_size"
|
||||
log "USB available size: $dst_size"
|
||||
if [ "$dst_size" -gt "$src_size" ]; then
|
||||
log "[ OK ] enought available space"
|
||||
return 0
|
||||
@ -153,10 +140,14 @@ function check_space () {
|
||||
}
|
||||
|
||||
function sync_files() {
|
||||
SRC_PATH=`dirname "$SRC"`
|
||||
SRC_DIR=`basename "$SRC"`
|
||||
DST_DIR=$USB_MOUNT$DST
|
||||
SRC="/mnt/${PID}${SOURCE}"
|
||||
DST="/mnt/usb${DESTINATION}"
|
||||
|
||||
log "SOURCE : ${SRC}"
|
||||
log "DESTINATION : ${DST}"
|
||||
log "DELETE : ${DELETE:-"No (default)"}"
|
||||
log "------------ RSYNC STARTED ---------"
|
||||
|
||||
cd $SRC_PATH
|
||||
if [ "$DELETE" == "yes" ]; then
|
||||
rsync -av --stats \
|
||||
@ -167,7 +158,7 @@ function sync_files() {
|
||||
--no-group \
|
||||
--include ".*" \
|
||||
--delete \
|
||||
"$SRC_DIR" "$DST_DIR" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG
|
||||
"${SRC}" "${DST}" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG
|
||||
else
|
||||
rsync -av \
|
||||
--stats \
|
||||
@ -177,15 +168,16 @@ function sync_files() {
|
||||
--human-readable \
|
||||
--no-owner \
|
||||
--no-group \
|
||||
"$SRC_DIR" "$DST_DIR" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG
|
||||
"${SRC}" "${DST}" | sed '/sending\ incremental\ file\ list/d' | tee -a $LOG
|
||||
fi
|
||||
cd /root
|
||||
|
||||
cd -
|
||||
log "------------ RSYNC ENDED -----------"
|
||||
return 0
|
||||
}
|
||||
|
||||
function check_copy () {
|
||||
cd ${SRC}
|
||||
cd ${SOURCE}
|
||||
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
|
||||
@ -200,42 +192,42 @@ function check_copy () {
|
||||
return 0
|
||||
}
|
||||
|
||||
function umount_usb() {
|
||||
if umount /mnt/usb; then
|
||||
log "[ OK ] umount /mnt/usb"
|
||||
return 0
|
||||
else
|
||||
log "[FAIL] umount /mnt/usb"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function luks_close() {
|
||||
for n in `seq 1 12`; do
|
||||
/usr/sbin/cryptsetup close crypted_usb
|
||||
cryptsetup close crypted_usb
|
||||
if [ $? = 0 ]; then
|
||||
log "[ OK ] luksClose /dev/mapper/crypted_usb"
|
||||
return 0
|
||||
fi
|
||||
sleep 300
|
||||
sleep 10
|
||||
done
|
||||
log "[FAIL] luksClose (timeout)"
|
||||
return 1
|
||||
}
|
||||
|
||||
function umount_usb() {
|
||||
if /usr/bin/umount $USB_MOUNT; then
|
||||
log "[ OK ] umount usb disk"
|
||||
return 0
|
||||
else
|
||||
log "[FAIL] to find mounted usb disk"
|
||||
return 1
|
||||
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}"
|
||||
USBTOTAL=$(df -h | grep /mnt/usb | awk '{print $2}')
|
||||
USBUSAGE=$(df -h | grep /mnt/usb | awk '{print $5}')
|
||||
USBUSED=$(df -h | grep /mnt/usb | awk '{print $3}')
|
||||
USBFREE=$(df -h | grep /mnt/usb | 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
|
||||
function mount_only() {
|
||||
if find_luks; then
|
||||
if get_luks_password; then
|
||||
mount_usb
|
||||
fi
|
||||
@ -243,11 +235,6 @@ function mount() {
|
||||
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'} )
|
||||
@ -268,7 +255,7 @@ function free_to_run() {
|
||||
}
|
||||
|
||||
function umount_remote() {
|
||||
if umount /mnt/$PID then
|
||||
if umount /mnt/$PID; then
|
||||
log "[ OK ] umount /mnt/${PID}"
|
||||
if rmdir /mnt/$PID; then
|
||||
log "[ OK ] rmdir /mnt/${PID}"
|
||||
@ -280,22 +267,34 @@ function umount_remote() {
|
||||
fi
|
||||
}
|
||||
|
||||
function format() {
|
||||
if [ -b ${FORMAT} ]; then
|
||||
if cryptsetup -y -v luksFormat ${FORMAT}; then
|
||||
log "[ OK ] formating ${FORMAT}"
|
||||
else
|
||||
log "[FAIL] formating ${FORMAT}"
|
||||
fi
|
||||
else
|
||||
log "[FAIL] ${FORMAT} not found"
|
||||
fi
|
||||
exit
|
||||
}
|
||||
|
||||
function main () {
|
||||
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
|
||||
if umount_usb; then
|
||||
luks_close
|
||||
if find_luks; then
|
||||
if mount_luks $LUKS_PASS $USB_DEV; then
|
||||
if mount_usb; then
|
||||
sync_files
|
||||
statistics_usb
|
||||
if umount_usb; then
|
||||
luks_close
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
send_mail
|
||||
fi
|
||||
fi
|
||||
send_mail
|
||||
}
|
||||
|
||||
for i in "$@"
|
||||
@ -305,10 +304,15 @@ do
|
||||
usage
|
||||
;;
|
||||
--mount)
|
||||
mount
|
||||
mount_only
|
||||
;;
|
||||
--umount)
|
||||
umount
|
||||
umount_x
|
||||
;;
|
||||
--format=*)
|
||||
FORMAT="${i#*=}"
|
||||
format
|
||||
shift
|
||||
;;
|
||||
--src=*)
|
||||
SRC="${i#*=}"
|
||||
@ -345,13 +349,13 @@ if [[ -n "$CONFIG" ]]; then
|
||||
|
||||
if [ "$PROTO" = "smb" ]; then
|
||||
if mount_smb; then
|
||||
echo "mount_smb"
|
||||
main
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$PROTO" = "ssh" ]; then
|
||||
if mount_ssh; then
|
||||
echo "mount_ssh"
|
||||
main
|
||||
fi
|
||||
fi
|
||||
|
||||
@ -360,8 +364,8 @@ fi
|
||||
|
||||
exit
|
||||
|
||||
[ -z "$SRC" ] && usage "option \e[92m'--src'\e[39m is missing"
|
||||
[ -z "$DST" ] && usage "option \e[92m'--dst'\e[39m is missing"
|
||||
[ -z "$SOURCE" ] && usage "option \e[92m'--src'\e[39m is missing"
|
||||
[ -z "$DESTINATION" ] && usage "option \e[92m'--dst'\e[39m is missing"
|
||||
|
||||
main
|
||||
|
||||
|
||||
0
password-api/password.py
Normal file → Executable file
0
password-api/password.py
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user