#!/bin/bash
#

MYSQL_DIR=/home/edoc2/macrowing/edoc2v5/data/mysql_tmp
BACKUP_DIR=/home/edoc2/macrowing/edoc2v5/data/dbbackup_tmp
IMAGE=registry.edoc2.com:5000/edoc2v5/mysql:prod600-v8.0.17.17


cat << 'EOF' > ${BACKUP_DIR}/mysql_restore.sh
#!/bin/bash

DIR=/dbbackup

#v510 mysql8
## 修改此处配置 ##
FULL='2021-07-07-16:35:58-base'
INCRE=(
'2021-07-07-16:41:07-inc'
'2021-07-08-16:41:07-inc'
'2021-07-09-16:41:07-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
    if [ -f *.gz ];then
        gzip -d ${dir}.gz
        xbstream -x < $dir
    fi
    cd ..
done


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

if [ ! -f $$DIR/$FULL/xtrabackup_master_key_id ];then
    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
fi



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

echo -e  "[mysqld]\ndatadir=/var/lib/mysql"  > /etc/mysql/my.cnf

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

sleep 2

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

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




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

EOF
chmod +x ${BACKUP_DIR}/mysql_restore.sh

if [ "$(ls -A ${MYSQL_DIR})" ]; then
    mv ${MYSQL_DIR} ${MYSQL_DIR}-`date +%Y%d%m-%H%M%S` && mkdir ${MYSQL_DIR}
else
    mkdir -p ${MYSQL_DIR}
fi

# docker run -it --rm -v $MYSQL_DIR:/var/lib/mysql -v $BACKUP_DIR:/dbbackup  $IMAGE --entrypoint bash /dbbackup/mysql_restore.sh
docker run -it  -e MODE=standalone --rm -v $MYSQL_DIR:/var/lib/mysql -v $BACKUP_DIR:/dbbackup  --entrypoint /dbbackup/mysql_restore.sh  $IMAGE

if [ $? -eq 0 ]; then
    echo -e "\033[32m 请注意执行完成后是否打印'completed OK',否则为失败！ \033[0m"
    echo -e "\033[32m 请启动ecm服务,如果是集群环境,请通过集群数据恢复脚本,将此节点数据恢复到其他数据库节点 \033[0m"
fi
