Xtrabackup是由percona提供的mysql數據庫備份工具,據官方介紹,這也是世界上唯一一款開源的可以對innodb和xtradb數據庫進行熱備的工具。特色:
(1)備份過程快速、可靠;
(2)備份過程不會打斷正在執行的事務;
(3)可以基於壓縮等功能節約磁盤空間和流量;
(4)自動實現備份檢驗;
(5)還原速度快;mysql
獲取: http://www.percona.com/software/percona-xtrabackup/sql
直接安裝 rpm 包:數據庫
[root@bogon ~]# yum localinstall percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm --skip-broken Loaded plugins: fastestmirror Setting up Local Package Process Examining percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm: percona-xtrabackup-24-2.4.3-1.el6.x86_64 Marking percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm to be installed Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: perl(DBD::mysql) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed --> Processing Dependency: perl(DBI::Const::GetInfoType) for package: perl-DBD-MySQL-4.013-3.el6.x86_64 --> Processing Dependency: perl(DBI) for package: perl-DBD-MySQL-4.013-3.el6.x86_64 --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed Packages skipped because of dependency problems: percona-xtrabackup-24-2.4.3-1.el6.x86_64 from /percona-xtrabackup-24-2.4.3-1.el6.x86_64 perl-DBD-MySQL-4.013-3.el6.x86_64 from srr perl-DBI-1.609-4.el6.x86_64 from srr [root@bogon ~]# yum localinstall percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm Loaded plugins: fastestmirror Setting up Local Package Process Examining percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm: percona-xtrabackup-24-2.4.3-1.el6.x86_64 Marking percona-xtrabackup-24-2.4.3-1.el6.x86_64.rpm to be installed Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: perl(DBD::mysql) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed --> Processing Dependency: perl(DBI::Const::GetInfoType) for package: perl-DBD-MySQL-4.013-3.el6.x86_64 --> Processing Dependency: perl(DBI) for package: perl-DBD-MySQL-4.013-3.el6.x86_64 --> Running transaction check ---> Package percona-xtrabackup-24.x86_64 0:2.4.3-1.el6 will be installed --> Processing Dependency: libev.so.4()(64bit) for package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed --> Finished Dependency Resolution Error: Package: percona-xtrabackup-24-2.4.3-1.el6.x86_64 (/percona-xtrabackup-24-2.4.3-1.el6.x86_64) Requires: libev.so.4()(64bit) You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest
報這個錯是由於沒有安裝epel-release服務器
yum install epel-release
以後則能夠正常安裝app
Xtabackup 的命令行工具: innobackupexide
innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/
進行備份的用戶最小權限爲: RELOAD, LOCK TABLES, REPLICATION CLIENT , 可單首創建一個最小權限用戶工具
使用innobakupex備份時,其會調用xtrabackup備份全部的InnoDB表,複製全部關於表結構定義的相關文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關文件,同時還會備份觸發器和數據庫配置信息相關的文件。這些文件會被保存至一個以時間命令的目錄中。ui
在備份的同時,innobackupex還會在備份目錄中建立以下文件:
(1)xtrabackup_checkpoints —— 備份類型(如徹底或增量)、備份狀態(如是否已經爲prepared狀態)和LSN(日誌序列號)範圍信息;命令行
每一個InnoDB頁(一般爲16k大小)都會包含一個日誌序列號,即LSN。LSN是整個數據庫系統的系統版本號,每一個頁面相關的LSN可以代表此頁面最近是如何發生改變的。rest
(2)xtrabackup_binlog_info —— mysql服務器當前正在使用的二進制日誌文件及至備份這一刻爲止二進制日誌事件的位置。
(3)xtrabackup_binlog_pos_innodb —— 二進制日誌文件及用於InnoDB或XtraDB表的二進制日誌文件的當前position。
(4)xtrabackup_binary —— 備份中用到的xtrabackup的可執行文件;
(5)backup-my.cnf —— 備份命令用到的配置選項信息;
在使用innobackupex進行備份時,還可使用--no-timestamp選項來阻止命令自動建立一個以時間命名的目錄;如此一來,innobackupex命令將會建立一個BACKUP-DIR目錄來存儲備份數據。
[root@bogon ~]# service mysqld start Starting MySQL (Percona Server) SUCCESS! [root@bogon ~]# mysql -h127.0.0.1 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.11-4 Percona Server (GPL), Release 4, Revision 5c940e1 Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database week1; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | week1 | +--------------------+ 5 rows in set (0.00 sec) mysql> grant all privileges on *.* to 'shiina'@'%' identified by 'shiina'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> \q Bye [root@bogon ~]# innobackupex --user=shiina --password=shiina /backup/ ... #省略不少不少 ... 160530 05:39:31 Finished backing up non-InnoDB tables and files 160530 05:39:31 Executing LOCK BINLOG FOR BACKUP... 160530 05:39:31 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS... xtrabackup: The latest check point (for incremental): '2491433' xtrabackup: Stopping log copying thread. .160530 05:39:31 >> log scanned up to (2491442) 160530 05:39:31 Executing UNLOCK BINLOG 160530 05:39:31 Executing UNLOCK TABLES 160530 05:39:31 All tables unlocked 160530 05:39:31 [00] Copying ib_buffer_pool to /backup/2016-05-30_05-39-26/ib_buffer_pool 160530 05:39:31 [00] ...done 160530 05:39:31 Backup created in directory '/backup/2016-05-30_05-39-26' 160530 05:39:31 [00] Writing backup-my.cnf 160530 05:39:31 [00] ...done 160530 05:39:31 [00] Writing xtrabackup_info 160530 05:39:31 [00] ...done xtrabackup: Transaction log of lsn (2491433) to (2491442) was copied. 160530 05:39:31 completed OK! [root@bogon ~]# ls /backup/2016-05-30_05-39-26/ backup-my.cnf ibdata1 performance_schema week1 xtrabackup_info ib_buffer_pool mysql sys xtrabackup_checkpoints xtrabackup_logfile
通常狀況下,在備份完成後,數據尚且不能用於恢復操做,由於備份的數據中可能會包含還沒有提交的事務或已經提交但還沒有同步至數據文件中的事務。所以,此時數據文件仍處理不一致狀態。「準備」的主要做用正是經過回滾未提交的事務及同步已經提交的事務至數據文件也使得數據文件處於一致性狀態。
innobakupex命令的--apply-log選項可用於實現上述功能。以下面的命令:
innobackupex --apply-log /path/to/BACKUP-DIR
若是執行正確,其最後輸出的幾行信息一般以下:
xtrabackup: starting shutdown with innodb_fast_shutdown = 1 120407 9:01:36 InnoDB: Starting shutdown... 120407 9:01:40 InnoDB: Shutdown completed; log sequence number 92036620 120407 09:01:40 innobackupex: completed OK!
在實現「準備」的過程當中,innobackupex一般還可使用--use-memory選項來指定其可使用的內存的大小,默認一般爲100M。若是有足夠的內存可用,能夠多劃分一些內存給prepare的過程,以提升其完成速度。
[root@bogon ~]# innobackupex --apply-log /backup/2016-05-30_05-39-26/ ... #又省略了不少 ... xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 2491944 160530 05:45:33 completed OK!
innobackupex命令的--copy-back選項用於執行恢復操做,其經過複製全部數據相關的文件至mysql服務器DATADIR目錄中來執行恢復過程。innobackupex經過backup-my.cnf來獲取DATADIR目錄的相關信息。
innobackupex --copy-back /path/to/BACKUP-DIR
若是執行正確,其輸出信息的最後幾行一般以下:
innobackupex: Starting to copy InnoDB log files innobackupex: in '/backup/2012-04-07_08-17-03' innobackupex: back to original InnoDB log directory '/mydata/data' innobackupex: Finished copying back files. 120407 09:36:10 innobackupex: completed OK!
請確保如上信息的最行一行出現「innobackupex: completed OK!」。
當數據恢復至DATADIR目錄之後,還須要確保全部數據文件的屬主和屬組均爲正確的用戶,如mysql,不然,在啓動mysqld以前還須要事先修改數據文件的屬主和屬組。如:
chown -R mysql:mysql /mydata/data/ [root@bogon ~]# rm -rf /data/mysql/* [root@bogon ~]# innobackupex --copy-back /backup/2016-05-30_05-39-26/ ... # 你懂 ... 160530 05:49:54 completed OK! [root@bogon ~]# chown -R mysql:mysql /data/mysql/* [root@bogon ~]# ll /data/mysql/ total 122912 -rw-r----- 1 mysql mysql 290 May 30 05:49 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 May 30 05:49 ibdata1 -rw-r----- 1 mysql mysql 50331648 May 30 05:49 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 May 30 05:49 ib_logfile1 -rw-r----- 1 mysql mysql 12582912 May 30 05:49 ibtmp1 drwxr-x--- 2 mysql mysql 4096 May 30 05:49 mysql drwxr-x--- 2 mysql mysql 4096 May 30 05:49 performance_schema drwxr-x--- 2 mysql mysql 12288 May 30 05:49 sys drwxr-x--- 2 mysql mysql 4096 May 30 05:49 week1 -rw-r----- 1 mysql mysql 421 May 30 05:49 xtrabackup_info [root@bogon ~]# mysql -h127.0.0.1 -p ... mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | week1 | +--------------------+ 5 rows in set (0.01 sec)
然而此時 mysql 服務卻出了問題 --> 服務沒法中止, 由於pid文件被刪除了, 找不到pid文件不知道mysql服務的進程號, 因此沒法結束mysql進程
殺死進程, 重啓服務, 一切正常
[root@bogon ~]# service mysqld restart ERROR! MySQL (Percona Server) PID file could not be found! Starting MySQL (Percona Server)... ERROR! The server quit without updating PID file (/data/mysql/bogon.pid). [root@bogon ~]# kill 1382 [root@bogon ~]# service mysqld start Starting MySQL (Percona Server). SUCCESS! [root@bogon ~]# mysql -h127.0.0.1 -p ... mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | week1 | +--------------------+ 5 rows in set (0.00 sec)
完成