看到网上很多Docker环境下MySQL 8.0的xtrabackup都是通过docker compose的方式来进行备份的,个人觉得太麻烦了,于是通过修改MySQL Docker镜像的方式来扩展备份功能
第一部分 MySQL8.0功能扩展
1.MySQL 8.0官方镜像扩展xtrabackup,Dockerfile如下
FROM docker.io/mysql:8.0.34-oracle
RUN microdnf install -y rsync perl-DBD-MySQL perl-Digest-MD5 procps libev && microdnf clean all
RUN rpm -ivh https://repo.percona.com/yum/release/8.0/RPMS/x86_64/percona-xtrabackup-80-8.0.34-29.1.el8.x86_64.rpm
2.打包镜像
docker build -t mysql:8.0.34-oracle-with-xtrabackup .
第二部分 增量备份
1.增量备份脚本
#!/bin/bash
#创建备份用户
# create user backup identified with mysql_native_password by "123456";
# GRANT SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW, EVENT, TRIGGER, PROCESS ON *.* TO 'backup'@'%';
EXEC="docker exec -it `docker ps |grep mysql|awk '{print \$1}'` bash -c "
SOCK=/var/run/mysqld/mysqld.sock
USER=root
PASS=123456
WEEK=$(date +%w)
DAY=$(date +%F)
YESTERDAY=$(date -d yesterday +%F)
days=7
BASE_BAKDIR=/data/backup
BAKDIR=${BASE_BAKDIR}/${DAY}
INCDIR=${BASE_BAKDIR}/${YESTERDAY}
function backup_all(){
BAKDIR=$1
mkdir -p ${BAKDIR}
#rm -rf ${BAKDIR}/*
${EXEC} "xtrabackup --backup --extra-lsndir=$BAKDIR -S $SOCK -u$USER -p$PASS --stream=xbstream | gzip > $BAKDIR/backup.stream.gz"
}
function backup_inc(){
BAKDIR=$1
INCDIR=$2
mkdir -p ${BAKDIR}
#rm -rf ${BAKDIR}/*
${EXEC} "xtrabackup --backup --history --incremental-basedir=${INCDIR} --extra-lsndir=$BAKDIR -S $SOCK -u$USER -p$PASS --stream=xbstream | gzip > $BAKDIR/backup.stream.gz"
}
if [[ ${WEEK} -eq 0 || ${WEEK} -eq 3 ]];then
backup_all ${BAKDIR}
else
if [ ! -d ${INCDIR} ];then
backup_all ${BAKDIR}
else
backup_inc ${BAKDIR} ${INCDIR}
fi
fi
find ${BASE_BAKDIR} -name "*.gz" -type f -mtime +$days -exec rm {} \;
标签:8.0,--,备份,BAKDIR,xtrabackup,MySQL,backup
From: https://blog.51cto.com/fengwan/9112411