Part01-基础概念与理论知识
1.1 物理备份概述
物理备份是指直接复制MySQL数据库的物理文件,包括数据文件、日志文件等。物理备份的优点是备份和恢复速度快,适用于大型数据库。风哥教程参考MySQL官方文档Backup and Recovery部分的物理备份相关内容。更多视频教程www.fgedu.net.cn
# ls -la /mysql/data/
drwxr-xr-x. 6 mysql mysql 4096 Apr 1 10:00 .
drwxr-xr-x. 3 root root 4096 Mar 31 15:00 ..
-rw-r—–+ 1 mysql mysql 56 Apr 1 10:00 auto.cnf
-rw——-+ 1 mysql mysql 1679 Apr 1 10:00 ca-key.pem
-rw-r–r–+ 1 mysql mysql 1112 Apr 1 10:00 ca.pem
-rw-r—–+ 1 mysql mysql 1679 Apr 1 10:00 client-cert.pem
-rw-r—–+ 1 mysql mysql 1679 Apr 1 10:00 client-key.pem
-rw-r—–+ 1 mysql mysql 502 Apr 1 10:00 ib_buffer_pool
-rw-r—–+ 1 mysql mysql 12M Apr 1 10:00 ibdata1
-rw-r—–+ 1 mysql mysql 48M Apr 1 10:00 ib_logfile0
-rw-r—–+ 1 mysql mysql 48M Apr 1 10:00 ib_logfile1
-rw-r—–+ 1 mysql mysql 48M Apr 1 10:00 ibtmp1
drwxr-x—+ 2 mysql mysql 4096 Apr 1 10:00 mysql
drwxr-x—+ 2 mysql mysql 4096 Apr 1 10:00 performance_schema
drwxr-x—+ 2 mysql mysql 4096 Apr 1 10:00 sys
drwxr-x—+ 2 mysql mysql 4096 Apr 1 10:00 fgedudb
1.2 物理备份类型
物理备份可以分为冷备份、热备份和温备份三种类型。不同类型的物理备份有不同的特点和适用场景。学习交流加群风哥微信: itpux-com
1.3 物理备份特点
物理备份的特点包括:备份速度快、恢复速度快、占用空间大、跨平台性差、对存储设备有要求等。学习交流加群风哥QQ113257174
# du -sh /mysql/data/
50G /mysql/data/
# 查看单个数据库文件大小
# du -sh /mysql/data/fgedudb/
20G /mysql/data/fgedudb/
Part02-生产环境规划与建议
2.1 物理备份规划
在生产环境中,需要根据数据库大小、业务需求、存储资源等因素,规划合理的物理备份策略。风哥提示:生产环境中应优先选择热备份方式,使用专业的备份工具如xtrabackup,以减少对业务的影响。
2.2 存储规划
物理备份的存储规划包括存储介质选择、存储容量规划、存储位置等方面。合理的存储规划可以确保备份数据的安全和可恢复性。更多学习教程公众号风哥教程itpux_com
– 存储介质:SSD(主备份)+ 磁带(归档备份)
– 存储容量:至少为数据库大小的2-3倍
– 存储位置:本地存储(近期备份)+ 异地存储(所有备份)
– 存储期限:完全备份保留30天,增量备份保留7天
2.3 性能影响评估
物理备份会对MySQL的性能产生一定影响,主要体现在I/O操作和CPU使用率上。在实施前需要评估性能影响,并采取相应的优化措施。from MySQL:www.itpux.com
# 备份前
# iostat -x 1 10
Linux 5.14.0-362.el9.x86_64 (fgedu.net.cn) 04/01/2026 _x86_64_ (16 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
10.00 0.00 5.00 2.00 0.00 83.00
device r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
nvme0n1 10.00 5.00 0.20 0.10 42.67 0.10 6.67 5.00 10.00 2.00 3.00
# 备份中
# iostat -x 1 10
Linux 5.14.0-362.el9.x86_64 (fgedu.net.cn) 04/01/2026 _x86_64_ (16 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
15.00 0.00 8.00 12.00 0.00 65.00
device r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
nvme0n1 100.00 20.00 4.00 0.50 76.80 1.20 10.00 8.00 20.00 4.00 48.00
Part03-生产环境项目实施方案
3.1 xtrabackup工具安装
xtrabackup是Percona公司开发的一款开源物理备份工具,支持热备份、增量备份等功能,是MySQL物理备份的首选工具。
# 方法1:使用yum安装
# yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
# percona-release enable-only tools release
# yum install -y percona-xtrabackup-80
# 方法2:源码编译安装
# wget https://www.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.4.0/binary/tarball/percona-xtrabackup-8.4.0-Linux-x86_64.tar.gz
# tar -xvf percona-xtrabackup-8.4.0-Linux-x86_64.tar.gz
# mv percona-xtrabackup-8.4.0-Linux-x86_64 /usr/local/xtrabackup
# ln -s /usr/local/xtrabackup/bin/* /usr/bin/
# 验证xtrabackup安装
# xtrabackup –version
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
3.2 完全备份配置
完全备份是指备份所有数据库文件,是物理备份的基础。使用xtrabackup进行完全备份的配置步骤如下。
# vi /mysql/scripts/xtrabackup_full.sh
#!/bin/bash
# xtrabackup_full.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
BACKUP_DIR=”/backup/mysql/physical/full”
DATE=$(date +%Y%m%d_%H%M%S)
DB_USER=”fgedu_admin”
DB_PASS=”StrongPassword123!”
# 创建备份目录
mkdir -p $BACKUP_DIR
# 执行完全备份
xtrabackup –backup –user=$DB_USER –password=$DB_PASS –target-dir=$BACKUP_DIR/$DATE
# 验证备份
if [ $? -eq 0 ]; then
echo “[$(date +%Y-%m-%d%H:%M:%S)] Full backup completed successfully: $BACKUP_DIR/$DATE” >> /mysql/logs/backup.log
else
echo “[$(date +%Y-%m-%d%H:%M:%S)] Full backup failed” >> /mysql/logs/backup.log
exit 1
fi
# 删除7天前的备份
find $BACKUP_DIR -type d -mtime +7 -delete
3.3 增量备份配置
增量备份是指只备份自上次备份以来变化的数据,可以节省备份时间和存储空间。使用xtrabackup进行增量备份的配置步骤如下。
# vi /mysql/scripts/xtrabackup_incremental.sh
#!/bin/bash
# xtrabackup_incremental.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
BACKUP_DIR=”/backup/mysql/physical”
FULL_BACKUP_DIR=”$BACKUP_DIR/full”
INCREMENTAL_BACKUP_DIR=”$BACKUP_DIR/incremental”
DATE=$(date +%Y%m%d_%H%M%S)
DB_USER=”fgedu_admin”
DB_PASS=”StrongPassword123!”
# 创建备份目录
mkdir -p $INCREMENTAL_BACKUP_DIR
# 获取最新的完全备份
LATEST_FULL_BACKUP=$(ls -la $FULL_BACKUP_DIR | grep -v “^total” | grep -v “^d” | tail -1 | awk ‘{print $9}’)
# 执行增量备份
xtrabackup –backup –user=$DB_USER –password=$DB_PASS –target-dir=$INCREMENTAL_BACKUP_DIR/$DATE –incremental-basedir=$FULL_BACKUP_DIR/$LATEST_FULL_BACKUP
# 验证备份
if [ $? -eq 0 ]; then
echo “[$(date +%Y-%m-%d%H:%M:%S)] Incremental backup completed successfully: $INCREMENTAL_BACKUP_DIR/$DATE” >> /mysql/logs/backup.log
else
echo “[$(date +%Y-%m-%d%H:%M:%S)] Incremental backup failed” >> /mysql/logs/backup.log
exit 1
fi
# 删除7天前的增量备份
find $INCREMENTAL_BACKUP_DIR -type d -mtime +7 -delete
Part04-生产案例与实战讲解
4.1 使用xtrabackup进行完全备份
使用xtrabackup进行完全备份是物理备份的基础,以下是具体的操作步骤。
# mkdir -p /backup/mysql/physical/full
# 步骤2:执行完全备份
# xtrabackup –backup –user=fgedu_admin –password=StrongPassword123! –target-dir=/backup/mysql/physical/full/20260401_120000
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup: The target directory is not empty.
xtrabackup: WARNING: The target directory exists. xtrabackup will remove all existing files in the target directory before backup.
xtrabackup: xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /mysql/data
xtrabackup: open files limit requested 0, set to 1048576
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Starting backup operation
[01] Copying /mysql/data/ibdata1 to /backup/mysql/physical/full/20260401_120000/ibdata1
[01] …done
[01] Copying /mysql/data/fgedudb/ibd2_16188_1927968463_0.ibd to /backup/mysql/physical/full/20260401_120000/fgedudb/ibd2_16188_1927968463_0.ibd
[01] …done
[01] Copying /mysql/data/fgedudb/fgedu_users.ibd to /backup/mysql/physical/full/20260401_120000/fgedudb/fgedu_users.ibd
[01] …done
[01] Copying /mysql/data/fgedudb/fgedu_orders.ibd to /backup/mysql/physical/full/20260401_120000/fgedudb/fgedu_orders.ibd
[01] …done
[01] Copying /mysql/data/mysql/gtid_executed.ibd to /backup/mysql/physical/full/20260401_120000/mysql/gtid_executed.ibd
[01] …done
[01] Copying /mysql/data/ib_buffer_pool to /backup/mysql/physical/full/20260401_120000/ib_buffer_pool
[01] …done
[01] Copying /mysql/data/ibtmp1 to /backup/mysql/physical/full/20260401_120000/ibtmp1
[01] …done
xtrabackup: creating checkpoint.
xtrabackup: Validating the checkpoint between 4654321 and 4654350.
xtrabackup: Creating zipped log /backup/mysql/physical/full/20260401_120000/xtrabackup_logfile.qp
xtrabackup: Transaction log of lsn (4654321) to (4654350) was copied.
xtrabackup: completed OK!
# 步骤3:查看备份文件
# ls -la /backup/mysql/physical/full/20260401_120000/
drwxr-xr-x. 5 root root 4096 Apr 1 12:00 .
drwxr-xr-x. 3 root root 4096 Apr 1 11:59 ..
drwxr-x—+ 2 root root 4096 Apr 1 12:00 fgedudb
drwxr-x—+ 2 root root 4096 Apr 1 12:00 mysql
drwxr-x—+ 2 root root 4096 Apr 1 12:00 performance_schema
drwxr-x—+ 2 root root 4096 Apr 1 12:00 sys
-rw-r—–+ 1 root root 12M Apr 1 12:00 ibdata1
-rw-r—–+ 1 root root 48M Apr 1 12:00 ib_logfile0
-rw-r—–+ 1 root root 48M Apr 1 12:00 ib_logfile1
-rw-r—–+ 1 root root 48M Apr 1 12:00 ibtmp1
-rw-r—–+ 1 root root 502 Apr 1 12:00 ib_buffer_pool
-rw-r—–+ 1 root root 187 Apr 1 12:00 xtrabackup_checkpoints
-rw-r—–+ 1 root root 511 Apr 1 12:00 xtrabackup_info
-rw-r—–+ 1 root root 2.5M Apr 1 12:00 xtrabackup_logfile.qp
4.2 使用xtrabackup进行增量备份
使用xtrabackup进行增量备份可以节省备份时间和存储空间,以下是具体的操作步骤。
# mkdir -p /backup/mysql/physical/incremental
# 步骤2:执行增量备份
# xtrabackup –backup –user=fgedu_admin –password=StrongPassword123! –target-dir=/backup/mysql/physical/incremental/20260401_130000 –incremental-basedir=/backup/mysql/physical/full/20260401_120000
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup: The target directory is not empty.
xtrabackup: WARNING: The target directory exists. xtrabackup will remove all existing files in the target directory before backup.
xtrabackup: xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /mysql/data
xtrabackup: open files limit requested 0, set to 1048576
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Starting backup operation
xtrabackup: using incremental backup mode
xtrabackup: checkpoint LSN of the base backup: 4654321
xtrabackup: using the following InnoDB configuration for the second pass:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Waiting for redo log to be flushed…
xtrabackup: Copying ./ibdata1 to /backup/mysql/physical/incremental/20260401_130000/ibdata1.delta
xtrabackup: Generating a list of tablespaces
[01] Copying /mysql/data/fgedudb/fgedu_users.ibd to /backup/mysql/physical/incremental/20260401_130000/fgedudb/fgedu_users.ibd.delta
[01] …done
[01] Copying /mysql/data/fgedudb/fgedu_orders.ibd to /backup/mysql/physical/incremental/20260401_130000/fgedudb/fgedu_orders.ibd.delta
[01] …done
xtrabackup: creating checkpoint.
xtrabackup: Validating the checkpoint between 4656789 and 4656818.
xtrabackup: Creating zipped log /backup/mysql/physical/incremental/20260401_130000/xtrabackup_logfile.qp
xtrabackup: Transaction log of lsn (4656789) to (4656818) was copied.
xtrabackup: completed OK!
# 步骤3:查看增量备份文件
# ls -la /backup/mysql/physical/incremental/20260401_130000/
drwxr-xr-x. 5 root root 4096 Apr 1 13:00 .
drwxr-xr-x. 3 root root 4096 Apr 1 12:59 ..
drwxr-x—+ 2 root root 4096 Apr 1 13:00 fgedudb
drwxr-x—+ 2 root root 4096 Apr 1 13:00 mysql
drwxr-x—+ 2 root root 4096 Apr 1 13:00 performance_schema
drwxr-x—+ 2 root root 4096 Apr 1 13:00 sys
-rw-r—–+ 1 root root 1.2M Apr 1 13:00 ibdata1.delta
-rw-r—–+ 1 root root 187 Apr 1 13:00 xtrabackup_checkpoints
-rw-r—–+ 1 root root 511 Apr 1 13:00 xtrabackup_info
-rw-r—–+ 1 root root 1.8M Apr 1 13:00 xtrabackup_logfile.qp
4.3 物理备份恢复
物理备份恢复是确保备份有效性的重要环节,以下是使用xtrabackup进行恢复的具体步骤。
# xtrabackup –prepare –target-dir=/backup/mysql/physical/full/20260401_120000
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup: xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
xtrabackup: cd to /backup/mysql/physical/full/20260401_120000
xtrabackup: This target seems to be already prepared. xtrabackup will continue with the apply-log operation.
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Starting to apply log to the backup
xtrabackup: The log was applied successfully.
xtrabackup: completed OK!
# 步骤2:应用增量备份(如果有)
# xtrabackup –prepare –target-dir=/backup/mysql/physical/full/20260401_120000 –incremental-dir=/backup/mysql/physical/incremental/20260401_130000
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup: xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
xtrabackup: cd to /backup/mysql/physical/full/20260401_120000
xtrabackup: This target seems to be already prepared. xtrabackup will continue with the apply-log operation.
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir =
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Starting to apply incremental data from /backup/mysql/physical/incremental/20260401_130000
xtrabackup: Using InnoDB as the storage engine.
xtrabackup: Generating a list of tablespaces
xtrabackup: Applying /backup/mysql/physical/incremental/20260401_130000/ibdata1.delta to ./ibdata1
xtrabackup: Applying /backup/mysql/physical/incremental/20260401_130000/fgedudb/fgedu_users.ibd.delta to ./fgedudb/fgedu_users.ibd
xtrabackup: Applying /backup/mysql/physical/incremental/20260401_130000/fgedudb/fgedu_orders.ibd.delta to ./fgedudb/fgedu_orders.ibd
xtrabackup: Starting to apply log to the backup
xtrabackup: The log was applied successfully.
xtrabackup: completed OK!
# 步骤3:停止MySQL服务
# systemctl stop mysqld
# 步骤4:清理数据目录
# rm -rf /mysql/data/*
# 步骤5:恢复备份
# xtrabackup –copy-back –target-dir=/backup/mysql/physical/full/20260401_120000
xtrabackup: recognized server arguments: –datadir=/mysql/data –tmpdir=/tmp
xtrabackup: xtrabackup version 8.4.0 based on MySQL server 8.4.0 Linux (x86_64) (revision id: 12345678)
xtrabackup: cd to /backup/mysql/physical/full/20260401_120000
xtrabackup: Target directory: /mysql/data
xtrabackup: Copying ./ibdata1 to /mysql/data/ibdata1
xtrabackup: Copying ./fgedudb/ibd2_16188_1927968463_0.ibd to /mysql/data/fgedudb/ibd2_16188_1927968463_0.ibd
xtrabackup: Copying ./fgedudb/fgedu_users.ibd to /mysql/data/fgedudb/fgedu_users.ibd
xtrabackup: Copying ./fgedudb/fgedu_orders.ibd to /mysql/data/fgedudb/fgedu_orders.ibd
xtrabackup: Copying ./mysql/gtid_executed.ibd to /mysql/data/mysql/gtid_executed.ibd
xtrabackup: Copying ./ib_buffer_pool to /mysql/data/ib_buffer_pool
xtrabackup: Copying ./ibtmp1 to /mysql/data/ibtmp1
xtrabackup: Creating directory /mysql/data/sys
xtrabackup: Creating directory /mysql/data/performance_schema
xtrabackup: Creating directory /mysql/data/mysql
xtrabackup: Creating directory /mysql/data/fgedudb
xtrabackup: completed OK!
# 步骤6:修改文件权限
# chown -R mysql:mysql /mysql/data/
# 步骤7:启动MySQL服务
# systemctl start mysqld
# 步骤8:验证恢复结果
mysql> SELECT * FROM fgedudb.fgedu_users LIMIT 10;
+—-+——+——————+————-+
| id | name | id_card | phone |
+—-+——+——————+————-+
| 1 | 张三 | 110101199001011234 | 13812345678 |
| 2 | 李四 | 310101199102022345 | 13987654321 |
| 3 | 王五 | 440101199203033456 | 13765432109 |
+—-+——+——————+————-+
3 rows in set (0.00 sec)
Part05-风哥经验总结与分享
通过多年的MySQL数据库管理经验,我总结了以下关于MySQL物理备份的关键点:
1. 工具选择:优先选择专业的物理备份工具,如xtrabackup,它支持热备份、增量备份等功能,是MySQL物理备份的首选工具。
2. 备份策略:采用完全备份和增量备份的组合,以平衡备份时间、存储空间和恢复时间。
3. 存储规划:合理规划备份存储,包括存储介质、存储容量、存储位置等,确保备份数据的安全。
4. 性能优化:在备份过程中,注意优化I/O性能,如使用SSD存储、调整备份参数等,减少对业务的影响。
5. 备份验证:定期验证备份的完整性,确保备份能够成功恢复。
6. 恢复演练:定期进行恢复演练,熟悉恢复流程,确保在实际灾难发生时能够快速响应。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
