基于XtraBackup的备份有效性检查脚本
生产环境的MySQL是通过crontab的方式,定时调度热备脚本备份数据。目前是通过XtraBackup软件实现热备。关于热备脚本方面,请查看我原先的博客《使用shell实现mysql自动全备、增备&日志备份》:http://linzhijian.blog.51cto.com/1047212/1891745 ,这里不再展开说明。
备份存放:通过XtraBackup的流式备份,将备份异地存放到备份服务器上。
备份策略:周日全备,周一到周六增备。
目前缺漏:这些备份数据未能实现有效性检查,无法探知这些备份是否具有可用性,需要通过一定的机制实现有效性检测。
目前在备份机的备份文件列表如下:
drwxr-xr-x 18 mysql mysql 4096 Apr 9 03:28 mysql01_20170409_023001_full drwxr-xr-x 18 mysql mysql 4096 Apr 10 03:25 mysql01_20170410_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 11 03:26 mysql01_20170411_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 12 03:25 mysql01_20170412_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 13 03:26 mysql01_20170413_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 14 03:26 mysql01_20170414_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 15 03:27 mysql01_20170415_023001_incr drwxr-xr-x 18 mysql mysql 4096 Apr 16 03:29 mysql01_20170416_023001_full drwxr-xr-x 18 mysql mysql 4096 Apr 17 03:26 mysql01_20170417_023001_incr
其中full结尾的说明当天是全备的,incr结尾的说明当天是增备的。
脚本实现逻辑:自动恢复全备数据,并依次恢复其余的增备数据到全备数据中,最后将恢复完毕的全备数据用mysqld拉起来,检查MySQL的错误日志是否有异常报错来判断恢复是否正常。
vim dbrecover.sh
#!/bin/sh if [ $# -ne 1 ] then echo "usage: `basename $0` [mysql01|mysql02]" exit 1 fi hostname=$1 today=`date +%Y%m%d` sh /home/mysql/shell/mysql_recover.sh $hostname $today
vim mysql_recover.sh
#!/bin/sh if [ $# -ne 2 ] then echo "usage: `basename $0` [mysql01|mysql02|mysql03] 20170501 " exit 1 fi hostname=$1 #hostname="mysql02" #today=`date +%Y%m%d` today=$2 #week=`date +%w` week=`date -d $today +%w` time1=`date +%s` timestamp=`date +%Y%m%d%H%M%S` logdir="/home/mysql/log/mysqlrecoverlog/$hostname/$timestamp" dir="/mysqlbackup/databak/$hostname/" fullname="$dir/full_backup_file.txt" incrname="$dir/incr_backup_file.txt" datadir=`grep datadir /etc/my.cnf|awk -F \= '{print $NF}'` errlog=`grep log-error /etc/my.cnf|awk -F \= '{print $NF}'` n1="0" ##周几做热备,周一到周六为1~6,周日为0。 n2="6" ##周几最后一次增备,周一到周六为1~6,周日为0。 mkdir $logdir ##创建日志目录 function getdir() { if [ $week -eq "$n1" ] then fulldir=`find /mysqlbackup/databak/${hostname}/ -type d -name "*${today}*full"` #/mysqlbackup/databak/mysql01/mysql01_20170430_023001_full if [ ! -n "$fulldir" ] then echo "the fulldir not exist!!!" >> $logdir/recover_${timestamp}.log exit 1 fi num=`find /mysqlbackup/databak/${hostname}/ -type d -name "*${today}*full"|wc -l` #/mysqlbackup/databak/mysql01/mysql01_20170429_023001_incr if [ $num -eq "1" ] then echo $fulldir > $fullname else echo "there are not only full dbbackup in $today, please check!!!" >> $logdir/recover_${timestamp}.log exit 1 fi else incrdir=`find /mysqlbackup/databak/${hostname}/ -type d -name "*${today}*incr"` if [ ! -n "$incrdir" ] then echo "the incrdir not exist!!!" >> $logdir/recover_${timestamp}.log fi num=`find /mysqlbackup/databak/${hostname}/ -type d -name "*${today}*incr"|wc -l` if [ $num -eq "1" ] then echo $incrdir > $incrname else echo "there are not only incr dbbackup in $today, please check!!!" >> $logdir/recover_${timestamp}.log exit 1 fi fi } function uncompress() { dir=$1 /usr/bin/innobackupex --decompress --parallel=8 $dir >>$logdir/uncompress_${timestamp}.log 2>&1 success_flag=`cat $logdir/uncompress_${timestamp}.log|grep "completed OK"` if [ -n success_flag ] then echo "$dir decompress sucessfully!" >> $logdir/recover_${timestamp}.log else echo "$dir decompress failed " >> $logdir/recover_${timestamp}.log exit 1 fi } function full_recover() { fullbakdir=$1 uncompress $fullbakdir /usr/bin/innobackupex --use-memory=2G --apply-log --redo-only $fullbakdir >>$logdir/full_recover_${timestamp}.log 2>&1 success_flag=`cat $logdir/full_recover_${timestamp}.log|grep "innobackupex: completed OK"` if [ -n "$success_flag" ] then echo "the full dbbackup $fullbakdir recovery is success!" >> $logdir/recover_${timestamp}.log else echo "the full dbbackup $fullbakdir recovery is fail!" >> $logdir/recover_${timestamp}.log exit 1 fi } function incr_recover() { incrbakdir=$1 fullbakdir=$2 uncompress $incrbakdir uncompress $fullbakdir if [ $week -ne "$n2" ] then /usr/bin/innobackupex --use-memory=2G --apply-log --redo-only --incremental-dir=$incrbakdir $fullbakdir >>$logdir/incr_recover_${timestamp}.log 2>&1 else /usr/bin/innobackupex --use-memory=2G --apply-log --incremental-dir=$incrbakdir $fullbakdir >>$logdir/incr_recover_${timestamp}.log 2>&1 fi success_flag=`cat $logdir/incr_recover_${timestamp}.log|grep "innobackupex: completed OK"` if [ -n "$success_flag" ] then echo "the incr dbbackup $incrbakdir recovery is success!" >> $logdir/recover_${timestamp}.log else echo "the incr dbbackup $incrbakdir recovery is fail!" >> $logdir/recover_${timestamp}.log exit 1 fi } function mysqlrecover() { fullbakdir=$1 #uncompress $fullbakdir rm -fr $datadir /bin/ln -s $fullbakdir $datadir chown -R mysql:mysql $datadir chown -R mysql:mysql $fullbakdir /sbin/service mysqld start error_flag=`grep -i error $errlog` if [ -z "$error_flag" ] then echo "the mysqld don't report error, mysql recover is success!" >> $logdir/recover_${timestamp}.log else echo "the mysqld report error, mysql recover is fail, please check!" >> $logdir/recover_${timestamp}.log exit 1 fi /sbin/service mysqld stop } getdir if [ $week -eq "$n1" ] then full=`cat $fullname` full_recover $full else incr=`cat $incrname` full=`cat $fullname` incr_recover $incr $full if [ $week -eq "$n2" ] then mysqlrecover $full fi fi time2=`date +%s` times=$((${time2}-${time1})) echo "it takes $times seconds to finish the recover!!!" >> $logdir/recover_${timestamp}.log
备份机目前采用二进制包安装MySQL的方式,直接上传到/usr/local/mysql目录上,配置好相应的/etc/profile和/etc/my.cnf即可。
cat /etc/my.cnf
[mysqld] datadir=/mysqlbackup/mysql_test socket=/mysqlbackup/mysql_test/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/mysqlbackup/mysql_test/mysqld.log pid-file=/mysqlbackup/mysql_test/mysqld.pid
生成的日志文件如下:
-rw-r--r-- 1 root root 613918 May 4 09:34 incr_recover_20170504092501.log -rw-r--r-- 1 root root 307 May 4 09:34 recover_20170504092501.log -rw-r--r-- 1 root root 259310 May 4 09:26 uncompress_20170504092501.log
其中:
uncompress_20170504092501.log:解压备份文件时产生的日志信息
incr_recover_20170504092501.log:XtraBackup应用备份文件时产生的日志信息
recover_20170504092501.log:当次恢复备份文件记录的简要日志信息
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
docker—适用于中小企业的生产、测试、开发环境
一、使用 Docker 搭建 Tomcat 运行环境 1 Docker与虚拟机 2 搭建过程 2.1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS 内核: # uname -r 2.2 安装Docker # yum install docker 可使用以下命令,查看 Docker 是否安装成功: # docker version 若输出了 Docker 的版本号,则说明安装成功了,可通过以下命令启动 Docker 服务: # systemctl start docker.service 一旦 Docker 服务启动完毕,就可以开始使用 Docker 了。 2.3 下载镜像 以 CentOS 为例,下载一个 CentOS 的镜像: # docker pull centos:7.2.1511 下载完成后,使用命令查看本地镜像列表: # docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE...
- 下一篇
VMware vSphere 5/6 创建共享磁盘
今天搭建RAC环境,想要让RAC的共享磁盘使用存储设备的磁盘空间,所以在搭建vmware vsphere5下的RAC中创建一个共享磁盘,避免以后遗忘,做一下简单的记录,希望对大家也会有一定的帮助; 以下为相关创建步骤,摘抄下来供大家学习。 To create one 20GB datastore for a virtual machine1. In vSphere Client, select a virtual machine.2. Right-click the name of the virtual machine and select Edit Settings to display the Virtual Machine Properties pane.3. Click Add to display the Add Hardware wizard.4. In the Add Hardware wizard select Hard Disk for the Device Type, and click Next.5. In the Disk section of the Sel...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8安装Docker,最新的服务器搭配容器使用
- MySQL8.0.19开启GTID主从同步CentOS8
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS7设置SWAP分区,小内存服务器的救世主