一 簡介:
Xtrabackup是一個對InnoDB作數據備份的工具,支持在線熱備份(備份時不影響數據讀寫),是商業備份工具InnoDB Hotbackup的一個很好的替代品。它能對InnoDB和XtraDB存儲引擎的數據庫非阻塞地備份(對於MyISAM的備份一樣須要加表鎖)。XtraBackup支持全部的Percona Server、MySQL、MariaDB和Drizzle。幾年前使用過,但如今忘記的差很少了,因此就從新拾起看看。html
xtrabackup有兩個主要的工具:xtrabackup、innobackupexmysql
(1).xtrabackup只能備份InnoDB和XtraDB 兩種數據表 (2).innobackupex則封裝了xtrabackup,同時能夠備份MyISAM數據表
xtrabackup_binlog_info:記錄當前最新的LOG Position
xtrabackup_binlog_pos_innodb:innodb log postion
xtrabackup_checkpoints: 存放備份的起始位置beginlsn和結束位置endlsn,增量備份須要這個lsn[增量備份能夠在這裏面看from和to兩個值的變化]
二 安裝: http://www.percona.com/downloads/sql
下載安裝:數據庫
1)直接下載二進制包,不編譯,編譯須要和MySQL源碼包配合。ubuntu
wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/binary/Linux/x86_64/percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz
1)tar zxvf percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz 2)ls -lh drwxr-xr-x 2 root root 4.0K 5月 2 2014 bin drwxr-xr-x 4 root root 4.0K 5月 2 2014 share 3) ls -lh bin/ -rwxr-xr-x 1 root root 165K 5月 2 2014 innobackupex lrwxrwxrwx 1 root root 12 5月 2 2014 innobackupex-1.5.1 -> innobackupex -rwxr-xr-x 1 root root 2.2M 5月 2 2014 xbcrypt -rwxr-xr-x 1 root root 2.2M 5月 2 2014 xbstream -rwxr-xr-x 1 root root 13M 5月 2 2014 xtrabackup -rwxr-xr-x 1 root root 16M 5月 2 2014 xtrabackup_55 -rwxr-xr-x 1 root root 79M 5月 2 2014 xtrabackup_56 4) cp innobackupex-1.5.1 /usr/bin/innobackupex cp xtrabackup_55 /usr/bin/xtrabackup #cp xtrabackup /usr/bin
2)Ubuntu 下的安裝:http://www.percona.com/doc/percona-xtrabackup/2.1/installation/apt_repo.html服務器
1:執行 apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A 2:在 vi /etc/apt/sources.list 添加 deb http://repo.percona.com/apt VERSION main deb-src http://repo.percona.com/apt VERSION main 3:安裝 $ apt-get update $ apt-get install percona-xtrabackup
支持的版本有:併發
Debianapp
6.0 (squeeze)ssh
7.0 (wheezy)socket
Ubuntu
10.04LTS (lucid)
12.04LTS (precise)
12.10 (quantal)
13.04 (raring)
13.10 (saucy)
把2中的 VERSION 按照本身的系統把上面的代入。
deb http://repo.percona.com/apt precise main deb-src http://repo.percona.com/apt precise main
# precise是Ubuntu-12.04的版本代號,若是是其它系統版本,須要更換。
三 使用說明:
安裝完以後會生成幾個工具:http://www.percona.com/doc/percona-xtrabackup/2.1/manual.html
innobackupex:這個是實際上是下面三個工具的一個perl腳本封裝,能夠備份MyISAM, InnoDB, XtraDB表。但在處理Myisam時須要加一個讀鎖。
xtrabackup:一個由C編譯而來的二進制文件,只能備份InnoDB和XtraDB數據。
xbcrypt:用來加密或解密備份的數據。
xbstream:用來解壓或壓縮xbstream格式的壓縮文件。
innobackupex :
xtrabackup命令只備份數據文件,並不備份數據表結構(.frm),因此使用xtrabackup恢復的時候必須有對應表結構文件(.frm)。用innobackupex命令,此命令至關於冷備份,複製數據目錄的索引,數據,結構文件,但會有短暫的鎖表(時間依賴於MyISAM大小)。
參數:
--defaults-file:指定my.cnf參數文件的位置[此配置文件裏必須指定datadir] --apply-log:同xtrabackup的--prepare參數,通常狀況下,在備份完成後,數據尚且不能用於恢復操做,由於備份的數據中可能會包含還沒有提交的事務或已經提交但還沒有同步至數據文件中的事務。所以,此時數據 文件仍處理不一致狀態。--apply-log的做用是經過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處於一致性狀態。 --copy-back:作數據恢復時將備份數據文件拷貝到MySQL服務器的datadir --remote-host=HOSTNAME: 經過ssh將備份數據存儲到進程服務器上 --stream=[tar]:備份文件輸出格式, 該文件可在XtarBackup binary文件中得到. 在使用參數stream=tar備份的時候,你的xtrabackup_logfile可能會臨時放在/tmp目錄下,若是你備份的時候併發寫入較大的話,xtrabackup_logfile可能會很大(5G+),極可能會撐滿你的/tmp目錄,能夠經過參數--tmpdir指定目錄來解決這個問題. --tmpdir=DIRECTORY:當有指定--remote-host or --stream時, 事務日誌臨時存儲的目錄, 默認採用MySQL配置文件中所指定的臨時目錄tmpdir --redo-only --apply-log:強制備份日誌時只redo,跳過rollback,這在作增量備份時很是必要 --use-memory=*:該參數在prepare的時候使用,控制prepare時innodb實例使用的內存 --databases=LIST:列出須要備份的databases,若是沒有指定該參數,全部包含MyISAM和InnoDB表的database都會被備份 --slave-info:備份從庫, 加上--slave-info備份目錄下會多生成一個xtrabackup_slave_info 文件, 這裏會保存主日誌文件以及偏移, 文件內容相似於:CHANGE MASTER TO MASTER_LOG_FILE='', MASTER_LOG_POS=0 --socket=SOCKET:指定mysql.sock所在位置,以便備份進程登陸mysql.
更多參數見:http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/innobackupex_option_reference.html
四 測試:
1)全量備份&還原
#初始化
mysql> create database xtra_test default charset utf8; Query OK, 1 row affected (0.00 sec) mysql> use xtra_test Database changed mysql> create table M(id int,name varchar(10))engine=myisam; Query OK, 0 rows affected (0.00 sec) mysql> create table I(id int,name varchar(10))engine=innodb; Query OK, 0 rows affected (0.00 sec) mysql> insert into M values(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> insert into I values(11,'A'),(22,'B'),(33,'C'),(44,'D'),(55,'E'); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from M; +------+------+ | id | name | +------+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | +------+------+ 5 rows in set (0.00 sec) mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec)
#備份
root@ha1:~# innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --databases=xtra_test /home/zhoujy/xtrabackup/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. Get the latest version of Percona XtraBackup, documentation, and help resources: http://www.percona.com/xb/p 141111 11:56:58 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/mysql/my.cnf;mysql_read_default_group=xtrabackup;host=192.168.200.51' as 'zjy' (using password: YES). 141111 11:56:58 innobackupex: Connected to MySQL server 141111 11:56:58 innobackupex: Executing a version check against the server... 141111 11:56:58 innobackupex: Done. 141111 11:56:58 innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully. At the end of a successful backup run innobackupex prints "completed OK!". innobackupex: Using server version 5.5.38-0ubuntu0.12.04.1-log innobackupex: Created backup directory /home/zhoujy/xtrabackup/2014-11-11_11-56-58 141111 11:56:58 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/etc/mysql/my.cnf" --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58 --innodb_log_file_size="5242880" --innodb_data_file_path="ibdata1:10M:autoextend" --tmpdir=/tmp --extra-lsndir='/tmp' --databases='xtra_test' innobackupex: Waiting for ibbackup (pid=10539) to suspend innobackupex: Suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2' xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /var/lib/mysql xtrabackup: open files limit requested 0, set to 1024 xtrabackup: using the following InnoDB configuration: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 >> log scanned up to (7363097930) xtrabackup: Generating a list of tablespaces [01] Copying ./ibdata1 to /home/zhoujy/xtrabackup/2014-11-11_11-56-58/ibdata1 >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) [01] ...done >> log scanned up to (7363097930) xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2' with pid '10540' 141111 11:57:11 innobackupex: Continuing after ibbackup has suspended 141111 11:57:11 innobackupex: Executing FLUSH TABLES WITH READ LOCK... 141111 11:57:11 innobackupex: All tables locked and flushed to disk 141111 11:57:11 innobackupex: Starting to backup non-InnoDB tables and files innobackupex: in subdirectories of '/var/lib/mysql/' innobackupex: Backing up file '/var/lib/mysql//xtra_test/db.opt' innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.MYI' >> log scanned up to (7363097930) innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.frm' innobackupex: Backing up file '/var/lib/mysql//xtra_test/I.frm' innobackupex: Backing up file '/var/lib/mysql//xtra_test/M.MYD' 141111 11:57:11 innobackupex: Finished backing up non-InnoDB tables and files 141111 11:57:11 innobackupex: Executing FLUSH ENGINE LOGS... 141111 11:57:11 innobackupex: Waiting for log copying to finish xtrabackup: The latest check point (for incremental): '7363097930' xtrabackup: Stopping log copying thread. .>> log scanned up to (7363097930) xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_log_copied' with pid '10540' xtrabackup: Transaction log of lsn (7363097930) to (7363097930) was copied. 141111 11:57:12 innobackupex: All tables unlocked innobackupex: Backup created in directory '/home/zhoujy/xtrabackup/2014-11-11_11-56-58' innobackupex: MySQL binlog position: filename 'mysql-bin51.000001', position 967 141111 11:57:12 innobackupex: Connection to database server closed 141111 11:57:12 innobackupex: completed OK!
查看備份出來的文件:
ls -lh 2014-11-11_11-56-58/ total 1.2G -rw-r--r-- 1 root root 188 11月 11 11:56 backup-my.cnf -rw-r----- 1 root root 1.2G 11月 11 11:57 ibdata1 -rw-r--r-- 1 root root 25 11月 11 11:57 xtrabackup_binlog_info -rw-r----- 1 root root 95 11月 11 11:57 xtrabackup_checkpoints -rw-r--r-- 1 root root 666 11月 11 11:57 xtrabackup_info -rw-r----- 1 root root 2.5K 11月 11 11:57 xtrabackup_logfile drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test
/home/zhoujy/xtrabackup/ 備份存放的位置,備份會在該目錄下生成一個按照時間命名的文件夾。用--no-timestamp參數能夠指定到本身想要的備份文件夾,不受時間命名的文件夾限制。
利用 --apply-log的做用是經過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處於一致性狀態。
root@ha1:/home/zhoujy/xtrabackup# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_11-56-58/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. Get the latest version of Percona XtraBackup, documentation, and help resources: http://www.percona.com/xb/p 141111 12:02:42 innobackupex: Starting the apply-log operation IMPORTANT: Please check that the apply-log run completes successfully. At the end of a successful apply-log run innobackupex prints "completed OK!". 141111 12:02:42 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/home/zhoujy/xtrabackup/2014-11-11_11-56-58/backup-my.cnf" --defaults-group="mysqld" --prepare --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58 xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58 xtrabackup: This target seems to be not prepared yet. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(7363097930) xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter) InnoDB: Using atomics to ref count buffer pool pages InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Memory barrier is not used InnoDB: Compressed tables use zlib 1.2.3.4 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 100.0M InnoDB: Completed initialization of buffer pool InnoDB: Highest supported file format is Barracuda. InnoDB: The log sequence numbers 7363094028 and 7363094028 in ibdata files do not match the log sequence number 7363097930 in the ib_logfiles! InnoDB: Database was not shutdown normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages InnoDB: from the doublewrite buffer... InnoDB: Last MySQL binlog file position 0 967, file name /var/log/mysql/mysql-bin51.000001 InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.21 started; log sequence number 7363097930 [notice (again)] If you use binary log and don't use any hack of group commit, the binary log position seems to be: InnoDB: Last MySQL binlog file position 0 967, file name /var/log/mysql/mysql-bin51.000001 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 7363100603 141111 12:02:44 innobackupex: Restarting xtrabackup with command: xtrabackup --defaults-file="/home/zhoujy/xtrabackup/2014-11-11_11-56-58/backup-my.cnf" --defaults-group="mysqld" --prepare --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58 for creating ib_logfile* xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58 xtrabackup: This target seems to be already prepared. xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'. xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter) InnoDB: Using atomics to ref count buffer pool pages InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Memory barrier is not used InnoDB: Compressed tables use zlib 1.2.3.4 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 100.0M InnoDB: Completed initialization of buffer pool InnoDB: Setting log file ./ib_logfile101 size to 5 MB InnoDB: Setting log file ./ib_logfile1 size to 5 MB InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 InnoDB: New log files created, LSN=7363100603 InnoDB: Highest supported file format is Barracuda. InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.21 started; log sequence number 7363100684 [notice (again)] If you use binary log and don't use any hack of group commit, the binary log position seems to be: InnoDB: Last MySQL binlog file position 0 967, file name /var/log/mysql/mysql-bin51.000001 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 7363103417 141111 12:02:45 innobackupex: completed OK!
應用完以後再查看備份文件看是否有變化:
root@ha1:/home/zhoujy/xtrabackup# ls -lh 2014-11-11_11-56-58/ total 1.2G -rw-r--r-- 1 root root 188 11月 11 11:56 backup-my.cnf -rw-r----- 1 root root 1.2G 11月 11 12:02 ibdata1 -rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile0 -rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile1 -rw-r--r-- 1 root root 25 11月 11 11:57 xtrabackup_binlog_info -rw-r--r-- 1 root root 38 11月 11 12:02 xtrabackup_binlog_pos_innodb -rw-r----- 1 root root 95 11月 11 12:02 xtrabackup_checkpoints -rw-r--r-- 1 root root 666 11月 11 11:57 xtrabackup_info -rw-r----- 1 root root 2.0M 11月 11 12:02 xtrabackup_logfile drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test
看到redo log已經出現。xtrabackup_開頭的幾個文件記錄了一些日誌偏移量的信息和日誌名和時間等信息。在xtrabackup_checkpoints中記錄了備份的模式:
backup_type = full-backuped。
xtrabackup_binlog_info:記錄當前最新的LOG Position
xtrabackup_binlog_pos_innodb:innodb log postion
xtrabackup_checkpoints: 存放備份的起始位置beginlsn和結束位置endlsn,增量備份須要這個lsn[增量備份能夠在這裏面看from和to兩個值的變化
xtrabackup_info:備份的一些具體信息日誌
#還原
mysql> drop database xtra_test; Query OK, 2 rows affected (0.00 sec)
1) 刪除原始目錄裏的文件:
root@ha1:/var/lib/mysql# rm ib_logfile0 ib_logfile1 ibdata1
關閉數據庫: 1) root@ha1:/var/lib/mysql# /etc/init.d/mysql stop 開始恢復 2) 1:innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /home/zhoujy/xtrabackup/2014-11-11_11-56-58/ 執行上面的命令須要保證數據庫目錄是空的,不然會失敗,由於目錄裏有binlog日誌,要麼移走,要麼就用手動複製須要的文件。因此說日誌不要和數據放在一塊兒! 2:cp ib_logfile0 ib_logfile1 ibdata1 /var/lib/mysql/ cp -R xtra_test /var/lib/mysql/ 修改權限和開啓數據庫 3) root@ha1:/var/lib/mysql# chown -R mysql.mysql ib* root@ha1:/var/lib/mysql# chown -R mysql.mysql xtra_test/ 開啓數據庫 root@ha1:/var/lib/mysql# /etc/init.d/mysql start ... mysql start/running, process 15937
數據已恢復:
mysql> use xtra_test Database changed mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec)
2)增量備份&還原
#備份
在作增量備份的時候須要作一次全量備份,和上面同樣,只是針對全部庫的備份:
root@ha1:~# innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf /home/zhoujy/xtrabackup/ ... innobackupex: MySQL binlog position: filename 'mysql-bin51.000002', position 107 141111 13:57:24 innobackupex: Connection to database server closed 141111 13:57:24 innobackupex: completed OK!
增量備份這裏開始
1.數據庫操做:
mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec) mysql> insert into I values(111,'A'),(222,'B'),(333,'C'),(444,'D'),(555,'E'); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from M; +------+------+ | id | name | +------+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | +------+------+ 5 rows in set (0.00 sec) mysql> update M set name = upper(name); Query OK, 5 rows affected (0.00 sec) Rows matched: 5 Changed: 5 Warnings: 0 mysql> create table X(name varchar(20))default charset utf8; Query OK, 0 rows affected (0.01 sec) mysql> insert into X values('zjy'); Query OK, 1 row affected (0.00 sec)
2:增量備份(基於全量的增量備份)
root@ha1:~# innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --incremental --incremental-basedir=/home/zhoujy/xtrabackup/2014-11-11_13-57-11/ /home/zhoujy/increment_data/ ... ... trabackup: Creating suspend file '/home/zhoujy/increment_data/2014-11-11_14-14-56/xtrabackup_log_copied' with pid '19492' xtrabackup: Transaction log of lsn (7363106612) to (7363106612) was copied. 141111 14:15:06 innobackupex: All tables unlocked innobackupex: Backup created in directory '/home/zhoujy/increment_data/2014-11-11_14-14-56' innobackupex: MySQL binlog position: filename 'mysql-bin51.000002', position 981 141111 14:15:06 innobackupex: Connection to database server closed 141111 14:15:06 innobackupex: completed OK!
其中,--incremental指明是增量備份,--incremental-basedir指定上次完整備份或者增量備份文件的位置。這裏的增量備份其實只針對的是InnoDB,對於MyISAM來講,仍是完整備份。
增量備份的文件:
root@ha1:/home/zhoujy/increment_data# ls -lh 2014-11-11_14-14-56/ total 440K -rw-r--r-- 1 root root 188 11月 11 14:14 backup-my.cnf -rw-r----- 1 root root 400K 11月 11 14:15 ibdata1.delta -rw-r----- 1 root root 44 11月 11 14:14 ibdata1.meta drwxr-xr-x 2 root root 4.0K 11月 11 14:15 mha_test drwxr-xr-x 2 root root 4.0K 11月 11 14:15 mysql drwxr-xr-x 2 root root 4.0K 11月 11 14:15 performance_schema -rw-r--r-- 1 root root 25 11月 11 14:15 xtrabackup_binlog_info -rw-r----- 1 root root 102 11月 11 14:15 xtrabackup_checkpoints -rw-r--r-- 1 root root 738 11月 11 14:15 xtrabackup_info -rw-r----- 1 root root 2.5K 11月 11 14:15 xtrabackup_logfile drwxr-xr-x 2 root root 4.0K 11月 11 14:15 xtra_test root@ha1:/home/zhoujy/increment_data# cat 2014-11-11_14-14-56/xtrabackup_checkpoints backup_type = incremental from_lsn = 7363103427 對應增量中的to_lsn to_lsn = 7363106612 last_lsn = 7363106612 compact = 0
3:繼續增量備份
數據庫操做
mysql> insert into X values('dxy'); Query OK, 1 row affected (0.01 sec) mysql> insert into X values('浙江'); Query OK, 1 row affected (0.01 sec)
增量備份(基於增量的增量備份)
root@ha1:~# innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --incremental --incremental-basedir=/home/zhoujy/increment_data/2014-11-11_14-14-56/ /home/zhoujy/increment_data/ 141111 14:25:46 innobackupex: Connection to database server closed 141111 14:25:46 innobackupex: completed OK!
增量備份的文件
root@ha1:/home/zhoujy/increment_data# cat 2014-11-11_14-25-36/xtrabackup_checkpoints backup_type = incremental from_lsn = 7363106612 對應上一個備份的to_lsn to_lsn = 7363107216 last_lsn = 7363107216 compact = 0
要是在第2次作增量備份的時候 --incremental-basedir 指向全量備份,則第一次增量備份中的數據會被第2次包含,只須要還原一次就能夠恢復,如今則須要還原2次增量備份。
#還原
#還原全量備份: root@ha1:/home/zhoujy# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/ #第一次的增量備份應用日誌,應用完日誌後,將合併到全備上,恢復使用全備恢復 root@ha1:~# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/zhoujy/increment_data/2014-11-11_14-14-56/ #第二次的增量備份應用日誌,應用完日誌後,將合併到全備上,恢復使用全備恢復 root@ha1:~# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/zhoujy/increment_data/2014-11-11_14-25-36/ 此時兩次增量備份其實都合併到全備上了,恢復是隻須要使用全備進行恢復就能夠了
還原操做執行完以後,查看其lsn的信息:
root@ha1:/home/zhoujy/xtrabackup/2014-11-11_13-57-11# cat xtrabackup_checkpoints backup_type = full-prepared from_lsn = 0 to_lsn = 7363107216 last_lsn = 7363107216 compact = 0
說明了,還原操做執行沒問題。按照以前還原操做的步驟來恢復數據:先刪除數據,再把文件複製過去,看看是否成功。測試下來,還原成功。
3)壓縮備份:必須使用-i參數拆包解壓
打包(Tar)備份
innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --databases=xtra_test --stream=tar /home/zhoujy/xtrabackup/ 1>/home/zhoujy/xtrabackup/xtra_test.tar
拆包還原
root@ha1:/home/zhoujy/xtrabackup# tar ixvf xtra_test.tar ./backup-my.cnf ibdata1 xtra_test/db.opt xtra_test/M.MYI xtra_test/M.frm xtra_test/I.frm xtra_test/M.MYD ./xtrabackup_binlog_info xtrabackup_logfile xtrabackup_checkpoints ./xtrabackup_info
還原方法和上面同樣,就很少作說明了。
打包壓縮:
root@ha1:/home/zhoujy/xtrabackup# innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --databases=xtra_test --stream=tar /home/zhoujy/xtrabackup/ | gzip >/home/zhoujy/xtrabackup/xtra_test.tar.gz
解壓:
root@ha1:/home/zhoujy/xtrabackup# tar izxvf xtra_test.tar.gz ./backup-my.cnf ibdata1 xtra_test/db.opt xtra_test/M.MYI xtra_test/M.frm xtra_test/I.frm xtra_test/M.MYD ./xtrabackup_binlog_info xtrabackup_logfile xtrabackup_checkpoints ./xtrabackup_info
還原方法和上面同樣。
更多信息見:
http://mysql.taobao.org/monthly/2016/03/07/
http://op.baidu.com/2014/07/xtrabackup%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E6%96%BD/
http://www.drupal001.com/2014/02/percona-xtrabackup-mysql/
http://www.xuchanggang.cn/archives/700.html