#!/bin/bash
user="root"
host="127.0.0.1"
passwd="password"
conf_file="/etc/mysql/mysql.conf.d/mysqld.cnf"
backup_file="/home/ehigh/DB.sql"
data_dir="/var/lib/mysql"
if [ $(id -u) -ne 0 ];then
echo "需要root用户执行"
exit 1
fi
# Force recovery
echo "innodb_force_recovery=6" >> ${conf_file}
if systemctl restart mysql.service &> /dev/null ;then
echo "在强制恢复模式下启动"
else
echo "启动失败";exit 1
fi
# Backup
mysqldump -u${user} -p${passwd} -A -F --single-transaction --flush-privileges --default-character-set=utf8 --hex-blob > ${backup_file}
echo "数据备份完成";
# Remove the force recovery option
sed -i '/innodb_force_recovery=6/d' ${conf_file}
if systemctl stop mysql.service &> /dev/null ; then
echo "mysql 停止成功"
else
kill -9 $(pidof mysqld)
fi
mv ${data_dir} ${data_dir}-bak-$(date +"%F_%T")
mkdir ${data_dir}
chown mysql:mysql ${data_dir}
if [ -d ${data_dir} ];then
echo "数据目录已重命名,新的数据目录已创建"
else
echo "创建数据目录失败";exit 1
fi
if mysqld --initialize-insecure ;then
echo "Mysql 初始化成功";
else
echo "MySQL初始化失败";exit 1
fi
if systemctl restart mysql &> /dev/null ;then
echo "mysql 重启成功";
else
echo "mysql 重启失败";exit 1
fi
if [ ! -s ${backup_file} ]; then
echo "备份文件为空";exit 1
fi
if mysql -uroot < ${backup_file} ;then
echo "Mysql 数据恢复成功";
else
echo "Mysql 数据恢复失败";exit 1
fi
# Restart MySQL
if systemctl restart mysql.service &> /dev/null ; then
echo "mysql数据恢复后重启成功"
else
echo “mysql 数据恢复后重启失败”
fi
标签:脚本,MySQL,echo,断电,exit,file,mysql,fi,else
From: https://www.cnblogs.com/heyongshen/p/17697554.html