增量備份xtrabackup
MySQL 備份工具
物理備份缺點(diǎn)
- 跨平臺(tái)性差
- 備份時(shí)間長(zhǎng),冗余備份,浪費(fèi)存儲(chǔ)空間
mysqldump 備份缺點(diǎn)
- 效率較低,備份和還原速度慢
- 備份過程中,數(shù)據(jù)插入和更新操作會(huì)被掛起
XtraBackup工具
優(yōu)點(diǎn)
- 備份過程中不鎖庫表,適合生產(chǎn)環(huán)境
- 由專業(yè)組織Percona提供( 改進(jìn)MySQL分支 )
主要組件
- xtrabackup: C程序,支持InnoDB/XtraDB
- innobackupex: 以Perl腳本封裝xtrabackup,支持MyISAM
安裝 Xtrabackup
通過官方源安裝
#安裝Percona官方源
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
#安裝 xtrabackup 2.4版本,不同版本支持不同版本的MySQL
yum install percona-xtrabackup-24 -y
#查看版本
xtrabackup --version
xtrabackup: recognized server arguments: --server-id=101 --log_bin=/logdir/rz --datadir=/var/lib/mysql
xtrabackup version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
#查看安裝rpm包
rpm -qa |grep percona
#查看RPM包安裝文件
rpm -ql percona-xtrabackup-24
基本參數(shù)
常用選項(xiàng) | 含義 |
---|---|
--host | 主機(jī)名 |
--user | 用戶名 |
--port | 端口號(hào) |
--password | 密碼 |
--databases | 數(shù)據(jù)庫名 |
--no-timestamp | 不用日期命名備份文件存儲(chǔ)的子目錄名 |
多庫語法 | 說明 |
---|---|
--databases="庫名" | 單個(gè)庫 |
--databases="庫1 庫2" | 多個(gè)庫 |
--databases="庫.表" | 單個(gè)表 |
進(jìn)階參數(shù)
常用選項(xiàng) | 含義 |
---|---|
--redo-only | 日志合并 |
--apply-log | 準(zhǔn)備還原(回滾日志) |
--copy-back | 恢復(fù)數(shù)據(jù) |
--incremental 目錄名 | 增量備份 |
--incrementtal-basedir=目錄名 | 增量備份時(shí),指定上一次備份數(shù)據(jù)存儲(chǔ)的目錄名 |
--incremental-dir=目錄名 | 準(zhǔn)備恢復(fù)數(shù)據(jù)時(shí),指定增量備份數(shù)據(jù)存儲(chǔ)的目錄名 |
--export | 導(dǎo)出表信息 |
--import | 導(dǎo)出表空間 |
Innobackupex應(yīng)用案例
完全備份
- 將所有庫完全備份到 /backup
innobackupex --user root --password 123123 /allbak [--no-timestamp]
xtrabackup 不能備份MySIAM表
innobackupex 封裝了xtrabackup,可以增量備份 innodb ,但是mysiam是全量備份
- 備份指定庫
innobackupex --user root --password 123123 --databases="buydb mysql" /twodbbak [--no-timestamp]
- 備份指定表所有數(shù)據(jù)
innobackupex --user root --password 123123 --databases="buydb.a" /opt/a --no-timestamp
- 要求數(shù)據(jù)庫目錄為空,才可以恢復(fù)數(shù)據(jù).生產(chǎn)環(huán)境一般使用全備.恢復(fù)時(shí),缺少系統(tǒng)三個(gè)庫時(shí)會(huì)無法啟動(dòng).
innobackupex --user root --password 123123 --databases="mysql sys performance_schema buydb" /opt/buydbfull --no-timestamp
完全恢復(fù)
要求數(shù)據(jù)庫目錄為空
- 重做日志 --apply-log
- 恢復(fù)數(shù)據(jù) --copy-back
- 重啟數(shù)據(jù)庫服務(wù)
#重做日志
innobackupex --user root --password 123123 --apply-log /opt/buydbfull/
#xtrabackup_checkpoints 文件中 backup_type 會(huì)從 backed變化為 prepared
cat /opt/buydbfull/xtrabackup_checkpoints
backup_type = full-prepared
#停止數(shù)據(jù)庫,清空數(shù)據(jù)目錄,否則無法恢復(fù)
systemctl stop mysqld
rm -rf /var/lib/mysql/*
#恢復(fù)數(shù)據(jù),修改文件所有者
innobackupex --copy-back /opt/buydbfull/
chown -R mysql. /var/lib/mysql
#啟動(dòng)mysqld,查看
systemctl start mysqld
增量備份
做首次備份(完全備份)
innobackupex --user root --password 123123 --databases="mysql sys performance_schema buydb" /fullbuydb --no-timestamp
寫入新數(shù)據(jù),第一次增量備份
--incremental 執(zhí)行增量備份文件存儲(chǔ)目錄
--incremental-basedir= 上次備份文件存儲(chǔ)目錄
innobackupex --user root --password 123123 --databases="mysql sys performance_schema buydb" --incremental /new1dir --incremental-basedir=/fullbuydb --no-timestamp
寫入新數(shù)據(jù),第二次增量備份
innobackupex --user root --password 123123 --databases="mysql sys performance_schema buydb" --incremental /new2dir --incremental-basedir=/new1dir --no-timestamp
生產(chǎn)環(huán)境中定時(shí)執(zhí)行,
00 23 * * 1 /shell/allbak.sh 完全備份innobackupex,每周1執(zhí)行
00 23 * * 2-7 /shell/newdata.sh 增量備份innobackupex,周二到周日?qǐng)?zhí)行
工作原理 innodb 和 xtrdb 有效
增量備份工作原理
事務(wù)日志文件的LSN,每次增量備份都會(huì)記錄每次的LSN范圍值.
ib_logfile0 正確完成的sql命令
ib_logfile1 正確完成的sql命令
ibdata1 未提交的sql命令
LSN 日志序列號(hào) 增量備份lsn記錄在 xtrabackup_checkpoints
每一條命令有一個(gè) LSN
增量恢復(fù)
--incremental-dir= 增量備份目錄名
重做并合并日志 --apply-log --redo-only
停止服務(wù),清空數(shù)據(jù)目錄
恢復(fù)數(shù)據(jù)
#合并增量備份的日志,范圍取自 lsn值
innobackupex --apply-log --redo-only /fullbuydb/
innobackupex --apply-log --redo-only /fullbuydb/ --incremental-dir=/new1dir
innobackupex --apply-log --redo-only /fullbuydb/ --incremental-dir=/new2dir
#停止服務(wù)清空數(shù)據(jù)目錄
systemctl stop mysqld
rm -rf /var/lib/mysql/*
#恢復(fù)數(shù)據(jù)
innobackupex --copy-back /fullbuydb/
chown mysql. /var/lib/mysql -R
systemctl start mysqld
完全備份文件中恢復(fù)某個(gè)表
--export 導(dǎo)出表信息
恢復(fù)步驟
- 從備份數(shù)據(jù)利導(dǎo)出表信息.
#全備中導(dǎo)出buydb庫下表信息
innobackupex --apply-log --export /buydbfull2 --no-timestamp
#查看buydb庫備份文件,新增 后綴為 .cfg 和 .exp 表信息文件
ls /buydbfull2/buydb/
a.cfg a.exp a.frm a.ibd b.cfg b.exp b.frm b.ibd db.opt
- 創(chuàng)建刪除的表( 表結(jié)構(gòu)要和刪除時(shí)相同 )
mysql> create table b(name char(10));
- 刪除表空間
alter table 庫名.表名 discard tablespace;
mysql> alter table buydb.b discard tablespace;
- 把導(dǎo)出的表信息文件拷貝到數(shù)據(jù)庫目錄下
cp /buydbfull2/buydb/b.{cfg,exp,ibd} /var/lib/mysql/buydb/
- 修改所有者
chown mysql. -R /var/lib/mysql
- 導(dǎo)入表空間
alter table 庫名.表名 import tablespace;
mysql> alter table buydb.b import tablespace;
- 驗(yàn)證查看數(shù)據(jù),刪除無用表信息文件
mysql> select * from buydb.b;
rm -rf /var/lib/mysql/buydb/b.{exp,cfg};