做者: 沈小然php
版本:html
文檔編號:mysql
日期:2016年 6月 14日linux
Xtrabackup是由percona開發的一個開源軟件,代替商業付費軟件MySQL Enterprise Backup (InnoDB Hot Backup),這個工具價格是 $5000 per Server,能夠在線對InnoDB/XtraDB引擎的表進行物理備份。sql
Mysql自帶的備份工具mysqldump支持在線備份,可是邏輯備份,效率比較差。數據庫
Xtrabackup有兩個主要的工具:xtrabackup、innobackupex,其中xtrabackup只能備份InnoDB和XtraDB兩種數據表,innobackupex則封裝了xtrabackup,同時能夠備份MyISAM數據表。app
l xtrabackup命令:用於熱備份innodb(支持事務), xtradb表的工具,不能備份其餘表。socket
l innobackupex命令:對xtrabackup封裝的perl腳本,提供了myisam表備份的能力(能進行整庫和數據表備份)。工具
Xtrabackup作備份的時候不能備份表結構、觸發器等等,智能紛紛.idb數據文件。另外innobackupex還不能徹底支持增量備份,須要和xtrabackup結合起來實現全備的功能。ui
官網下載頁面:https://www.percona.com/downloads/XtraBackup/LATEST/
官網在線文檔:https://www.percona.com/doc/percona-xtrabackup/2.2/index.html
下載rpm包:
https://www.percona.com/downloads/XtraBackup/XtraBackup-2.0.8/RPM/rhel6/x86_64/percona-xtrabackup-20-2.0.8-587.rhel6.x86_64.rpm
OS: Red Hat Enterprise Linux Server release 6.5 (Santiago)
Mysql:Server version: 5.1.71 Source distribution
注意:rhel6.5版本自帶的mysql5.1.71須要使用Percona XtraBackup 2.0系列的版本
從http://rpmfind.net/linux/rpm2html/search.php上下載libev-4.15-1.el6.rf.x86_64.rpm安裝包
# rpm -ivh libev-4.15-1.el6.rf.x86_64.rpm
Preparing... ########################################### [100%]
1:libev ########################################### [100%]
# rpm -ivh percona-xtrabackup-20-2.0.8-587.rhel6.x86_64.rpm
warning: percona-xtrabackup-20-2.0.8-587.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing... ########################################### [100%]
1:percona-xtrabackup-20 ########################################### [100%]
查看安裝後的文件位置
# rpm -ql percona-xtrabackup-20-2.0.8-587.rhel6.x86_64
/usr/bin/innobackupex
/usr/bin/innobackupex-1.5.1 -> innobackupex
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/bin/xtrabackup_51
/usr/bin/xtrabackup_55
/usr/bin/xtrabackup_56
/usr/share/doc/percona-xtrabackup-20-2.0.8
/usr/share/doc/percona-xtrabackup-20-2.0.8/COPYING
# vi /etc/my.cnf
[client] default-character-set = utf8
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 character-set-server = utf8 collation-server = utf8_general_ci
[mysql] no-auto-rehash default-character-set = utf8
[mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid |
innobackupex使用方法
Usage: [innobackupex [--defaults-file=#] --backup | innobackupex [--defaults-file=#] --prepare] [OPTIONS]
# innobackupex --defaults-file=/etc/my.cnf --user=root --password="" /mysql_backup
參數說明:
l --defaults-file=/etc/my.cnf:指定my.cnf配置文件位置
l --user=root:訪問mysql的用戶
l --password="123qwe":訪問mysql的用戶口令,當mysql的root口令爲空時省略—password參數。
l --slave-info:slave-info能夠記錄備份恢 復後,做爲slave須要的一些信息,根據這些信息,能夠很方便的利用備份來重作slave。
l /mysql_backup:備份位置
l 2>/mysql_backup/ innobackupex.log:記錄備份時的輸出
l --databases=數據庫名:使用這個參數,針對某個數據庫進行備份
1) 建立備份位置
# mkdir /mysql_backup
2) Innobackupex全備份
# innobackupex --user=root --slave-info --defaults-file=/etc/my.cnf /mysql_backup
……
160614 16:53:54 innobackupex: completed OK!
最後看見這句話innobackupex: completed OK!就表示備份成功了。
3)查看備份後的數據文件
# ls /mysql_backup/2016-06-14_16-53-35/
backup-my.cnf mysql test xtrabackup_binlog_info xtrabackup_logfile
ibdata1 ra xtrabackup_binary xtrabackup_checkpoints
說明:
xtrabackup_checkpoints:記錄的備份類型,例如backup_type = full-backuped
1)模擬mysql數據丟失
# /etc/init.d/mysqld stop
# rm -rf /var/lib/mysql/*
2)首先執行—apply-log
# innobackupex --apply-log /mysql_backup/2016-06-14_17-07-36/
3)恢復所有mysql數據,執行—copy-back
# innobackupex --copy-back /mysql_backup/2016-06-14_17-07-36/
驗證一下恢復的數據文件
# ls
ibdata1 ib_logfile0 ib_logfile1 mysql ra test xtrabackup_slave_info
恢復數據後的權限是root,須要手動改爲mysql.mysql
# chown -R mysql.mysql /var/lib/mysql
最後,啓動mysqld服務
# /etc/init.d/mysqld start
恢復所有完成
1) 首先使用innobackupex作全備份
# innobackupex --user=root --slave-info --defaults-file=/etc/my.cnf /mysql_backup
2) 使用xtrabackup作增量備份
# xtrabackup --backup --incremental-basedir=/mysql_backup/2016-06-14_17-25-59/ --target-dir=/mysql_backup/2016-06-14_17-25-59-2
說明:
--incremental-basedir:表示使用哪一個目錄作爲全量檢查點。
--target-dir:增量數據保存的目錄,目錄名能夠追次變成xxx-2,xxx-3等。
1) 模擬mysql數據丟失
中止mysqld服務
# /etc/init.d/mysqld stop
刪除全部數據
# rm -rf *
2) 執行—apply-log
# innobackupex --apply-log /mysql_backup/2016-06-14_17-25-59
3) 使用xtrabackup將增量備份應用到全備份目錄2016-06-14_17-25-59中
# xtrabackup --prepare --incremental-dir=/mysql_backup/2016-06-14_17-25-59-2 --target-dir=/mysql_backup/2016-06-14_17-25-59
# xtrabackup --prepare --incremental-dir=/mysql_backup/2016-06-14_17-25-59-3 --target-dir=/mysql_backup/2016-06-14_17-25-59
說明:
--incremental-dir:表示增量目錄,應用增量的時間順序應該是由早-晚
--target-dir:應用到的全備份目錄
4) 恢復所有mysql數據,執行—copy-back
# innobackupex --user=root --copy-back /mysql_backup/2016-06-14_17-25-59
# chown -R mysql.mysql /var/lib/mysql
# /etc/init.d/mysqld start
數據所有恢復完成
# innobackupex --user=root --defaults-file=/etc/my.cnf --databases=ra /mysql_backup/
# ls 2016-06-14_18-06-20/
backup-my.cnf ibdata1 xtrabackup_logfile xtrabackup_suspended
# innobackupex --defaults-file=/etc/my.cnf --backup --user=root --password="" /mysql_backup
Error: Built-in InnoDB in MySQL 5.1 is not supported in this release. You can either use Percona XtraBackup 2.0, or upgrade to InnoDB plugin.
這個錯誤須要從新下載安裝Percona XtraBackup 2.0版原本解決。