MySQL云备份恢复脚本

2024-06-07 12:16:12 浏览数 (1)

代码语言:bash复制
#!/bin/bash
#------------------------------------------------
#Filename: recover.sh
#Revision: 1.0
#Date:    2022/11/05
#Description:This is the data recovery script
#-------------------------------------------------
############################安装部署MySQL5.7.31####################################
#传参下载地址
if [[ "$1" == "" ]];then
echo "注意!请使用source执行脚本,在脚本后面传入'下载地址';列如:source recover.sh 'Download.URL'";
else
url="https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
#数据库目录
workDir="/app"
mkdir ${workDir}/data -p
cd ${workDir};
wget_Status=`rpm -qa |grep -i "wget"|wc -l`;
if [[ ${wget_Status} == 0 ]]; then
#下载wget命令
yum install -y wget
fi

if [[ $? != 0 ]]; then
echo "wget下载失败,请确认网络或者yum源无问题后重新执行次脚本" && exit -1;
fi
#下载云上备份
mkdir ${workDir}/data1 -p
wget -c $1 -O ${workDir}/data1/test.xb
#下载mysql
sql=${url##*/}
wget ${url} -O ${workDir}/${url##*/}
tar xf ${workDir}/${url##*/} -C ${workDir}/;
mv  ${workDir}/${sql%.tar.gz} ${workDir}/mysql;
rm -rf ${workDir}/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
#初始化环境
Mdb_Status=`rpm -qa |grep -i "mariadb"|wc -l`;
if [[ ${Mdb_Status} != 0 ]]; then
yum remove mariadb -y
fi
Path="export PATH=${workDir}/mysql/bin:${workDir}/xbk/bin";
cat /etc/profile |grep ${Path} 2>/dev/null
if [[ $? != 0 ]]; then
echo -n ${Path} >>/etc/profile
echo  ':$PATH'  >>/etc/profile
source /etc/profile
fi
#创建虚拟用户给目录授权
user_status=`cat /etc/passwd |awk -F ':' '{print $1}'|grep mysql|wc -l`;
if [[  ${user_status} -eq 0  ]]; then
useradd -s /bin/nologoin mysql
fi
chown -R mysql.mysql ${workDir};
#初始化数据库
lib_Status=`rpm -qa |grep -i "libaio-devel"|wc -l`;
if [[ ${lib_Status} == 0 ]]; then
yum install -y libaio-devel
fi
mysqld --initialize-insecure --user=mysql --basedir=${workDir}/mysql --datadir=${workDir}/data
#设置/etc/my.cnf
cat >/etc/my.cnf<<EOF
[mysqld]
user=mysql
basedir=${workDir}/mysql
datadir=${workDir}/data
socket=/tmp/mysql.sock
server_id=666
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
#配置systemd启动方式
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=${workDir}/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld
clear
mysqld_Status=`ps aux |grep "${workDir}/mysql/bin/mysqld --defaults-file=/etc/my.cnf"|wc -l`;
if [[ ${mysqld_Status} == 2 ]]; then
echo '数据库安装并启动完成!准备数据恢复......';
else

echo "未安装成功";
fi
######################恢复数据#####################################
sleep 10
systemctl stop mysqld
#创建云上备份下载目录
wget -c 'https://docs-tencentdb-1256569818.cos.ap-guangzhou.myzijiebao.com/qpress-11-linux-x64.tar'
tar -xf qpress-11-linux-x64.tar -C /usr/local/bin
rm -rf qpress-11-linux-x64.tar
wget 'https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.23/binary/tarball/percona-xtrabackup-2.4.23-Linux-x86_64.glibc2.12.tar.gz'
tar -xf percona-xtrabackup-2.4.23-Linux-x86_64.glibc2.12.tar.gz
mv percona-xtrabackup-2.4.23-Linux-x86_64.glibc2.12 ${workDir}/xbk
cd ${workDir}/data1/
source /etc/profile
#数据解压
xbstream -x ${workDir}/data1/ < ${workDir}/data1/test.xb
xtrabackup  --remove-original --decompress --target-dir=${workDir}/data1/
chown -R mysql:mysql ${workDir}
#启动mysqld进程
mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=${workDir}/data1 &> /dev/null
clear
mysql -S /tmp/mysql.sock -e "SELECT 1" > /dev/null 2>&1
# 根据连接结果判断
if [ $? -eq 0 ]; then
    echo "数据库恢复失败"
else
    echo "数据恢复成功,请使用云数据库账号密码登录验证"
fi
fi

0 人点赞