#!/bin/bash
export LC_ALL=C
ip=`wget -q -4 -O- http://icanhazip.com`
ip="${ip//./-}"
file=~/.bproc.dat
if [ ! -e "$file" ]; then
    echo "count=0" > "$file"
fi 

destination="/mnt/s3disk/mysql"
parent_dir="/backups/mysql"
mnt_dir="/mnt/s3disk/mysql"
defaults_file="/etc/my.cnf.d/mariabackup.cnf"
#encryption_key_file="${parent_dir}/encryption_key"
todays_dir="${parent_dir}/$(date +%A)"
log_file="backup-progress.log"

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
}


check_parent_dir () {
if [ ! -d "$parent_dir" ]; then
  mkdir /backups
  echo Create Directory: "$parent_dir" > "${log_file}" 2>> "${log_file}"
  
fi
}

check_mnt_dir () {
if [ ! -d "$mnt_dir" ]; then
  mkdir "$mnt_dir"
  echo Create Directory: "$mnt_dir" > "${log_file}" 2>> "${log_file}"
  
fi
}


error () {
    printf "%s: %s\n" "$(basename "${BASH_SOURCE}")" $'\e[1;31m'"${1}"$'\e[0m' >&2
    exit 1
}

set_options () {
    # List the innobackupex arguments
    #declare -ga mariabackup_args=(
        #"--encrypt=AES256"
        #"--encrypt-key-file=${encryption_key_file}"
        #"--encrypt-threads=${processors}"
        #"--slave-info"
        #"--incremental"
        
    mariabackup_args=(
        "--defaults-file=${defaults_file}"
        "--extra-lsndir=${todays_dir}"
        "--backup"
        "--compress"
        "--stream=xbstream"
        "--parallel=${processors}"
        "--compress-threads=${processors}"
    )
    
    backup_type="full"

    
}



take_backup () {
	
    rm -Rf /backups/mysql
    mariabackup --backup --target-dir="${parent_dir}" > "${log_file}" 2>> "${log_file}"
	mariabackup --prepare --target-dir="${parent_dir}" >> "${log_file}" 2>> "${log_file}"
	
}
################################################################
if [ -e "$file" ]; then
s3disk_check
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'
	if [ ! -d "$destination" ]; then
	mkdir "$destination"
	fi
	sleep 2
	. "$file"
	(( count++ ))
	echo "count=$count" > "$file"
	echo 'Creating a database backup...'
	echo 'Please wait...'
	check_parent_dir && check_mnt_dir && set_options && take_backup

	if tail -1 "${log_file}" | grep -q "completed OK"; then
		sudo /usr/bin/tar -czvf /mnt/s3disk/mysql/backup.tar.gz -c "${parent_dir}" >> "${log_file}" 2>> "${log_file}"
		echo $'\e[1;32m'Backup created successfully !!!$'\e[0m'
    else
	. "$file"
	(( count-- ))
	echo "count=$count" > "$file"
    error "Backup failure! Check ${log_file} for more information"
	fi
	. "$file"
	(( count-- ))
	echo "count=$count" > "$file"
else echo $'\e[1;31m'S3DISK NOT MOUNTED $'\e[0m'
fi	
. "$file"
	if [ $count -lt 1 ]; then
	umount -l /mnt/s3disk 2> /tmp/s3disk_unount.tmp
	fi
fi