溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

發(fā)布時間:2020-06-29 13:49:42 來源:網(wǎng)絡 閱讀:4326 作者:民工哥 欄目:MySQL數(shù)據(jù)庫

Xtrabackup介紹
Xtrabackup是由percona開源的免費數(shù)據(jù)庫熱備份軟件,它能對InnoDB數(shù)據(jù)庫和XtraDB存儲引擎的數(shù)據(jù)庫非阻塞地備份(對于MyISAM的備份同樣需要加表鎖);mysqldump備份方式是采用的邏輯備份,其最大的缺陷是備份和恢復速度較慢,如果數(shù)據(jù)庫大于50G,mysqldump備份就不太適合。
Xtrabackup安裝完成后有4個可執(zhí)行文件,其中2個比較重要的備份工具是innobackupex、xtrabackup
1)xtrabackup 是專門用來備份InnoDB表的,和mysql server沒有交互;
2)innobackupex 是一個封裝xtrabackup的Perl腳本,支持同時備份innodb和myisam,但在對myisam備份時需要加一個全局的讀鎖。
3)xbcrypt 加密解密備份工具
4)xbstream 流傳打包傳輸工具,類似tar

Xtrabackup優(yōu)點

1)備份速度快,物理備份可靠
2)備份過程不會打斷正在執(zhí)行的事務(無需鎖表)
3)能夠基于壓縮等功能節(jié)約磁盤空間和流量
4)自動備份校驗
5)還原速度快
6)可以流傳將備份傳輸?shù)搅硗庖慌_機器上
7)在不增加服務器負載的情況備份數(shù)據(jù)

Xtrabackup備份原理
備份原理
備份開始時首先會開啟一個后臺檢測進程,實時檢測mysq redo的變化,一旦發(fā)現(xiàn)有新的日志寫入,立刻將日志記入后臺日志文件xtrabackup_log中,之后復制innodb的數(shù)據(jù)文件一系統(tǒng)表空間文件ibdatax,復制結束后,將執(zhí)行flush tables with readlock,然后復制.frm MYI MYD等文件,最后執(zhí)行unlock tables,最終停止xtrabackup_log
輸出如下提示信息

xtrabackup: Transaction log of lsn (2543172) to (2543181) was copied.
171205 10:17:52 completed OK!

Xtrabackup安裝
下載安裝xtrabackup

wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/6/x86_64/Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@centos ~]# ll
total 703528
-rw-r--r-- 1 root root 654007697 Sep 27 09:18 mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
-rw-r--r-- 1 root root  65689600 Nov 30 00:11 Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@centos ~]# tar xf Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@centos ~]# yum install percona-xtrabackup-24-2.4.9-1.el6.x86_64.rpm -y
[root@centos ~]# which xtrabackup 
/usr/bin/xtrabackup
[root@centos ~]# innobackupex -v
innobackupex version 2.4.9 Linux (x86_64) (revision id: a467167cdd4)

#已經(jīng)安裝完成

創(chuàng)建測試數(shù)據(jù)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table T1 (name varchar(10) not null,sex varchar(10) not null);
Query OK, 0 rows affected (0.15 sec)

mysql> insert into T1 values('zhang','man');
Query OK, 1 row affected (0.01 sec)

mysql> insert into T1 values('zhan','man');
Query OK, 1 row affected (0.01 sec)

mysql> insert into T1 values('sun','woman');
Query OK, 1 row affected (0.00 sec)

mysql> select * from T1;
+-------+-------+
| name  | sex   |
+-------+-------+
| zhang | man   |
| zhan  | man   |
| sun   | woman |
+-------+-------+
3 rows in set (0.00 sec)

Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

全量備份與恢復
innobackupex --defaults-file=/etc/my.cnf --user=root --password="123456" --backup /root
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復
從備份過程截圖可以看出會創(chuàng)建一個時間的目錄

[root@VM_0_8_centos ~]# ll /root/2017-12-04_14-43-20/
total 12352
-rw-r----- 1 root root      425 Dec  4 13:57 backup-my.cnf
-rw-r----- 1 root root      322 Dec  4 13:57 ib_buffer_pool
-rw-r----- 1 root root 12582912 Dec  4 13:57 ibdata1
drwxr-x--- 2 root root     4096 Dec  4 13:57 mysql
drwxr-x--- 2 root root     4096 Dec  4 13:57 performance_schema
drwxr-x--- 2 root root    12288 Dec  4 13:57 sys
drwxr-x--- 2 root root     4096 Dec  4 13:57 test
-rw-r----- 1 root root       22 Dec  4 13:57 xtrabackup_binlog_info
-rw-r----- 1 root root      113 Dec  4 13:57 xtrabackup_checkpoints
-rw-r----- 1 root root      537 Dec  4 13:57 xtrabackup_info
-rw-r----- 1 root root     2560 Dec  4 13:57 xtrabackup_logfile

#這里面就是相關的備份文件,同樣也可以看到我們創(chuàng)建的庫的名稱
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

mysql> drop table T1;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from T1;
ERROR 1146 (42S02): Table 'test.T1' doesn't exist

[root@VM_0_8_centos ~]# innobackupex --apply-log /root/2017-12-04_14-43-20/

#使用此參數(shù)使用相關數(shù)據(jù)性文件保持一致性狀態(tài)
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

接下來準備恢復誤刪除數(shù)據(jù)
#恢復數(shù)據(jù)之前需要保證數(shù)據(jù)目錄是空的狀態(tài)
[root@centos ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /root/2017-12-04_13-57-29/
#具體看日志截圖
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

[root@centos ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@centos ~]# lsof -i :3306
COMMAND PID USER  FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 5935 mysql 21u  IPv6 21850  0t0  TCP *:mysql (LISTEN)
mysql> use test;
Database changed
mysql> select * from T1;
+-------+-------+
| name  | sex |
+-------+-------+
| zhang | man  |
| zhan  | man |
| sun  | woman |
+-------+-------+
3 rows in set (0.00 sec)

##恢復成功

Xtrabackup增量備份與恢復
#需要注意的是,增量備份僅能應用于InooDB或XtraDB表,對于MyISAM表,增量與全備相同

mysql> select * from T1;
+-------+-------+
| name | sex  |
+-------+-------+
| zhang | man  |
| zhan  | man  |
| sun  | woman |
| susun | woman |
| sige | man  |
| mgg  | man |
+-------+-------+
6 rows in set (0.00 sec)

創(chuàng)建用于增量備份的數(shù)據(jù),用來模擬刪除掉了全備后的數(shù)據(jù),能否通過增量備份文件來恢復
[root@Vcentos ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123456 --incremental /backup/ --incremental-basedir=/root/2017-12-04_13-57-29
#--incremental /backup/ 指定增量備份文件備份的目錄
#--incremental-basedir 指定上一次全備或增量備份的目錄
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

[root@Vcentos ~]# ll /backup/2017-12-05_09-27-06/
total 312
-rw-r----- 1 root root    425 Dec  5 09:27 backup-my.cnf
-rw-r----- 1 root root    412 Dec  5 09:27 ib_buffer_pool
-rw-r----- 1 root root 262144 Dec  5 09:27 ibdata1.delta
-rw-r----- 1 root root     44 Dec  5 09:27 ibdata1.meta
drwxr-x--- 2 root root   4096 Dec  5 09:27 mysql
drwxr-x--- 2 root root   4096 Dec  5 09:27 performance_schema
drwxr-x--- 2 root root  12288 Dec  5 09:27 sys
drwxr-x--- 2 root root   4096 Dec  5 09:27 test
-rw-r----- 1 root root     21 Dec  5 09:27 xtrabackup_binlog_info
-rw-r----- 1 root root    117 Dec  5 09:27 xtrabackup_checkpoints
-rw-r----- 1 root root    560 Dec  5 09:27 xtrabackup_info
-rw-r----- 1 root root   2560 Dec  5 09:27 xtrabackup_logfile
[root@centos ~]# cd /backup/2017-12-05_09-27-06/
[root@centos 2017-12-05_09-27-06]# cat  xtrabackup_binlog_info
mysql-bin.000001    945
[root@centos 2017-12-05_09-27-06]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2542843
to_lsn = 2547308
last_lsn = 2547317
compact = 0
recover_binlog_info = 0

刪除一條數(shù)據(jù)來測試增量恢復

mysql> delete  from T1 where name='susun';
Query OK, 1 row affected (0.06 sec)

Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復
增量恢復操作過程如下

[root@centos ~]# innobackupex --apply-log --redo-only /root/2017-12-04_13-57-29/
[root@centos ~]# innobackupex --apply-log --redo-only /root/2017-12-04_13-57-29/ --incremental-dir=/backup/2017-12-05_09-27-06/

恢復全部數(shù)據(jù)

[root@centos ~]#innobackupex --defaults-file=/etc/my.cnf --copy-back /root/2017-12-04_13-57-29/
[root@centos ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 
[root@centos ~]# lsof -i :3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  23217 mysql  21u  IPv6 283226  0t0  TCP *:mysql (LISTEN)

查看恢復的數(shù)據(jù)完整性
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

更多精彩內(nèi)容請關注 民工哥公眾號(mingogge-linux)
Xtrabackup實現(xiàn)數(shù)據(jù)的備份與恢復

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI