xtrabackup好用,但奈何国外对ARM架构支持不是很好,官网并没有现成的ARM包,只能源码编译了。本镜像适用于MySQL5.7系列。

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
FROM centos:7

ENV PATH=${PATH}:/usr/local/xtrabackup/bin \
    TZ=Asia/Shanghai
COPY ./CMakeLists.txt  /tmp/CMakeLists.txt
RUN yum install epel-release -y  --nogpgcheck  \
    && yum clean all && yum makecache \
    && yum install --nogpgcheck -y git wget make cmake3 python3-sphinx openssl-devel libaio libaio-devel gcc gcc-c++ libaio libaio-devel automake \
    && yum install -y --nogpgcheck autoconf bison libtool ncurses-devel autoconf bison libtool libgcrypt-devel \
    && yum install -y --nogpgcheck libev-devel ncurses-devel libgcrypt-devel libev-devel libcurl-devel zlib-devel libcurl-devel vim-common \
    && cd tmp \
    && git clone https://github.com/percona/percona-xtrabackup.git \
    && cd percona-xtrabackup && git checkout 2.4 \
    && rm -rf /tmp/percona-xtrabackup/storage/innobase/CMakeLists.txt \
    && cp /tmp/CMakeLists.txt /tmp/percona-xtrabackup/storage/innobase/CMakeLists.txt \
    && cd /tmp/percona-xtrabackup && cmake3 -DWITH_MAN_PAGES=OFF -DDOWNLOAD_BOOST=ON -DWITH_BOOST=/tmp -B ./ \
    && cp /usr/include/sys/prctl.h /tmp/percona-xtrabackup/sql/ \
    && sed -i '/mysqld.daemon.h/a #include "prctl.h"'  /tmp/percona-xtrabackup/sql/mysqld.cc \
    && make \
    && make install \
    && rm -rf /tmp/* \
    && yum remove -y python3-sphinx gcc git gcc-c++ automake cmake3 make vim-common \
    && yum clean all

CMakeLists.txt

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# This is the CMakeLists for InnoDB

INCLUDE(innodb.cmake)

SET(INNOBASE_SOURCES
        api/api0api.cc
        api/api0misc.cc
        btr/btr0btr.cc
        btr/btr0cur.cc
        btr/btr0pcur.cc
        btr/btr0sea.cc
        btr/btr0bulk.cc
        buf/buf0block_hint.cc
        buf/buf0buddy.cc
        buf/buf0buf.cc
        buf/buf0dblwr.cc
        buf/buf0checksum.cc
        buf/buf0dump.cc
        buf/buf0flu.cc
        buf/buf0lru.cc
        buf/buf0rea.cc
        data/data0data.cc
        data/data0type.cc
        dict/dict0boot.cc
        dict/dict0crea.cc
        dict/dict0dict.cc
        dict/dict0load.cc
        dict/dict0mem.cc
        dict/dict0stats.cc
        dict/dict0stats_bg.cc
        eval/eval0eval.cc
        eval/eval0proc.cc
        fil/fil0fil.cc
        fsp/fsp0fsp.cc
        fsp/fsp0file.cc
        fsp/fsp0space.cc
        fsp/fsp0sysspace.cc
        fut/fut0fut.cc
        fut/fut0lst.cc
        ha/ha0ha.cc
        ha/ha0storage.cc
        ha/hash0hash.cc
        fts/fts0fts.cc
        fts/fts0ast.cc
        fts/fts0blex.cc
        fts/fts0config.cc
        fts/fts0opt.cc
        fts/fts0pars.cc
        fts/fts0que.cc
        fts/fts0sql.cc
        fts/fts0tlex.cc
        gis/gis0geo.cc
        gis/gis0rtree.cc
        gis/gis0sea.cc
        fts/fts0plugin.cc
        handler/ha_innodb.cc
        handler/ha_innopart.cc
        handler/handler0alter.cc
        handler/i_s.cc
        ibuf/ibuf0ibuf.cc
        lock/lock0iter.cc
        lock/lock0prdt.cc
        lock/lock0lock.cc
        lock/lock0wait.cc
        log/log0log.cc
        log/log0recv.cc
        mach/mach0data.cc
        mem/mem0mem.cc
        mtr/mtr0log.cc
        mtr/mtr0mtr.cc
        os/os0file.cc
        os/os0proc.cc
        os/os0event.cc
        os/os0thread.cc
        page/page0cur.cc
        page/page0page.cc
        page/page0zip.cc
        pars/lexyy.cc
        pars/pars0grm.cc
        pars/pars0opt.cc
        pars/pars0pars.cc
        pars/pars0sym.cc
        que/que0que.cc
        read/read0read.cc
        rem/rem0cmp.cc
        rem/rem0rec.cc
        row/row0ext.cc
        row/row0ftsort.cc
        row/row0import.cc
        row/row0ins.cc
        row/row0merge.cc
        row/row0mysql.cc
        row/row0log.cc
        row/row0purge.cc
        row/row0row.cc
        row/row0sel.cc
        row/row0trunc.cc
        row/row0uins.cc
        row/row0umod.cc
        row/row0undo.cc
        row/row0upd.cc
        row/row0quiesce.cc
        row/row0vers.cc
        srv/srv0conc.cc
        srv/srv0mon.cc
        srv/srv0srv.cc
        srv/srv0start.cc
        sync/sync0arr.cc
        sync/sync0rw.cc
        sync/sync0debug.cc
        sync/sync0sync.cc
        trx/trx0i_s.cc
        trx/trx0purge.cc
        trx/trx0rec.cc
        trx/trx0roll.cc
        trx/trx0rseg.cc
        trx/trx0sys.cc
        trx/trx0trx.cc
        trx/trx0undo.cc
        usr/usr0sess.cc
        ut/ut0byte.cc
        ut/ut0crc32.cc
        ut/ut0dbg.cc
        ut/ut0list.cc
        ut/ut0mem.cc
        ut/ut0new.cc
        ut/ut0rbt.cc
        ut/ut0rnd.cc
        ut/ut0ut.cc
        ut/ut0vec.cc
        ut/ut0wqueue.cc)

IF(WITH_INNODB)
  # Legacy option
  SET(WITH_INNOBASE_STORAGE_ENGINE TRUE)
ENDIF()

UNSET(NUMA_LIBRARY)
IF(HAVE_LIBNUMA)
   SET(NUMA_LIBRARY "numa")
ENDIF()

MYSQL_ADD_PLUGIN(innobase ${INNOBASE_SOURCES} STORAGE_ENGINE
  MANDATORY
  MODULE_OUTPUT_NAME ha_innodb
  LINK_LIBRARIES ${ZLIB_LIBRARY} ${LZ4_LIBRARY} ${NUMA_LIBRARY})

# Remove -DMYSQL_SERVER, it breaks embedded build
SET_TARGET_PROPERTIES(innobase PROPERTIES COMPILE_DEFINITIONS "")

IF(WITH_INNOBASE_STORAGE_ENGINE)
  ADD_DEPENDENCIES(innobase GenError)
ENDIF()

INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
# Avoid generating Hardware Capabilities due to crc32 instructions
IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
  MY_CHECK_CXX_COMPILER_FLAG("-Wa,-nH" HAVE_WA_NH)
  IF(HAVE_WA_NH)
    ADD_COMPILE_FLAGS(
      ut/ut0crc32.cc
      COMPILE_FLAGS "-Wa,-nH"
    )
  ENDIF()
ENDIF()

MY_CHECK_CXX_COMPILER_FLAG("-Wcast-function-type" HAVE_CAST_FUNCTION_TYPE)
IF(HAVE_CAST_FUNCTION_TYPE)
  ADD_COMPILE_FLAGS(fts/fts0pars.cc
    COMPILE_FLAGS "-Wno-cast-function-type")
ENDIF()

# A GCC bug causes crash when compiling these files on ARM64 with -O1+
# Compile them with -O0 as a workaround.
IF(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  # Bug was fixed in GCC 5.2, so workaround only needed < 5.2
  EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                  OUTPUT_VARIABLE GCC_VERSION)
  IF(GCC_VERSION VERSION_LESS 5.2)
    INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
    ADD_COMPILE_FLAGS(
      btr/btr0btr.cc
      btr/btr0cur.cc
      buf/buf0buf.cc
      gis/gis0sea.cc
      handler/handler0alter.cc
      mtr/mtr0mtr.cc
      row/row0merge.cc
      row/row0mysql.cc
      row/row0trunc.cc
      srv/srv0srv.cc
      fts/fts0fts.cc
      COMPILE_FLAGS "-O0"
      )
  ENDIF()
ENDIF()

# Must be after all ADD_DEFINITIONS() to be inherited by the
# 'xtrabackup' subdirectory
ADD_SUBDIRECTORY(xtrabackup)

备份所有库

备份

backup.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
export PATH
set -e

backup_date=$(date +%Y%m%d)
delete_date=$(date --date "3 days ago" +%Y%m%d)
backup_dir="/backup"
container_name="mysql-prod"
db="test"
user="root"
pass="pass"
host="10.0.2.174"
port=3306

if [ ! -d ${backup_dir}/${backup_date} ]; then
  mkdir -p ${backup_dir}/${backup_date}
fi

# Use Container
docker run --rm -i \
       --volumes-from ${container_name} \
       -v ${backup_dir}/${backup_date}:/backup \
       unknowname/xtrabackup:2.4.22 \
       xtrabackup --host=${host} \
                  --port=${port} \
                  --user=${user} \
                  # --datadir=/var/lib/mysql \  #非容器备份时需要指定datadir,默认会从/var/lib/mysql里面复制数据
                  --password=${pass} \
                  --target-dir=/backup \
                  # --databases=${db} \ #备份单库
                  --backup > out.log
if [ $(echo $?) -eq 0 ]; then
  rm -rf ${backup_dir}/${delete_date}
fi

还原

  • 应用备份集
1
2
3
4
5
6
backup_dir=/data/backup/20250102
docker run --rm  \
      -v ${backup_dir}:/backup unknowname/xtrabackup:2.4.22 \
      xtrabackup prepare  --target-dir=/backup
# 复制一份出来,防止异常污染到了原备份集
cp -r ${backup_dir}  tmp
  • 启动

  • docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mysql:
  image: mysql:5.7.30_arm
  restart: always
  container_name: mysql-tmp
  privileged: true
  ports:
    - 3308:3306
  environment:
    - TZ=Asia/Shanghai
    # 帐号验证信息为原帐号密码,这里指定无效
    # - MYSQL_ROOT_PASSWORD=password
  volumes:
    - ./tmp:/var/lib/mysql
  • 启动容器
1
docker-compose up -d

还原单库

单库要麻烦许多,它不能像所有库一样,直接将整个目录当成MySQLDATADIR

1. 总体步骤

除了在前面的备份参数增加--databases指定库外,还有备份原库的所有表结构。用于后续创建相同表结构再导入表空间。可以参考前面的backup.sh,只需要在脚本中再增加一条导出表结构即可

1
mysqldump -u${user} -p${password} --no-data --databases ${db} > all_${db}_schema.sql
  1. 导出指定库的备份文件
  2. 导出指定库的所有表结构
  3. 在临时库中创建相同的表结构,同时将表的TABLESPACE丢弃掉(DISCARD)
  4. 应用备份出来的文件
  5. 复制原备份的表空间至临时库中
  6. 导入指定表的表空间

详细步骤略