您好,登錄后才能下訂單哦!
xtrabackup是Percona公司CTO Vadim參與開發的一款基于InnoDB的在線熱備工具,具有開源,免費,支持在線熱備,備份恢復速度快,占用磁盤空間小等特點,并且支持不同情況下的多種備份形式。xtrabackup的官方下載地址為http://www.percona.com/software/percona-xtrabackup。
xtrabackup包含兩個主要的工具,即xtrabackup和innobackupex,二者區別如下:
(1)xtrabackup只能備份innodb和xtradb兩種引擎的表,而不能備份myisam引擎的表;
(2)innobackupex是一個封裝了xtrabackup的Perl腳本,支持同時備份innodb和myisam,但在對myisam備份時需要加一個全局的讀鎖。還有就是myisam不支持增量備份。
1.備份過程
innobackupex備份過程如下圖:
(圖1 innobackupex備份過程,本文中所有圖都是google所得)
在圖1中,備份開始時首先會開啟一個后臺檢測進程,實時檢測mysql redo的變化,一旦發現redo中有新的日志寫入,立刻將日志記入后臺日志文件xtrabackup_log中。之后復制innodb的數據文件和系統表空間文件ibdata1,待復制結束后,執行flush tables with read lock操作,復制.frm,MYI,MYD,等文件(執行flush tableswith read lock的目的是為了防止數據表發生DDL操作,并且在這一時刻獲得binlog的位置)最后會發出unlock tables,把表設置為可讀可寫狀態,最終停止xtrabackup_log。
2.全備恢復
這一階段會啟動xtrabackup內嵌的innodb實例,回放xtrabackup日志xtrabackup_log,將提交的事務信息變更應用到innodb數據/表空間,同時回滾未提交的事務(這一過程類似innodb的實例恢復)。恢復過程如下圖:
(圖2 innobackupex 恢復過程)
3.增量備份
innobackupex增量備份過程中的"增量"處理,其實主要是相對innodb而言,對myisam和其他存儲引擎而言,它仍然是全拷貝(全備份)
"增量"備份的過程主要是通過拷貝innodb中有變更的"頁"(這些變更的數據頁指的是"頁"的LSN大于xtrabackup_checkpoints中給定的LSN)。增量備份是基于全備的,第一次增備的數據必須要基于上一次的全備,之后的每次增備都是基于上一次的增備,最終達到一致性的增備。增量備份的過程如下,和全備的過程很類似,區別僅在第2步。
( 圖 3 innobackupex增量備份過程)
4.增量備份恢復
和全備恢復類似,也需要兩步,一是數據文件的恢復,如圖4,這里的數據來源由3部分組成:全備份,增量備份和xtrabackup log。二是對未提交事務的回滾,如圖5所示:
( 圖4 innobackupex 增量備份恢復過程1)
( 圖5 innobackupex增量備份恢復過程2)
5.innobackupex使用示例
(1)安裝使用xtrabackup,安裝比較簡單,我們使用二進制編譯好的就行了,這種工具無需源碼編譯,因為沒有什么功能需要特殊定制。
[root@MySQL01 ~]# wget http://www.percona.com/redir/downloads/XtraBackup/LATEST/binary/Linux/x86_64/percona-xtrabackup-2.1.8-733-Linux-x86_64.tar.gz
[root@MySQL-01 ~]# tar xf percona-xtrabackup-2.1.8-733-Linux-x86_64.tar.gz -C /usr/local/ [root@MySQL-01 ~]# mv /usr/local/percona-xtrabackup-2.1.8-Linux-x86_64/ /usr/local/xtrabackup [root@MySQL-01 ~]# echo "export PATH=\$PATH:/usr/local/xtrabackup/bin" >> /etc/profile [root@MySQL-01 ~]# source /etc/profile
(2)全量備份
創建備份用戶:
mysql> create user 'backup'@'%' identified by 'hello';
Query OK, 0 rows affected (0.01 sec) mysql> grant reload,lock tables,replication client,create tablespace,super on *.* to 'backup'@'%'; Query OK, 0 rows affected (0.00 sec) 進行全備份 備份數據存放在/data/backup/下面,innobackupex會自動創建一個文件夾,是當前系統的時間戳 mysql> select * from peng.t1; +------+-------+ | id | name | +------+-------+ | 1 | peng | | 2 | atlas | +------+-------+ 2 rows in set (0.00 sec) 測試數據就是peng庫中的t1表 [root@MySQL-01 ~]# xtrabackup: Creating suspend file '/data/backup/2014-04-07_23-05-04/xtrabackup_log_copied' with pid '57608' xtrabackup: Transaction log of lsn (5324782783) to (5324782783) was copied. 140407 23:06:14 innobackupex: All tables unlocked innobackupex: Backup created in directory '/data/backup/2014-04-07_23-05-04' innobackupex: MySQL binlog position: filename 'mysqlbin.000014', position 2983 140407 23:06:14 innobackupex: Connection to database server closed 140407 23:06:14 innobackupex: completed OK! 上面的過程中處理過程,主要看最后是否提示innobackupex completed ok,可以看見備份成功。我們看看/data/backup目錄下產生了什么! [root@MySQL-01 backup]# pwd /data/backup [root@MySQL-01 backup]# ll total 4 drwxr-xr-x 12 root root 4096 Apr 7 23:06 2014-04-07_23-05-04 [root@MySQL-01 backup]# cd 2014-04-07_23-05-04/ [root@MySQL-01 2014-04-07_23-05-04]# ll total 845888 -rw-r--r-- 1 root root 261 Apr 7 23:05 backup-my.cnf drwx------ 2 root root 4096 Apr 7 23:06 employees drwx------ 2 root root 4096 Apr 7 23:06 host -rw-r----- 1 root root 866123776 Apr 7 23:05 ibdata1 drwx------ 2 root root 4096 Apr 7 23:06 menagerie drwxr-xr-x 2 root root 4096 Apr 7 23:06 mysql drwxr-xr-x 2 root root 4096 Apr 7 23:06 performance_schema drwx------ 2 root root 4096 Apr 7 23:06 sakila drwx------ 2 root root 4096 Apr 7 23:06 test drwx------ 2 root root 4096 Apr 7 23:06 world_innodb drwxr-xr-x 2 root root 4096 Apr 7 23:06 world_myisam -rw-r--r-- 1 root root 13 Apr 7 23:06 xtrabackup_binary -rw-r--r-- 1 root root 24 Apr 7 23:06 xtrabackup_binlog_info -rw-r----- 1 root root 95 Apr 7 23:06 xtrabackup_checkpoints -rw-r----- 1 root root 2560 Apr 7 23:06 xtrabackup_logfile drwx------ 2 root root 4096 Apr 7 23:06 peng 可以看見有對應數據庫的名字,比如peng,還有一個以時間戳命名的目錄。我們看看對應文件里面的內容,這幾個比較重要: [root@MySQL-01 2014-04-07_23-05-04]# cat xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 5324782783 last_lsn = 5324782783 compact = 0 [root@MySQL-01 2014-04-07_23-05-04]# cat xtrabackup_binlog_info mysql-bin.000014 2983
可以看見相關文件記錄了LSN,日志偏移量,還可以看見這次是全備份,相信聰明的童鞋們一眼就看懂了。^_^
刪除數據庫,然后恢復全備(線上不要這樣搞)
mysql> drop database peng; Query OK, 1 row affected (0.04 sec)
恢復全備
恢復備份到mysql的數據文件目錄,這一過程要先關閉mysql數據庫,重命名或者刪除原數據文件目錄都可以,再創建一個新的數據文件目錄,將備份數據復制到新的數據文件目錄下,賦權,修改權限,啟動數據庫。
[root@MySQL-01 ~]# /etc/init.d/mysqld stop Shutting down MySQL..... [ OK ] [root@MySQL-01 ~]# mv /data/mysql /data/mysql_bak [root@MySQL-01 ~]# mkdir /data/mysql
[root@MySQL-01 ~]# innobackupex --apply-log /data/backup/2014-04-07_23-05-04/ xtrabackup: starting shutdown with innodb_fast_shutdown = 1 140407 23:22:36 InnoDB: Starting shutdown... 140407 23:22:40 InnoDB: Shutdown completed; log sequence number 5324784 140 140407 23:22:40 innobackupex: completed OK! 以上對應的目錄就是innobackupex全備份自己創建的目錄。 [root@MySQL-01 ~]# innobackupex: Starting to copy InnoDB log files innobackupex: in '/data/backup/2014-04-07_23-05-04' innobackupex: back to original InnoDB log directory '/data/mysql' innobackupex: Copying '/data/backup/2014-04-07_23-05-04/ib_logfile1' to '/data/mysql/ib_logfile1' innobackupex: Copying '/data/backup/2014-04-07_23-05-04/ib_logfile0' to '/data/mysql/ib_logfile0' innobackupex: Finished copying back files. 140407 23:27:38 innobackupex: completed OK! 可以看見已經成功恢復,修改數據目錄權限,啟動mysql,效驗數據是否正常,查看peng庫下面的t1表中的數據。 [root@MySQL-01 ~]# chown -R mysql.mysql /data/mysql [root@MySQL-01 ~]# /etc/init.d/mysqld start Starting MySQL................. [ OK ] mysql> use peng
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t1; +------+-------+ | id | name | +------+-------+ | 1 | peng | | 2 | atlas | +------+-------+ 2 rows in set (0.00 sec)
發現數據已經成功恢復。
(3)增量備份
在進行增量備份時,首先要進行一次全量備份,第一次增量備份是基于全備的,之后的增量備份是基于上一次的增量備份,以此類推。
全備份放在/data/backup/full,增量備份放在/data/backup/incremental
[root@MySQL-01 ~]# tree /data/backup/
/data/backup/ ├── full └── incremental 2 directories, 0 files 廢話少說,咱們先來一次全備份 [root@MySQL-01 ~]# innobackupex: Backup created in directory '/data/backup/full/2014-04-07_23-37-20' innobackupex: MySQL binlog position: filename 'mysqlbin.000001', position 107 140407 23:38:29 innobackupex: Connection to database server closed 140407 23:38:29 innobackupex: completed OK! 為了測試效果,我們在t1表中插入數據 mysql> select * from t1; +------+-------+ | id | name | +------+-------+ | 1 | peng | | 2 | atlas | +------+-------+ 2 rows in set (0.00 sec) mysql> insert into t1 select 1,'love sql'; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from t1; +------+----------+ | id | name | +------+----------+ | 1 | peng | | 2 | atlas | | 1 | love sql | +------+----------+ 3 rows in set (0.00 sec) 現在來一次增量備份1 [root@MySQL-01 ~]# innobackupex: Backup created in directory '/data/backup/incremental/2014-04-07_23-42-46' innobackupex: MySQL binlog position: filename 'mysqlbin.000001', position 301 140407 23:43:25 innobackupex: Connection to database server closed 140407 23:43:25 innobackupex: completed OK! 我們看看增量備份的大小以及文件內容 [root@MySQL-01 ~]# du -sh /data/backup/full/2014-04-07_23-37-20/ 1.2G /data/backup/full/2014-04-07_23-37-20/ [root@MySQL-01 ~]# du -sh /data/backup/incremental/2014-04-07_23-42-46/ 3.6M /data/backup/incremental/2014-04-07_23-42-46/ 看見增量備份的數據很小吧,就是備份改變的數據而已。 [root@MySQL-01 2014-04-07_23-42-46]# pwd /data/backup/incremental/2014-04-07_23-42-46 [root@MySQL-01 2014-04-07_23-42-46]# cat xtrabackup_checkpoints from_lsn = 5324784718 to_lsn = 5324785066 last_lsn = 5324785066 compact = 0 上面已經明顯說明是增量備份了,該工具很人性化吧,呵呵 我們再次向t1表插入數據,然后創建增量備份2 mysql> select * from t1; +------+----------+ | id | name | +------+----------+ | 1 | peng | | 2 | atlas | | 1 | love sql | +------+----------+ 3 rows in set (0.00 sec) mysql> insert into t1 select 1,'mysql dba'; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from t1; +------+-----------+ | id | name | +------+-----------+ | 1 | peng | | 2 | atlas | | 1 | love sql | | 1 | mysql dba | +------+-----------+ 4 rows in set (0.00 sec) 創建增量備份2(這次是基于上次的增量備份哦) [root@MySQL-01 ~]# innobackupex: Backup created in directory '/data/backup/incremental/2014-04-07_23-51-15' innobackupex: MySQL binlog position: filename 'mysqlbin.000001', position 496 140407 23:51:55 innobackupex: Connection to database server closed 140407 23:51:55 innobackupex: completed OK! [root@MySQL-01 ~]# ls -ltr /data/backup/full/ total 4 drwxr-xr-x 12 root root 4096 Apr 7 23:38 2014-04-07_23-37-20 [root@MySQL-01 ~]# ls -ltr /data/backup/incremental/ total 8 drwxr-xr-x 12 root root 4096 Apr 7 23:43 2014-04-07_23-42-46 drwxr-xr-x 12 root root 4096 Apr 7 23:51 2014-04-07_23-51-15 (4)增量備份恢復 增量備份的恢復大體為3個步驟 恢復完全備份(注意這里一定要加--redo-only參數,該參數的意思是只應用xtrabackup日志中已提交的事務數據,不回滾還未提交的數據) [root@MySQL-01 ~]# xtrabackup: starting shutdown with innodb_fast_shutdown = 1 140407 23:59:43 InnoDB: Starting shutdown... 140407 23:59:43 InnoDB: Shutdown completed; log sequence number 5324784718 140407 23:59:43 innobackupex: completed OK! 將增量備份1應用到完全備份 [root@MySQL-01 ~]# innobackupex: Copying '/data/backup/incremental/2014-04-07_23-42-46/mysql/func.frm' to '/data/backup/full/2014-04-07_23-37-20/mysql/func.frm' innobackupex: Copying '/data/backup/incremental/2014-04-07_23-42-46/mysql/help_relation.frm' to '/data/backup/full/2014-04-07_23-37-20/mysql/help_relation.frm' innobackupex: Copying '/data/backup/incremental/2014-04-07_23-42-46/mysql/help_category.MYD' to '/data/backup/full/2014-04-07_23-37-20/mysql/help_category.MYD' innobackupex: Copying '/data/backup/incremental/2014-04-07_23-42-46/mysql/ndb_binlog_index.frm' to '/data/backup/full/2014-04-07_23-37-20/mysql/ndb_binlog_index.frm' 140408 00:02:07 innobackupex: completed OK! 將增量備份2應用到完全備份(注意恢復最后一個增量備份時需要去掉--redo-only參數,回滾xtrabackup日志中那些還未提交的數據) [root@MySQL-01 ~]# innobackupex: Copying '/data/backup/incremental/2014-04-07_23-51-15/mysql/help_relation.frm' to '/data/backup/full/2014-04-07_23-37-20/mysql/help_relation.frm' innobackupex: Copying '/data/backup/incremental/2014-04-07_23-51-15/mysql/help_category.MYD' to '/data/backup/full/2014-04-07_23-37-20/mysql/help_category.MYD' innobackupex: Copying '/data/backup/incremental/2014-04-07_23-51-15/mysql/ndb_binlog_index.frm' to '/data/backup/full/2014-04-07_23-37-20/mysql/ndb_binlog_index.frm' 140408 00:04:33 innobackupex: completed OK! 把所有合在一起的完全備份整體進行一次apply操作,回滾未提交的數據: [root@MySQL-01 ~]# xtrabackup: starting shutdown with innodb_fast_shutdown = 1 140408 0:06:32 InnoDB: Starting shutdown... 140408 0:06:36 InnoDB: Shutdown completed; log sequence number 5324785676 140408 00:06:36 innobackupex: completed OK! 把恢復完的備份復制到數據庫目錄文件中,賦權,然后啟動mysql數據庫,檢測數據正確性 [root@MySQL-01 ~]# /etc/init.d/mysqld stop Shutting down MySQL. [ OK ] [root@MySQL-01 ~]# mv /data/mysql /data/mysql_bak [root@MySQL-01 ~]# mkdir /data/mysql [root@MySQL-01 ~]# innobackupex: Starting to copy InnoDB log files innobackupex: in '/data/backup/full/2014-04-07_23-37-20' innobackupex: back to original InnoDB log directory '/data/mysql'innobackupex: Copying '/data/backup/full/2014-04-07_23-37-20/ib_logfile1' to '/data/mysql/ib_logfile1' innobackupex: Copying '/data/backup/full/2014-04-07_23-37-20/ib_logfile0' to '/data/mysql/ib_logfile0' innobackupex: Finished copying back files. 140408 00:12:42 innobackupex: completed OK! [root@MySQL-01 ~]# chown -R mysql.mysql /data/mysql [root@MySQL-01 ~]# /etc/init.d/mysqld start Starting MySQL.... [ OK ]
查看數據是否正確
mysql> select * from t1;
+------+-----------+
| id | name |
+------+-----------+
| 1 | peng |
| 2 | atlas |
| 1 | love sql |
| 1 | mysql dba |
+------+-----------+
4 rows in set (0.00 sec)
(5)克隆slave
在日常工作中,我們有時候需要在線添加從庫,比如線上有一主一從兩個數據庫,但是由于業務的需要,一臺從庫的讀取無法滿足現在的需求,這樣就需要我們在線添加從庫,由于出于安全考慮,我們通常需要在從庫上進行在線克隆slave。
克隆slave時,常用參數--slave-info和--safe-slave-backup。
--slave-info會將master的binlog文件名和偏移量位置保存到xtrabackup_slave_info文件中
--safe-slave-backup會暫停slave的SQL線程直到沒有打開的臨時表的時候開始備份。備份結束后SQL線程會自動啟動,這樣操作的目的主要是確保一致性的復制狀態。
下面的例子,將介紹一主一從情況下在線搭建新的從庫,環境如下:
master 192.168.0.10 #主庫
slave 192.168.0.20 #從庫
newslave 192.168.0.100 # 新的從庫
在上述示例中,newslave即為要新搭建的從庫。在老的從庫上面進行備份:
[root@MySQL-02 ~]# innobackupex: Backup created in directory '/data/cloneslave' innobackupex: MySQL binlog position: filename 'mysql-bin.000022', position 107 innobackupex: MySQL slave binlog position: master host '192.168.0.10', filename 'mysql-bin.000006', position 732 140413 23:25:13 innobackupex: completed OK! 這里的/data/cloneslave 目錄要不存在,如果存在是會報錯的。 查看目錄下生成的文件: [root@MySQL-02 ~]# ll /data/cloneslave/ total 26668 -rw-r--r-- 1 root root 261 Apr 13 23:24 backup-my.cnf -rw-r--r-- 1 root root 27262976 Apr 13 23:24 ibdata1 drwxr-xr-x 2 root root 4096 Apr 13 23:25 mysql drwxr-xr-x 2 root root 4096 Apr 13 23:25 performance_schema drwxr-xr-x 2 root root 4096 Apr 13 23:25 sakila drwxr-xr-x 2 root root 4096 Apr 13 23:25 world_innodb -rw-r--r-- 1 root root 13 Apr 13 23:25 xtrabackup_binary -rw-r--r-- 1 root root 23 Apr 13 23:25 xtrabackup_binlog_info -rw-r--r-- 1 root root 79 Apr 13 23:25 xtrabackup_checkpoints -rw-r--r-- 1 root root 2560 Apr 13 23:25 xtrabackup_logfile -rw-r--r-- 1 root root 72 Apr 13 23:25 xtrabackup_slave_info drwxr-xr-x 2 root root 4096 Apr 13 23:25 peng 查看xtrabackup_slave_info文件內容,這個內容就是為搭建從庫時需要change master to的參數: [root@MySQL-02 ~]# cat /data/cloneslave/xtrabackup_slave_info CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=732 在老的slave服務器上進行還原,即192.168.0.20 [root@MySQL-02 ~]# xtrabackup: starting shutdown with innodb_fast_shutdown = 1 140413 23:30:37 InnoDB: Starting shutdown... 140413 23:30:37 InnoDB: Shutdown completed; log sequence number 12981048 140413 23:30:37 innobackupex: completed OK! 將還原的文件復制到新的從庫newslave,即192.168.0.100 [root@MySQL-02 data]# rsync -avprP -e ssh /data/cloneslave/ 192.168.0.100:/data/mysql/ 在主庫master上添加對新從庫newslave的授權: mysql> grant replication slave on *.* to 'repl'@'192.168.0.100' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) 拷貝老的從庫的配置文件到新的從庫newslave,并且修改server-id參數,修改完畢后,啟動新的從庫; [root@MySQL-02 data]# scp /etc/my.cnf 192.168.0.100:/etc/ root@192.168.0.100's password: my.cnf 100% 4881 4.8KB/s 00:00 [root@newslave mysql]# egrep 'log-slave|^server-id|skip_slave' /etc/my.cnf server-id = 3 skip_slave_start log-slave-updates=1 [root@newslave mysql]# chown -R mysql.mysql . [root@newslave mysql]# /etc/init.d/mysqld restart Shutting down MySQL. [ OK ] Starting MySQL.. [ OK ] 查找老的從庫備份后生成的xtrabackup_slave_info文件,提取其中的master_log_file和master_log_pos信息,然后在新的從庫上進行change master to操作: 在新的從庫上進行同步: mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.10',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=732; Query OK, 0 rows affected (0.09 sec) 啟動io線程和sql線程,并觀察復制是否正常: mysql> start slave; Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.10 Master_User: repl Master_Port: 3306 Connect_Retry: 2 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 1309 Relay_Log_File: MySQL-02-relay-bin.000002 Relay_Log_Pos: 830 Relay_Master_Log_File: mysql-bin.000006 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: peng.% Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1309 Relay_Log_Space: 989 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
查看主庫,發現已經有兩個線程(Binlog Dump)
mysql> show processlist\G
*************************** 1. row ***************************
Id: 8 User: slave Host: 192.168.0.20:44251 db: NULL Command: Binlog Dump Time: 1088 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 2. row *************************** Id: 9 User: root Host: localhost db: peng Command: Query Time: 0 State: NULL Info: show processlist *************************** 3. row *************************** Id: 10 User: repl Host: 192.168.0.100:45844 db: NULL Command: Binlog Dump Time: 124 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL3 rows in set (0.00 sec)
正常工作,到此在線克隆slave就結束啦。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。