config files added
This commit is contained in:
parent
4f3aa526e5
commit
1f7f84f090
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
server.backup
|
||||||
|
desktop.backup
|
||||||
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
IFS=$'\n'
|
IFS=$'\n'
|
||||||
|
PID=$$
|
||||||
|
|
||||||
NOW=$(date +"%Y-%m-%d_%H:%M")
|
NOW=$(date +"%Y-%m-%d_%H:%M")
|
||||||
LOG="/root/backup_$NOW.log"
|
LOG="/root/backup_$NOW.log"
|
||||||
|
|||||||
167
backup.sh
167
backup.sh
@ -15,17 +15,62 @@ function usage() {
|
|||||||
echo -e "./backup.sh \e[93m--help \e[39m"
|
echo -e "./backup.sh \e[93m--help \e[39m"
|
||||||
echo -e "./backup.sh \e[93m--mount \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--umount \e[39m"
|
||||||
|
echo
|
||||||
|
echo "Local directories"
|
||||||
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/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/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 -e "./backup.sh \e[92m--src=\e[39m/mnt/projects \e[96m--dst=\e[39m/backup \e[91m--delete=\e[39myes"
|
||||||
echo
|
echo
|
||||||
|
echo "Remote directories [smb|ssh]"
|
||||||
|
echo -e "./backup.sh \e[35m--config=\e[39m./windows.backup"
|
||||||
|
echo
|
||||||
echo -e " $1"
|
echo -e " $1"
|
||||||
echo
|
echo
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_luks_password() {
|
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"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log "[FAIL] mount //$HOST/$SHARE to /mnt/$PID"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
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
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log "[FAIL] mount $USER@$HOST:$SOURCE to /mnt/$SRC"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "[FAIL] remote dir $DIR not found"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "[FAIL] ssh $USER@$HOST"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "[FAIL] to create /mnt/$PID"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_luks_password() {
|
||||||
LUKS_PASS=$(curl -s $LUKS_PASS_URL | jq -r ".password")
|
LUKS_PASS=$(curl -s $LUKS_PASS_URL | jq -r ".password")
|
||||||
if [ -z "$LUKS_PASS" ]; then
|
if [ -z "$LUKS_PASS" ]; then
|
||||||
log "[FAIL] to get LUKS password"
|
log "[FAIL] to get LUKS password"
|
||||||
@ -53,21 +98,22 @@ function find_usb() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function mount_luks() {
|
function mount_luks() {
|
||||||
for n in `seq 0 9`; do
|
for n in `seq 0 9`; do
|
||||||
waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'})
|
waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'})
|
||||||
if [[ wating -eq "0" ]]; then
|
if [[ wating -eq "0" ]]; then
|
||||||
echo $1 | /usr/sbin/cryptsetup luksOpen $2 crypted_usb
|
for n in `seq 0 9`; do
|
||||||
if [ $? = 0 ]; then
|
echo $1 | /usr/sbin/cryptsetup luksOpen $2 crypted_usb
|
||||||
log "[ OK ] created /dev/mapper/crypted_usb"
|
if [ $? = 0 ]; then
|
||||||
return 0
|
log "[ OK ] created /dev/mapper/crypted_usb"
|
||||||
else
|
return 0
|
||||||
log "[FAIL] to create /dev/mapper/crypted_usb (error)"
|
fi
|
||||||
return 1
|
sleep 60
|
||||||
fi
|
done
|
||||||
|
log "[FAIL] to create /dev/mapper/crypted_usb (error)"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
sleep 2
|
sleep 60
|
||||||
done
|
done
|
||||||
log "[FAIL] to create /dev/mapper/crypted_usb (timeout)"
|
log "[FAIL] to create /dev/mapper/crypted_usb (timeout)"
|
||||||
return 1
|
return 1
|
||||||
@ -110,8 +156,6 @@ function sync_files() {
|
|||||||
SRC_PATH=`dirname "$SRC"`
|
SRC_PATH=`dirname "$SRC"`
|
||||||
SRC_DIR=`basename "$SRC"`
|
SRC_DIR=`basename "$SRC"`
|
||||||
DST_DIR=$USB_MOUNT$DST
|
DST_DIR=$USB_MOUNT$DST
|
||||||
log "[ OK ] source : ${SRC_DIR}"
|
|
||||||
log "[ OK ] destination: ${DST_DIR}/${SRC_DIR}"
|
|
||||||
log "------------ RSYNC STARTED ---------"
|
log "------------ RSYNC STARTED ---------"
|
||||||
cd $SRC_PATH
|
cd $SRC_PATH
|
||||||
if [ "$DELETE" == "yes" ]; then
|
if [ "$DELETE" == "yes" ]; then
|
||||||
@ -140,7 +184,6 @@ function sync_files() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function check_copy () {
|
function check_copy () {
|
||||||
cd ${SRC}
|
cd ${SRC}
|
||||||
find * -type f -exec ls -s {} \; > /root/backup-src.log
|
find * -type f -exec ls -s {} \; > /root/backup-src.log
|
||||||
@ -158,38 +201,25 @@ function check_copy () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function luks_close() {
|
function luks_close() {
|
||||||
for n in `seq 0 9`; do
|
for n in `seq 1 12`; do
|
||||||
waiting=$(grep 'Dirty\|Writeback' /proc/meminfo | grep Writeback: | awk {'print $2'})
|
/usr/sbin/cryptsetup close crypted_usb
|
||||||
if [[ waiting -eq "0" ]]; then
|
if [ $? = 0 ]; then
|
||||||
cryptsetup luksClose /dev/mapper/crypted_usb
|
log "[ OK ] luksClose /dev/mapper/crypted_usb"
|
||||||
if [ $? = 0 ]; then
|
return 0
|
||||||
log "[ OK ] luksClose /dev/mapper/crypted_usb"
|
fi
|
||||||
return 0
|
sleep 300
|
||||||
fi
|
|
||||||
fi
|
|
||||||
sleep 2
|
|
||||||
done
|
done
|
||||||
log "[FAIL] luksClose /dev/mapper/crypted_usb"
|
log "[FAIL] luksClose (timeout)"
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function umount_usb() {
|
function umount_usb() {
|
||||||
if /usr/bin/umount $USB_MOUNT; then
|
if /usr/bin/umount $USB_MOUNT; then
|
||||||
if [ $? = 0 ]; then
|
log "[ OK ] umount usb disk"
|
||||||
log "[ OK ] umount usb disk"
|
return 0
|
||||||
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
|
else
|
||||||
log "[FAIL] to find mounted usb disk"
|
log "[FAIL] to find mounted usb disk"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,8 +256,31 @@ function send_mail() {
|
|||||||
echo "Backup Report" | mutt -s $SUBJECT -F /etc/muttrc $MAIL_RECIPIENT -a $LOG
|
echo "Backup Report" | mutt -s $SUBJECT -F /etc/muttrc $MAIL_RECIPIENT -a $LOG
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function free_to_run() {
|
||||||
|
for n in `seq 0 9`; do
|
||||||
|
if [[ `ps aux | grep backup.sh | grep -v grep` ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
sleep 120
|
||||||
|
done
|
||||||
|
log "[ ] wating another instance to finish"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
function umount_remote() {
|
||||||
|
if umount /mnt/$PID then
|
||||||
|
log "[ OK ] umount /mnt/${PID}"
|
||||||
|
if rmdir /mnt/$PID; then
|
||||||
|
log "[ OK ] rmdir /mnt/${PID}"
|
||||||
|
else
|
||||||
|
log "[FAIL] rmdir /mnt/${PID}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "[FAIL] umount /mnt/${PID}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
function main () {
|
function main () {
|
||||||
rm $LOG -rf
|
|
||||||
log "SOURCE : $SRC"
|
log "SOURCE : $SRC"
|
||||||
log "DESTINATION : $USB_MOUNT$DST"
|
log "DESTINATION : $USB_MOUNT$DST"
|
||||||
log "DELETE : ${DELETE:-"No (default)"}"
|
log "DELETE : ${DELETE:-"No (default)"}"
|
||||||
@ -236,7 +289,9 @@ function main () {
|
|||||||
if mount_usb; then
|
if mount_usb; then
|
||||||
sync_files
|
sync_files
|
||||||
statistics_usb
|
statistics_usb
|
||||||
umount_usb
|
if umount_usb; then
|
||||||
|
luks_close
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
send_mail
|
send_mail
|
||||||
fi
|
fi
|
||||||
@ -267,12 +322,44 @@ do
|
|||||||
DELETE="${i#*=}"
|
DELETE="${i#*=}"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
--config=*)
|
||||||
|
CONFIG="${i#*=}"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
usage
|
usage
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [[ -n "$CONFIG" ]]; then
|
||||||
|
PID=$$
|
||||||
|
source $CONFIG
|
||||||
|
|
||||||
|
[ -z "$PROTO" ] && usage "config \e[92m'PROTO'\e[39m is missing"
|
||||||
|
[ -z "$SHARE" ] && usage "config \e[92m'SHARE'\e[39m is missing"
|
||||||
|
[ -z "$USER" ] && usage "config \e[92m'USER'\e[39m is missing"
|
||||||
|
[ -z "$PASS" ] && usage "config \e[92m'PASS'\e[39m is missing"
|
||||||
|
[ -z "$SOURCE" ] && usage "config \e[92m'SOURCE'\e[39m is missing"
|
||||||
|
[ -z "$DESTINATION" ] && usage "config \e[92m'DESTINATION'\e[39m is missing"
|
||||||
|
|
||||||
|
if [ "$PROTO" = "smb" ]; then
|
||||||
|
if mount_smb; then
|
||||||
|
echo "mount_smb"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$PROTO" = "ssh" ]; then
|
||||||
|
if mount_ssh; then
|
||||||
|
echo "mount_ssh"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
umount_remote
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit
|
||||||
|
|
||||||
[ -z "$SRC" ] && usage "option \e[92m'--src'\e[39m is missing"
|
[ -z "$SRC" ] && usage "option \e[92m'--src'\e[39m is missing"
|
||||||
[ -z "$DST" ] && usage "option \e[92m'--dst'\e[39m is missing"
|
[ -z "$DST" ] && usage "option \e[92m'--dst'\e[39m is missing"
|
||||||
|
|
||||||
|
|||||||
6
desktop.backup.tmp
Normal file
6
desktop.backup.tmp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
PROTO=ssh
|
||||||
|
HOST=192.168.2.11
|
||||||
|
USER=filio
|
||||||
|
TITLE="Office Desktop"
|
||||||
|
SOURCE=/home/filio/Documents/Filio
|
||||||
|
DESTINATION=/desktop
|
||||||
8
server.backup.tmp
Normal file
8
server.backup.tmp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
PROTO=smb
|
||||||
|
HOST=192.168.2.205
|
||||||
|
SHARE=backup
|
||||||
|
USER=backup
|
||||||
|
PASS=1234
|
||||||
|
TITLE="Epsilon Net Server"
|
||||||
|
SOURCE=/backup
|
||||||
|
DESTINATION=/server
|
||||||
142
smbbackup.sh
142
smbbackup.sh
@ -1,142 +0,0 @@
|
|||||||
#!/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
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user