#!/bin/bash
#
DIR=/dbbackup
#v510 mysql8
FULL='2021-11-01-00:10:01-base'
#INCRE=(
#'2019-12-17-00:10:01-inc'
#'2019-12-18-00:10:01-inc'
#'2019-12-19-00:10:01-inc'
#'2019-12-20-00:10:01-inc'
#'2019-12-21-00:10:01-inc'
#'2019-12-22-00:10:02-inc'
#)

#v590 v591
#FULL='2019-12-23_full.tar.gz'
#INCRE=(
#'2019-12-24.tar.gz'
#'2019-12-25.tar.gz'
#'2019-12-26.tar.gz'
#)



MYSQL8(){

DIRS=(${INCRE[@]} $FULL)
cd $DIR
for dir in ${DIRS[@]}; do
    cd $dir
    gzip -d ${dir}.gz
    xbstream -x < $dir
    cd ..
done

cat > /etc/mysql/my.cnf <<EOF
[mysqld]
datadir=/var/lib/mysql
EOF

if [ -z "$INCRE" ]; then
    echo -e "\033[32m 开始执行还原准备操作 \033[0m"

    xtrabackup --prepare --apply-log-only --target-dir=$DIR/$FULL
    if [ $? -ne 0 ]; then
        echo -e "\033[31m 执行全备恢复操作失败!! \033[0m"
        exit 1
    else
        echo -e "\033[32m 开始还原mysql数据,请注意执行完成后是否打印'completed OK',否则为失败！ \033[0m"
    fi
    sleep 2

    cd $DIR/$FULL && mv $FULL ../${FULL}.xb && cd -

    xtrabackup  --defaults-file=/etc/mysql/my.cnf --copy-back  --target-dir=$DIR/$FULL
else
    echo -e "\033[32m 开始执行还原准备操作 \033[0m"

    xtrabackup --prepare --apply-log-only --target-dir=$DIR/$FULL

    if [ $? -ne 0 ]; then
        echo -e "\033[31m 执行全备恢复操作失败!! \033[0m"
        exit 1
    fi

    sleep 2

    for i in ${INCRE[@]}; do
        echo -e "\033[32m 执行还原准备操作:[$i] \033[0m"
        xtrabackup --prepare --apply-log-only --target-dir=$DIR/$FULL --incremental-dir=$DIR/$i
    
        if [ $? -ne 0 ]; then
           echo -e "\033[31m 执行还原准备操作[$i]失败!! \033[0m"
           return
        fi

        sleep 2

    done

    echo -e "\033[32m 结束还原准备操作 \033[0m"

    sleep 2

    xtrabackup --prepare --apply-log-only --target-dir=$DIR/$FULL

    echo -e "\033[32m 开始还原mysql数据,请注意执行完成后是否打印'completed OK',否则为失败！ \033[0m"

    sleep 2

    xtrabackup  --defaults-file=/etc/mysql/my.cnf --copy-back  --target-dir=$DIR/$FULL
fi

chown -R mysql.mysql /var/lib/mysql


}


MYSQL56(){

cd $DIR

mkdir fulldir

tar xf $FULL -C fulldir/


for f in ${INCRE[@]}; do
    tar xf $f
done


echo -e "\033[32m 开始执行还原准备操作 \033[0m"

sleep 2

innobackupex --use-memory=100M --apply-log --redo-only fulldir

if [ $? -ne 0 ]; then
    echo -e "\033[31m 执行全备恢复操作失败!! \033[0m"
    exit 1
fi


counts=${#INCRE[@]}
count=1

for i in ${INCRE[@]}; do

    echo -e "\033[32m 执行还原准备操作:[$i] \033[0m"

    sleep 2
 
    if [ $count -ne $counts ]; then
        innobackupex --use-memory=100M --apply-log --redo-only fulldir --incremental-dir=${i%%.*}
         
        if [ $? -ne 0 ]; then
             echo -e "\033[31m 执行还原准备操作[$i]失败!! \033[0m"
             return
        else
             let count++
        fi
    else
        innobackupex --use-memory=100M --apply-log  fulldir --incremental-dir=${i%%.*}
        if [ $? -ne 0 ]; then
             echo -e "\033[31m 执行还原准备操作[$i]失败!! \033[0m"
             return
        fi

    fi

done


echo -e "\033[32m 开始还原mysql数据,请注意执行完成后是否打印'completed OK',否则为失败！ \033[0m"

sleep 2

innobackupex --copy-back fulldir


chown -R mysql.mysql /var/lib/mysql


}

version=`mysqld -V | cut -d ' ' -f 4`
ver=`echo ${version%%.*}`

case $ver in 

5)
   MYSQL56;;
8)
   MYSQL8;;
esac

