#!/bin/bash
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
ip=`wget -q -4 -O- http://icanhazip.com`
ip="${ip//./-}"
file=~/.bproc.dat
if [ ! -e "$file" ]; then
    echo "count=0" > "$file"
fi 
	
error () {
printf "%s: %s\n" "$(basename "${BASH_SOURCE}")" $'\e[1;31m'"${1}"$'\e[0m' >&2
exit 1
}
	
s3disk_check() {
r=-1;
mount > /tmp/s3disk_check.tmp
grep 's3fs on /mnt/s3disk type fuse.s3fs' /tmp/s3disk_check.tmp > /tmp/s3disk_check_res.tmp
filesize=$(ls -lh '/tmp/s3disk_check_res.tmp' | awk '{print  $5}')
if [ $filesize = 0 ]; then 
	r=0
else
	r=1
fi
rm /tmp/s3disk_check.tmp
rm /tmp/s3disk_check_res.tmp
return $r
}

s3disk_mount() {

if [ ! -d "/mnt/s3disk" ]; then
mkdir "/mnt/s3disk"
fi
if [ $? -eq '0' ]; then
sudo s3fs $1 /mnt/s3disk -o passwd_file=/root/.passwd-s3fs -o url=http://storage.yandexcloud.net -o use_path_request_style
sleep 1
fi
}

###################################
if [ -e "$file" ]; then
s3disk_check
fi
if [ $? -eq '0' ]; then
rm -r /mnt/s3disk
s3disk_mount $ip
fi

s3disk_check
if [ $? -eq '1' ]; then
	echo $'\e[1;32m'Baguette with the name ${bold}$ip${normal} successfully mounted !!!$'\e[0m'
	. "$file"
	(( count++ ))
	echo "count=$count" > "$file"
    log_file="backup-mysql-dump-progress.log"
	log_error="backup-mysql-dump-error.log"
    destination='/backups/mysql-dump'
	destination_mysql="/mnt/s3disk/mysql"
  
	source ini-file-parser.sh
	process_ini_file '/etc/my.cnf.d/mariabackup.cnf'
	userDB=$(get_value 'mariabackup' 'user')
	passwordDB=$(get_value 'mariabackup' 'password')

    if [ ! -d "/backups" ]; then
    mkdir "/backups"
    fi
    if [ ! -d "$destination" ]; then
    mkdir "$destination"
    fi
	if [ ! -d "$destination_mysql" ]; then
	mkdir "$destination_mysql"
	fi

    find $destination -type d \( -name "*-1[^5]" -o -name "*-[023]?" \) -mtime +30 -exec rm -R {} \; 2>&1
    find $destination -type d -name "*-*" -mtime +180 -exec rm -R {} \; 2>&1
    test -d $destination/$fdate || mkdir $destination/$fdate 2>&1
    
    echo 'Creating a database backup...'
	echo 'Please wait...'
    for dbname in `echo show databases | mysql -u$userDB -p$passwordDB 2>"$log_error" | grep -v Database`; do
        case $dbname in
            information_schema)
                continue ;;
            mysql)
                continue ;;
            performance_schema)
                continue ;;
            phpmyadmin)
                continue ;;
            *) mysqldump --databases --skip-comments --no-tablespaces -u$userDB -p$passwordDB $dbname > $destination/$dbname.sql ;
			
		esac
    done;
    
	filesize=$(ls -lh $log_error | awk '{print  $5}')
	if [ $filesize = 0 ]; then 
		tar -czvf /mnt/s3disk/mysql/backup-mysql-dump.tar.gz -c "${destination}" >> "${log_file}" 2>> "${log_file}"
		. "$file"
		(( count-- ))
		echo "count=$count" > "$file"
		if [ $count -lt 1 ]; then
		umount -l /mnt/s3disk 2> /tmp/s3disk_unount.tmp
		fi
		echo $'\e[1;32m'Backup created successfully !!!$'\e[0m'
	else
	. "$file"
	(( count-- ))
	echo "count=$count" > "$file"
	. "$file"
	if [ $count -lt 1 ]; then
		umount -l /mnt/s3disk 2> /tmp/s3disk_unount.tmp
	fi
	error "Backup failure! Check ${log_error} for more information"
	fi
	else echo $'\e[1;31m'S3DISK NOT MOUNTED $'\e[0m'
fi