#!/bin/bash
#
#********************************************************************
#Author: qiangshu
#Tel: 18610586896
#Date: 2019-11-14
#FileName: mysql_backup.sh
#URL: http://xxxxxx
#Description: 使用xtrabackup备份和恢复mysql数据库
#测试环境为CentOS7,yum安装的mariadb-server5.5,MySQL5.7.27二进制安装成功
#Copyright (C): 2019 All rights reserved
#********************************************************************# xtrabackup 备份后数据的存储目录
base_dir=/data/backup
# mysql的数据目录,恢复的时候用
data_dir=/data/mysql
sql_user='root'
sql_host='localhost'
sql_pass='123456'
sql_login="--user=$sql_user --host=$sql_host --password=$sql_pass"#安装epel源,然后安装percona-xtrabackup,yum安装的xtrabackup版本为percona-xtrabackup-2.3.6
#不支持mysql5.7的备份,需要再percona官网下载percona-xtrabackup-24-2.4.12版本或以上版本
#判断脚本参数case $1 in
--all|-a)
#--all 手动执行全备份 [ -d $base_dir ] || mkdir -p $base_dir
xtrabackup $sql_login --backup --target-dir=$base_dir/base;;--increment|-i)
#--incrementd 手动执行增量备份end_dir=`ls $base_dir | tail -1`#如果数据目录中只有base,那么就针对base目录进行增量备份if [ "$end_dir" == "base" ];then
xtrabackup $sql_login --backup --target-dir=$base_dir/inc1 --incremental-basedir=$base_dir/base
#如果数据目录中已经含有inc的目录,那么根据inc数字最大的目录进行增量备份elif [[ "$end_dir" =~ "inc" ]];then
num=`echo $end_dir | grep -o [0-9]*`
let num_add=num+1
xtrabackup $sql_login --backup --target-dir=$base_dir/inc$num_add --incremental-basedir=$base_dir/inc$num
fi
;;--manualrecovery|-m)
#手动恢复数据,判断数据库的目录是否是空,不为空退出 [ "`ls $data_dir`" == "" ] || { echo MySQL data dir is not null !!!;exit 2; }
inc_num=`ls -d $base_dir/inc* 2> /dev/null | wc -l`#如果没有增量数据直接还原if [ "$inc_num" -eq 0 ];then
xtrabackup --prepare --target-dir=$base_dir/base
#有增量数据的时候,根据增量数据的文件夹个数,循环恢复elif [ "$inc_num" -ge 1 ];then
xtrabackup --prepare --apply-log-only --target-dir=$base_dir/base
for i in `seq 1 $inc_num`;do
if [ $i -eq $inc_num ];then
xtrabackup --prepare --target-dir=$base_dir/base --incremental-dir=$base_dir/inc$i
break
fi
xtrabackup --prepare --apply-log-only --target-dir=$base_dir/base --incremental-dir=$base_dir/inc$i
done
fi
xtrabackup --copy-back --target-dir=$base_dir/base
# 修改数据目录所有文件的用户名为mysqlchown -R mysql:mysql $data_dir
;;--autobackup|b)
# 可以放在cron计划任务里,每天执行自动备份任务,周一生成全量数据,周二至周日生成增量数据
# 数据目录格式/data/backup/2019/45/1表示2019年第45周,周一year=`date +%Y`
week_number=`date +%U`
date_of_week=`date +%u`
full_dir=$base_dir/$year/$week_number
mkdir -p $base_dir/$year/$week_number
if [ $date_of_week -eq 1 ];then# 周1进行全备份 xtrabackup $sql_login --backup --target-dir=$full_dir/$date_of_week
else# 周2-7,针对前一天的数据做增量备份
let yesterday=$date_of_week-1# 前一天的目录如果不存在,证明没有备份数据[ -d $base_dir/$year/$week_number/$yesterday ] || { echo Not exist yesterday backup data !!!;exit 1; }
xtrabackup $sql_login --backup --target-dir=$full_dir/$date_of_week --incremental-basedir=$full_dir/$yesterday
fi
;;
--autorecovery|-r)
#自动备份恢复方法,使用格式如: ./backup --autorecovery 2019 45 3恢复到2019年45周第3天的数据
year=$2week_number=$3date_of_week=$4full_dir=$base_dir/$year/$week_number
# 如果日期的目录不存在,报错退出 [ -d $full_dir/$date_of_week ] || { echo Wrong input date !!!;exit 3; }
# 如果日期目录为空,报错退出 [ "`ls $full_dir/$date_of_week`" == "" ] && { echo Dir no data !!!;exit 4; }# num表示恢复到的周几
num=$date_of_week
# 如果数量为1,则只有周1的全备份,按全备份恢复if [ $num -eq 1 ];then
xtrabackup --prepare --target-dir=$full_dir/1
fi# 如果数量>=2,则有周1的全备份,还有多个备份,每个增量分别还原if [ $num -ge 2 ];then
xtrabackup --prepare --apply-log-only --target-dir=$full_dir/1
for i in `seq 2 $num`;do
if [ $i -eq $num ];then
xtrabackup --prepare --target-dir=$full_dir/1 --incremental-dir=$full_dir/$i
break
fi
xtrabackup --prepare --apply-log-only --target-dir=$full_dir/1 --incremental-dir=$full_dir/$i
done
fi
xtrabackup --copy-back --target-dir=$full_dir/1
chown -R mysql:mysql $data_dir
;;--help|-h)
cat << EOF
本脚本通过xtrabackup程序进行mysqld的备份,需要安装percona-xtrabackup-24-2.4.12或更高版本
参数选项:
--all或 -a:全备份,全备份默认路径是/data/backup/base
--increment 或 -i:增量备份,必须有全备份的情况下在进行增量备份,第一次的增量备份为/data/backup/inc1,第二次为/data/backup/inc2,以此类推
--manualrecovery 或 -m:恢复全备份和增量备份的数据,需要提前把全量备份置于/data/backup/base目录,增量1/data/backup/inc1,以此类推
--autobackup 或 -b:按照年,第几周,周几的格式备份数据。存储路径如/data/backup/2019/45/{1,2,3},判断如果是周一进行全备份,周二至周日针对前一天的备份进行增量备份。可以把脚本放到crontab任务中每天执行一次。例如:0 30 * * * /bin/bash 脚本名。
--autorecoveryh 或 -r:把--autobackup备份的数据进行还原,还原的时候需要输入年,第几周,周几。例如:script.sh --autorecovery 2019 45 2,意思是恢复到2019年第45周周2的数据
--help 或 -h:查看帮助
EOF
;;
*)
echo "use --help"
esac