安装备份工具
1.安装Percona yum存储库
sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
安装过程中需要按y继续安装
2.启用Percona Server 5.7存储库
sudo percona-release setup ps57
3、安装percona-xtrabackup-24
sudo yum -y install percona-xtrabackup-24.x86_64
4、查看是否安装成功xtrabackup
[zz@zfserver-centos7 ~]$ which xtrabackup
/usr/bin/xtrabackup
[zz@zfserver-centos7 ~]$
测试环境
CentOS7
数据库路径 /var/lib/mysql
备份路径/home/zz/xtrabackup_data/
手动备份
1. 全量备份
使用以下命令创建全量备份:
xtrabackup --backup --target-dir=/home/zz/xtrabackup_data/back_data --user=root --password='*******'
2. 第一次增量备份
基于全量备份创建第一次增量备份:
xtrabackup --backup --target-dir=/home/zz/xtrabackup_data/incremental_data_1 \
--incremental-basedir=/home/zz/xtrabackup_data/back_data \
--user=root --password='*******'
3. 第二次增量备份
基于第一次增量备份创建第二次增量备份:
xtrabackup --backup --target-dir=/home/zz/xtrabackup_data/incremental_data_2 \
--incremental-basedir=/home/zz/xtrabackup_data/incremental_data_1 \
--user=root --password='*******'
4. 检查备份lsn号
每次备份后应检查lsn号,本次的from_lsn应与上次的to_lsn对应,全量备份应从0开始。示例如下:
[zz@zfserver-centos7 ~]$ sudo cat /home/zz/xtrabackup_data/back_data/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 3519241408
……
[zz@zfserver-centos7 ~]$ sudo cat /home/zz/xtrabackup_data/incremental_data_1/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 3519241408
to_lsn = 3522481940
……
[zz@zfserver-centos7 ~]$ sudo cat /home/zz/xtrabackup_data/incremental_data_2/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 3522481940
to_lsn = 3522684952
……
手动恢复
恢复是将增量备份文件应用到全量备份文件的过程。
为避免污染原始备份文件,防止恢复过程出错,应先对全量备份文件进行拷贝备份。
1. 准备全量备份
使用以下命令应用全量备份的事务日志(仅准备,未完成恢复)
xtrabackup --prepare --apply-log-only --target-dir=/home/zz/xtrabackup_data/back_data
2. 合并增量备份
按照备份顺序逐一合并增量备份到全量备份中
(1) 合并第一次增量备份
xtrabackup --prepare --apply-log-only --target-dir=/home/zz/xtrabackup_data/back_data \
--incremental-dir=/home/zz/xtrabackup_data/incremental_data_1
(2) 合并第二次增量备份
xtrabackup --prepare --apply-log-only --target-dir=/home/zz/xtrabackup_data/back_data \
--incremental-dir=/home/zz/xtrabackup_data/incremental_data_2
3. 完成备份恢复
合并所有增量备份后,完成最终事务日志的应用
xtrabackup --prepare --target-dir=/home/zz/xtrabackup_data/back_data
4. 检查lsn号
在恢复备份完成后,再次查看已经应用了增量备份的全量备份文件,可看到to_lsn与最后一次的to_lsn一致,代表应用增量备份成功。
[zz@zfserver-centos7 ~]$ sudo cat /home/zz/xtrabackup_data/back_data/xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 3522684952
……
手动恢复数据库文件
1. 停止 MySQL 服务
在拷贝之前,确保 MySQL 服务已停止:
systemctl stop mysqld
验证服务是否已停止:
systemctl status mysqld
2. 清空现有数据目录
清空 MySQL 数据目录(如 /var/lib/mysql
)中的内容:
rm -rf /var/lib/mysql/*
3. 拷贝恢复的备份文件
将已恢复增量备份数据的数据库拷贝到 MySQL 数据目录:
cp -r /home/zz/xtrabackup_data/back_data/* /var/lib/mysql
4. 修改数据目录权限
为 MySQL 数据目录设置正确权限:
chown -R mysql:mysql /var/lib/mysql
chmod -R 750 /var/lib/mysql
5. 启动 MySQL 服务
完成备份文件恢复后,重新启动 MySQL 服务:
systemctl start mysqld
Cron添加定期备份命令
假如Cron执行脚本位于:/home/zz/xtrabackup_data/backup_cron.sh
使用:
sudo crontab -e
之后添加一行
0 * * * * /home/zz/xtrabackup_data/backup_cron.sh
Cron定期备份脚本
每小时一次全量备份,在/home/zz/xtrabackup_data/创建一个对应的时间文件夹,每5分钟进行一次增量备份,这个文件夹内会包含1次全量备份还有10次增量备份,脚本会检查需要及可用的空间大小。
#!/bin/bash# 设置备份目录
BACKUP_DIR="/home/zz/xtrabackup_data"# 检查备份目录是否存在,如果不存在,则递归创建
if [ ! -d "$BACKUP_DIR" ]; thenecho "[$(date)] - Backup directory does not exist, creating: $BACKUP_DIR" >> "$LOG_FILE"mkdir -p "$BACKUP_DIR" # 递归创建目录if [ $? -eq 0 ]; thenecho "[$(date)] - Backup directory created successfully." >> "$LOG_FILE"elseecho "[$(date)] - Error creating backup directory: $BACKUP_DIR" >> "$LOG_FILE"exit 1 # 创建失败时退出脚本fi
fi# MySQL 数据库数据目录(用于判断数据库目录大小)
DB_DATA_DIR="/var/lib/mysql"LOG_FILE="$BACKUP_DIR/backup_log.txt"
DATE=$(date +"%Y%m%d%H%M%S")
NEW_BACKUP_DIR="$BACKUP_DIR/$DATE"# 获取当前时间的分钟部分
CURRENT_MINUTE=$(date +%M)# 设置增量备份的起始条件
BACKUP_COUNTER=1# 目标文件夹所在磁盘分区
TARGET_DIR="$BACKUP_DIR"# 获取数据库大小(以字节为单位)
DB_SIZE=$(du -sb "$DB_DATA_DIR" | awk '{print $1}')
# 获取目标文件夹可用空间(以字节为单位)
AVAILABLE_SPACE=$(df "$TARGET_DIR" | awk 'NR==2 {print $4}' | awk '{print $1 * 1024}') # 转换为字节# 将空间和数据库信息写入日志
echo "[$(date)] - Checking disk space and database size:" >> "$LOG_FILE"
echo "[$(date)] - Database size (in bytes): $DB_SIZE" >> "$LOG_FILE"
echo "[$(date)] - Available space in target directory (in bytes): $AVAILABLE_SPACE" >> "$LOG_FILE"
echo "[$(date)] - Disk space check command: df $TARGET_DIR" >> "$LOG_FILE"
echo "[$(date)] - Database size check command: du -sb $DB_DATA_DIR" >> "$LOG_FILE"# 判断数据库大小 + 2GB 是否大于目标文件夹可用空间
# 1GB = 1073741824 字节
NEEDED_SPACE=$((DB_SIZE + 2 * 1073741824))if [ $NEEDED_SPACE -gt $AVAILABLE_SPACE ]; thenecho "[$(date)] - Error: Not enough space to perform backup. Required: $((NEEDED_SPACE / 1073741824)) GB, Available: $((AVAILABLE_SPACE / 1024 / 1024)) MB." >> "$LOG_FILE"exit 1
fi# 创建新的备份文件夹
mkdir -p "$NEW_BACKUP_DIR"
echo "[$(date)] - Created backup directory: $NEW_BACKUP_DIR" >> "$LOG_FILE"# 创建全量备份文件夹
FULL_BACKUP_DIR="$NEW_BACKUP_DIR/full_backup"
mkdir -p "$FULL_BACKUP_DIR"
echo "[$(date)] - Created full backup directory: $FULL_BACKUP_DIR" >> "$LOG_FILE"# 执行全量备份
xtrabackup --backup --target-dir="$FULL_BACKUP_DIR" --user=root --password='*******'
if [ $? -eq 0 ]; thenecho "[$(date)] - Full backup completed successfully." >> "$LOG_FILE"
elseecho "[$(date)] - Full backup failed." >> "$LOG_FILE"exit 1
fi# 循环检查当前时间,并执行增量备份
while true
doCURRENT_MINUTE=$(date +%M)CURRENT_MINUTE=$(printf "%02d" $CURRENT_MINUTE)# 如果当前分钟是55或56,退出循环if [ "$CURRENT_MINUTE" -eq 55 ] || [ "$CURRENT_MINUTE" -eq 56 ]; thenecho "[$(date)] - Time is 55 or 56 minutes, stopping the backup process." >> "$LOG_FILE"breakfi# 如果当前分钟是00,跳过增量备份(防止在全量备份后的第一分钟再次备份)if [ "$CURRENT_MINUTE" -eq 00 ]; thenecho "[$(date)] - Skipping incremental backup as it's the start of a new hour." >> "$LOG_FILE"sleep 60 # 等待60秒再继续检查时间continuefi# 每5分钟进行一次备份if [ $(($CURRENT_MINUTE % 5)) -eq 0 ]; then# 执行增量备份INCREMENTAL_BACKUP_DIR="$NEW_BACKUP_DIR/incremental_backup_$BACKUP_COUNTER"mkdir -p "$INCREMENTAL_BACKUP_DIR"echo "[$(date)] - Created incremental backup directory: $INCREMENTAL_BACKUP_DIR" >> "$LOG_FILE"if [ $BACKUP_COUNTER -eq 1 ]; then# 第一次增量备份,基于全量备份xtrabackup --backup --target-dir="$INCREMENTAL_BACKUP_DIR" --incremental-basedir="$FULL_BACKUP_DIR" --user=root --password='*******'else# 后续增量备份,基于上一次的增量备份PREVIOUS_INCREMENTAL_DIR="$NEW_BACKUP_DIR/incremental_backup_$((BACKUP_COUNTER-1))"xtrabackup --backup --target-dir="$INCREMENTAL_BACKUP_DIR" --incremental-basedir="$PREVIOUS_INCREMENTAL_DIR" --user=root --password='*******'fi# 检查增量备份是否成功if [ $? -eq 0 ]; thenecho "[$(date)] - Incremental backup $BACKUP_COUNTER completed successfully." >> "$LOG_FILE"elseecho "[$(date)] - Incremental backup $BACKUP_COUNTER failed." >> "$LOG_FILE"exit 1fi# 更新备份计数器BACKUP_COUNTER=$((BACKUP_COUNTER + 1))# 等待60秒再继续检查时间,避免重复执行sleep 60else# 如果当前时间不是5的倍数,则每15秒检查一次sleep 15fi
done# 结束备份
echo "[$(date)] - Backup process completed." >> "$LOG_FILE"
输出当前所有lsn号脚本
#!/bin/bash# 获取当前目录
dir=$(pwd)# 使用 find 获取文件夹,并按自然数字顺序排序
folders=$(find "$dir" -maxdepth 1 -type d | sort -V)# 遍历每一个文件夹
for folder in $folders; do# 排除当前目录(.)if [ "$folder" == "$dir" ]; thencontinuefi# 拼接出 xtrabackup_checkpoints 文件路径checkpoints_file="$folder/xtrabackup_checkpoints"# 判断文件是否存在if [ -f "$checkpoints_file" ]; then# 提取 from_lsn 和 to_lsnfrom_lsn=$(grep -oP 'from_lsn = \K\d+' "$checkpoints_file")to_lsn=$(grep -oP 'to_lsn = \K\d+' "$checkpoints_file")# 输出结果echo "Folder: $folder"echo " from_lsn: $from_lsn"echo " to_lsn: $to_lsn"elseecho "xtrabackup_checkpoints not found in folder: $folder"fi
done