xtrabackup+MySQL8全备+增备脚本

问题描述:运用xtrabackup进行mysql全备,mysql8之前使用的是innodbxtrabackup,mysql8之后开始使用xtrabackup,innobackupex把功能都集成到xtrabackup中。

脚本在备库上实现每周末零点进行全备,周一到周六凌晨一点进行增备!

 

这里下载地址很奇怪,为什么有的人就可以打开下载列表,我这里打开就是空的

下载地址:http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.8/source/ 

 

 

空的:

xtrabackup安装步骤,解压即用,我安装的是tar包,8.0.27高版本向下mysql8.0的兼容

 

 

 

 

 

全备脚本:

mysql_full_bakcup.sh

#一主两备集群,在从库上进行备份,如果从库同步失败,备份停止,备份用户还要用查看复制状态的权限
#!/bin/bash
# MySQL backup script
# Copyright (c) 2022 hzh
# Version: 1.0
# mkdir -pv /backup/{33306,backuplog,scripts}
date=`date +%F` #当前时间变量 backupDir=/backup/33306 #备份路径 target_dir=/backup/$date #全备路径变量 xtrDir=/root/percona-xtrabackup/bin/xtrabackup #xtrabackup启动环境变量 mysqlDir=/usr/local/mysql8/bin/mysql #mysql启动环境变量 backupLog=$backupDir/backuplog #备份日志目录 mysql_host=192.168.163.21 #备份host mysql_port=33310 #端口 mysql_user=root #用户 mysql_password=123456 #密码 mysql_socket=/data/mysql10/db_dxpt10/mysql.sock #socket mysql_cnf=/data/mysql10/db_dxpt10/conf/dxpt10.cnf #配置文件 show_slave_status=`"$mysqlDir" -S"$mysql_socket" -P"$mysql_port" -u"$mysql_user" -p"$mysql_password" -A -e 'show slave status\G;' 2>/dev/null` #获取slave状态 slave_sql_running_state=`echo "$show_slave_status" |grep Slave_SQL_Running_State |awk -F ': ' '{print $2}'` #Slave上SQL运行状态 ms_status=`echo "$show_slave_status" |grep Running: |awk -F ': ' '{if($2=="Yes"){sum += 1}}; END{print sum}'` #IO、SQL线程运行状态:2表示健康,!2表示不健康 if [ $ms_status -eq 2 ]; #检查mysql复制状态 then echo =========================Run full backup beginning========================== >>$backupLog/backup_full_success.log 2>&1 $xtrDir --defaults-file=$mysql_cnf --user=$mysql_user --password=$mysql_password --socket=$mysql_socket --compress --compress-threads=2 --backup --target-dir=$target_dir >>$backupLog/backup_full_success.log 2>&1 echo =========================Run full backup finished successfully========================== >>$backupLog/backup_full_success.log 2>&1 #如果需要 发送邮件 else echo =========================Error,the synchronization may fail. Locate the cause========================== >> $backupLog/backup_failed.log 2>&1 #如果需要 发送邮件 fi #删除两周前备份 #find $backupDir -maxdepth 1 -type d -mtime +14 -exec rm -Rf {} \; >>$backupLog/deleted_record.log  2>&1

 

 增备脚本:

mysql_incremental_backup.sh

#一主两备集群,在从库上进行备份,如果从库同步失败,备份停止,备份用户还要用查看复制状态的权限
#!/bin/bash
# MySQL backup script
# Copyright (c) 2022 hzh
# Version: 1.0

date=`date +%F` datenum=`date -d "1 days ago" +%F` #当前时间变量 cur_dateTime=$(date "+%Y-%m-%d %H:%M:%S") backupDir=/backup/33306 #备份路径 target_dir=/backup/$date #全备路径变量 xtrDir=/root/percona-xtrabackup/bin/xtrabackup #xtrabackup启动环境变量 mysqlDir=/usr/local/mysql8/bin/mysql #mysql启动环境变量 backupLog=$backupDir/backuplog mysql_host=192.168.163.21 #host mysql_port=33310 #端口 mysql_user=root #用户 mysql_password=123456 #密码 mysql_socket=/data/mysql10/db_dxpt10/mysql.sock #socket mysql_cnf=/data/mysql10/db_dxpt10/conf/dxpt10.cnf #配置文件 show_slave_status=`"$mysqlDir" -S"$mysql_socket" -P"$mysql_port" -u"$mysql_user" -p"$mysql_password" -A -e 'show slave status\G;' 2>/dev/null` #获取slave状态 slave_sql_running_state=`echo "$show_slave_status" |grep Slave_SQL_Running_State |awk -F ': ' '{print $2}'` #Slave上SQL运行状态 ms_status=`echo "$show_slave_status" |grep Running: |awk -F ': ' '{if($2=="Yes"){sum += 1}}; END{print sum}'` #IO、SQL线程运行状态:2表示健康,!2表示不健康 if [ $ms_status -eq 2 ]; #检查mysql复制状态 then echo -e $cur_dateTime >>$backupLog/backup_incr_success.log 2>&1 echo =========================Run incremental backup beginning========================== >>$backupLog/backup_incr_success.log 2>&1 $xtrDir --defaults-file=$mysql_cnf --port=$mysql_port --user=$mysql_user --password=$mysql_password --socket=$mysql_socket --compress --compress-threads=2 --backup --target-dir=$target_dir --incremental-basedir=$backupDir/$datenum >> $backupLog/backup_incr_success.log 2>&1 echo -e $cur_dateTime >>$backupLog/backup_incr_success.log 2>&1 echo =========================Run incremental backup finished successfully========================== >>$backupLog/backup_incr_success.log 2>&1 #发送邮件 else echo -e $cur_dateTime >>$backupLog/backup_incr_success.log 2>&1 echo =========================Error,the synchronization may fail. Locate the cause========================== >> $backupLog/backup_incr_failed.log 2>&1 #发送邮件 fi #删除两周前备份 #find $backupDir -maxdepth 1 -type d -mtime +14 -exec rm -Rf {} \; >>$backupLog/deleted_record.log  2>&1

 

配合定时任务使用

0 0 * * 7 sh /backup/scripts/mysql_full_backup.sh
0 1 * * 1-6 sh /backup/scripts/mysql_incremental_backup.sh

 

以上脚本都是使用root用户进行备份,有些生产限制这个,要换成专门的备份用户,但是我这边测试使用备份用户全备出来的内容跟root全备的内容不一样,要不然增备的时候出岔子,所以使用了root用户代替

创建备份用户
create user backuper@'localhost' identified by 'i6+MgKFN';
GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT,SUPER ON *.* TO 'backuper'@'localhost';
grant create,insert,select on percona_schema.* to 'backuper'@'localhost';
flush privileges;

 

脚本中还是用的是qp压缩方式,如果想要解压备份,还需要安装qp解压工具

下载链接:http://www.quicklz.com/qpress-11-source.zip

 

--解压.qb文件
[root@ ~]# cd /backup/33306/备份目录 
[root@ ~]# for qp in `find . -iname "*\.qp"`; do /usr/local/bin/qpress -d $qp $(dirname $qp) && rm $qp; done 

 

文中细节,欢迎指正!

posted @ 2022-03-23 11:11  我爱睡莲  阅读(987)  评论(2编辑  收藏  举报