Files
www.colben.cn/content/post/mysql-backup.md
2026-04-21 22:24:02 +08:00

212 lines
5.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "mysql/mariadb 备份"
date: 2019-10-30T00:54:15+08:00
lastmod: 2026-04-21T19:52:00+08:00
tags: ["xtrabackup", "mariabackup", "mysql-shell", "备份", "mysql", "mariadb"]
categories: ["database"]
---
## xtrabackup
### 环境
- mysql 8.4
- rockylinux 9.6
### 安装
- [下载与操作系统和 mysql 版本对应的通用二进制包](https://www.percona.com/downloads/),解压
```bash
tar zxf percona-xtrabackup-8.4.0-5-Linux-x86_64.glibc2.34-minimal.tar.gz
mv percona-xtrabackup-8.4.0-5-Linux-x86_64.glibc2.34-minimal /opt/pxb
```
### 创建备份用户和目录
- **备份目录如果是 nfs挂载时需指定 sync 选项**
- 创建备份用户
```sql
CREATE USER xb@localhost IDENTIFIED BY 'Xtrabackup_1234';
GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO xb@localhost;
GRANT SELECT ON performance_schema.log_status TO xb@localhost;
GRANT SELECT ON performance_schema.keyring_component_status TO xb@localhost;
GRANT SELECT ON performance_schema.replication_group_members TO xb@localhost;
```
### 全量备份和恢复
- 备份
```bash
/opt/pxb/bin/xtrabackup --backup --target-dir=/backup/ \
--user=xb --password=Xtrabackup_1234
```
- 准备
```bash
/opt/pxb/bin/xtrabackup --prepare --target-dir=/backup/
```
- 恢复,**提前停止 mysql**
```bash
/opt/pxb/bin/xtrabackup --move-back --target-dir=/backup/
chown -R mysql:mysql /var/lib/mysql
```
### 增量备份和恢复
- 先全量备份一次
```bash
/opt/pxb/bin/xtrabackup --backup --target-dir=/backup/full \
--user=xb --password=Xtrabackup_1234
```
- 创建增量备份
```bash
/opt/pxb/bin/xtrabackup --backup --target-dir=/backup/incre \
--incremental-basedir=/backup/full \
--user=xb --password=Xtrabackup_1234
```
- 准备全量备份,**注意这里指定"--apply-log-only"避免事务回滚**
```bash
/opt/pxb/bin/xtrabackup --prepare --apply-log-only --target-dir=/backup/full
```
- 准备增量备份,**准备最后一个增量备份无需指定"--apply-log-only"**
```bash
/opt/pxb/bin/xtrabackup --prepare --target-dir=/backup/full \
--incremental-dir=/data/backups/incre
```
- 恢复,**提前停止 mysql**
```bash
/opt/pxb/bin/xtrabackup --move-back --target-dir=/backup/full
chown -R mysql:mysql /var/lib/mysql
```
---
## mariabackup
### 环境
- mariadb 10.3
- rockylinux 8.10
### 安装
```bash
dnf install mariadb-backup
```
### 创建备份用户
```sql
CREATE USER backup@localhost IDENTIFIED BY 'Backup_1234';
GRANT SELECT, RELOAD, PROCESS, REPLICATION CLIENT ON *.* TO backup@localhost;
```
### 全量备份和恢复
- 备份
```bash
mariabackup --backup --target-dir=/backup/ \
--user=backup --password=Backup_1234
```
- 准备
```bash
mariabackup --prepare --target-dir=/backup/
```
- 恢复,**提前停止 mariadb**
```bash
mariabackup --move-back --target-dir=/backup/
chown -R mysql:mysql /var/lib/mysql
```
### 增量备份和恢复
- 先全量备份一次
```bash
mariabackup --backup --target-dir=/backup/full \
--user=backup --password=Backup_1234
```
- 创建增量备份
```bash
mariabackup --backup --target-dir=/backup/incre \
--incremental-basedir=/backup/full \
--user=backup --password=Backup_1234
```
- 准备全量备份
```bash
mariabackup --prepare --target-dir=/backup/full
```
- 准备增量备份
```bash
mariabackup --prepare --target-dir=/backup/full \
--incremental-dir=/data/backups/incre
```
- 恢复,**提前停止 mariadb**
```bash
/opt/pxb/bin/xtrabackup --move-back --target-dir=/backup/full
chown -R mysql:mysql /var/lib/mysql
```
---
## mysqlsh
### 环境
- mysql 8.4
- rockylinux 9.6
### 安装
- [下载最新的通用二进制包](https://dev.mysql.com/downloads/shell/),解压
```bash
tar zxf mysql-shell-8.4.9-linux-glibc2.28-x86-64bit.tar.gz
mv mysql-shell-8.4.9-linux-glibc2.28-x86-64git /opt/mysql-shell
```
### 创建备份用户
```sql
CREATE USER shell@'%' IDENTIFIED BY 'Shell_1234';
GRANT PROCESS, RELOAD, LOCK TABLES ON *.* TO shell@localhost;
```
### 备份命令
- 简化 msyql-shell 命令
```bash
export MYSQLSH="/opt/mysql-shell -h 'mysql-ip' -P 3306 -u shell -p 'Shell_1234'"
# 替换其中的 'mysql-ip'
```
- 备份实例
```bash
$MYSQLSH -e 'util.dumpInstance("/data/backup/full", {compression: "zstd"})'
# includeSchemas/excludeSchemas: ["db1", "db2"], 指定/忽略备份某些库
# includeTables/excludeTables: ["db1.tb1", "db2.tb2"], 指定/忽略备份某些库
# routines/users/triggers: true, 备份函数和存储过程/账号/触发器
```
- 备份指定库
```bash
$MYSQLSH -e 'util.dumpInstance("/data/backup/dbs", {includeSchemas: ["db1", "db2"]})'
# 或
$MYSQLSH -e 'util.dumpSchemas(["db1", "db2"], "/data/backup/dbs")'
```
- 备份指定表
```bash
$MYSQLSH -e 'util.dumpInstance("/data/backup/tbs", {includeTables: ["db1.tb1", "db2.tb2"]})'
# 或
$MYSQLSH -e 'util.dumpTables("db1", ["tb1", "tb2"], "/data/backup/db1_tbs")'
```
- 导入数据,建议在服务器本地导入
```bash
# 开启 local_infile
mysql -S /tmp/mysql.sock -uroot -p -e "set global LOCLA_INFILE=ON"
/opt/mysql-shell/bin/mysqlsh -S /tmp/mysql.sock \
-e 'util.loadDump("/data/backup/xxxx", {loadUsers: true})'
# loadUsers: true, 导入账号
```
## 参考
- https://docs.percona.com/percona-xtrabackup/8.4/index.html
- https://mp.weixin.qq.com/s/RC6MykrGbZ850xh3AOjrtw
- https://swmlee.com/2020/06/18/technicalessays/mariadbseries/08mariadb-dump-backup/