首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

XtraBackup 實現 MySQL 熱備份與增量備份完整指南

     編輯:MySQL 資料庫相關知識

前言

XtraBackup 是 Percona 開發的開源熱備份工具,支援 InnoDB 與 XtraDB 存儲引擎,能在不鎖表或僅短暫鎖表的情況下完成備份,是生產環境 MySQL 備份的首選方案。本文記錄備份、恢復與增量備份的實操流程。

一、基礎備份與恢復

1. InnoDB 配置說明

innodb_data_home_dir = ./
innodb_data_file_path = ibdata1:50M:autoextend
innodb_log_group_home_dir = ./
innodb_log_files_in_group = 4
innodb_log_file_size = 104857600

2. 全量備份

xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/u01/xtrabackup/1/
cd /u01/xtrabackup/1/ && ls
# 輸出:ibdata1 test xtrabackup_checkpoints xtrabackup_logfile

3. 恢復操作(需執行兩次 prepare)

xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/u01/xtrabackup/1/
xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/u01/xtrabackup/1/
cd /u01/xtrabackup/1/ && ls
# 輸出:ibdata1 ib_logfile0 ib_logfile1 ib_logfile2 ib_logfile3 test xtrabackup_checkpoints xtrabackup_logfile

此時 InnoDB 資料檔案已恢復完成。將這些檔案複製回 MySQL 資料目錄即可啟動服務。

關鍵提醒:XtraBackup 僅備份資料檔案(ibdata、ib_logfile 等),不備份表結構檔案。恢復時必須確保目標環境已存在對應的 .frm 表結構檔案。

二、增量備份策略

相較於 innobackupex,XtraBackup 原生支援增量備份,備份體積小、速度快,適合大數據量場景。

1. 基礎全量備份

xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/u01/xtrabackup/2/
ls /u01/xtrabackup/2/
# ibdata1 test xtrabackup_checkpoints xtrabackup_logfile

2. 執行增量備份

xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/u01/xtrabackup/2.1/ --incremental-basedir=/u01/xtrabackup/2/
ls /u01/xtrabackup/2.1/
# ibdata1.delta test xtrabackup_checkpoints xtrabackup_logfile

增量備份目錄下的資料檔案以 .delta 結尾,僅包含上次全量備份後被修改的頁面,空間佔用極小:

cd /u01/xtrabackup/ && du --max-depth=1 -h
428K ./2.1
935M ./2

3. 增量備份恢復流程

需分別對全量與增量目錄執行 prepare:

# 1. 準備全量備份(redo 恢復)
xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/u01/xtrabackup/2/

# 2. 合併增量備份到全量目錄
xtrabackup --prepare --target-dir=/u01/xtrabackup/2/ --incremental-dir=/u01/xtrabackup/2.1/

# 3. 再次準備全量目錄(可選,確保一致性)
xtrabackup --prepare --target-dir=/u01/xtrabackup/2/

完成後,/u01/xtrabackup/2/ 下的檔案即可直接作為 MySQL 資料目錄使用。

再次強調:恢復時必須具備對應的 .frm 表結構檔案,XtraBackup 不會備份表結構。

三、參考資源

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved