#!/usr/bin/bash
#Enter point variables
timestamp=`date +%d.%m.%Y-%H:%M:%S`
src="/mnt/srv-db-2"
dst="/mnt/lv2TB1/samba/archive/srv-db-2"
logfile="/mnt/lv2TB1/logs/srv-db-2.txt"
diskspace=`df -h --block-size=g | grep /dev/mapper/vg--01-lv2TB1 | awk '{print $6, $2, $3, $4, $5}'`
dstweight=`du -sh $dst | awk '{print $2, $1}'`
md5srcfile="/mnt/lv2TB1/scripts/srv-db-2/src.md5"
md5dstfile="/mnt/lv2TB1/scripts/srv-db-2/dst.md5"
haslist=`cat $md5dstfile`
#Lof file creating
echo "Бекап начат: $timestamp" > $logdir/$logfile
#Pushing subfolders
for list in `ls $src`
do
#Searching for elder and newest files
old=`ls -t | tail -1`
last=`ls -t $src/$list | head -1`
mkdir $dst/$list
#Retention policy
#rm -rf $dst/$list/$old
#Copying
#cp $src/$list/$last $dst/$list/
done
#Asumming hashes
for list in `ls $dst`
do
cd $dst/$list
dir=`pwd`
slash="/"
file=`ls -t | head -1`
echo "$dir$slah$file"
done
#Log & Alarm
for subfolders in `ls $dst`
do
cd $dst/$subfolders
echo "Папка:" >> $logfile
pwd >> $logfile
ls -la --block-size=M | grep bak | awk '{ print $9, $5, $1 }' >> $logfile
done
if cmp -s $md5srcfile $md5dstfile ; then
echo "Хэши md5 равны:" >> $logfile
for i in `cat $md5dstfile`
do
echo "$i" >> $logfile
done
echo -e "Бекап успешно завершен: $timestamp\n" >> $logfile
echo -e "Размер папки бекапов: $dstweight\n" >> $logfile
echo -e "Состояние диска: $diskspace\n" >> $logfile
curl --header 'Content-Type: application/json' --request 'POST' --data '{"chat_id":"1208760462","text":"Бекап srv-db-2 завершен. Лови лог"}' "https://api.telegram.org/bot/sendMessage"
curl -F document=@"$logfile" https://api.telegram.org/bot/sendDocument?chat_id=
else
echo -e "\n" >> $logfile
echo "Бекап провален: $timestamp\n" >> $logfile
echo -e "Размер папки бекапов: $dstweight\n" >> $logfile
echo -e "Состояние диска: $diskspace\n" >> $logfile
curl --header 'Content-Type: application/json' --request 'POST' --data '{"chat_id":"1208760462","text":"Бекап srv-db-2 провален. Лови лог"}' "https://api.telegram.org/bot/sendMessage"
curl -F document=@"$logfile" https://api.telegram.org/bot/sendDocument?chat_id=
fi
#Removing temporary
rm -rf $md5srcfile $md5dstfile