XtraBackup 是 Percona 開發的開源熱備份工具,支援 InnoDB 與 XtraDB 存儲引擎,能在不鎖表或僅短暫鎖表的情況下完成備份,是生產環境 MySQL 備份的首選方案。本文記錄備份、恢復與增量備份的實操流程。
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
xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/u01/xtrabackup/1/
cd /u01/xtrabackup/1/ && ls
# 輸出:ibdata1 test xtrabackup_checkpoints xtrabackup_logfile
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 原生支援增量備份,備份體積小、速度快,適合大數據量場景。
xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/u01/xtrabackup/2/
ls /u01/xtrabackup/2/
# ibdata1 test xtrabackup_checkpoints xtrabackup_logfile
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
需分別對全量與增量目錄執行 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 不會備份表結構。