溫馨提示×

溫馨提示×

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

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

使用xtrabackup工具如何熱備和恢復mysql

發(fā)布時間:2020-05-26 11:41:40 來源:網絡 閱讀:257 作者:三月 欄目:數(shù)據庫

下文主要給大家?guī)硎褂脁trabackup工具如何熱備和恢復mysql,希望這些內容能夠帶給大家實際用處,這也是我編輯使用xtrabackup工具如何熱備和恢復mysql這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。

熱備份與恢復

3.1xtrabackup介紹:

熱備份的方式也是直接復制數(shù)據物理文件,和冷備份一樣,但是熱備份可以不停機直接復制,一般用于7*24小時不間斷的重要核心業(yè)務。Mysql的社區(qū)版本熱備份工具InnoDB Hot Backup是付費的,只能試用30天,商業(yè)版才可以永久的使用,

Percona公司發(fā)布一個xtrabackup熱備工具,和付費的工具一樣,支持在線熱備(備份時不影響數(shù)據的讀寫)是商業(yè)工具InnoDB Hot Backup的一個很好的替代。

xtrabackup熱備工具是一個開源的工具,他可以非常快速的備份和恢復mysql數(shù)據庫。Xrabackup 中包含兩個工具: 

  1. xtrabackup適用于熱備份InnoDB及XtraDB表中數(shù)據的工具,不能備份其他類型的表,也不能備份數(shù)據表結構

  2. innobakupex是將xtrabackup進行封裝的perl腳本,他提供備份MyISAM表的能力,由于innobakupex功能更完善,所以一般采用innobakupex來進行備份,但在處理myisam時需要加一個讀鎖。并且加入了一些使用的選項。如slave-info可以記錄備份恢復后,作為slave需要的一些信息,根據這些信息,可以很方便的利用備份來重做slave。

Xtrabackup可以做什么:

在線(熱)備份整個庫的InnoDB、 XtraDB表

在xtrabackup的上一次整庫備份基礎上做增量備份(innodbonly)

以流的形式產生備份,可以直接保存到遠程機器上(本機硬盤空間不足時很有用)

MySQL數(shù)據庫本身提供的工具并不支持真正的增量備份,二進制日志恢復是point-in-time(時間點)的恢復而不是增量備份。Xtrabackup工具支持對InnoDB存儲引擎的增量備份,工作原理如下:

(1)首先完成一個完全備份,并記錄下此時檢查點的LSN(LogSequenceNumber)。

(2)在進程增量備份時,比較表空間中每個頁的LSN是否大于上次備份時的LSN,如果是,則備份該頁,同時記錄當前檢查點的LSN。

  首先,在logfile中找到并記錄最后一個checkpoint(“l(fā)ast checkpoint LSN”),然后開始從LSN的位置開始拷貝InnoDB的logfile到xtrabackup_logfile;接著,開始拷貝全部的數(shù)據文件.ibd;在拷貝全部數(shù)據文件結束之后,才停止拷貝logfile。 

  因為logfile里面記錄全部的數(shù)據修改情況,所以,即時在備份過程中數(shù)據文件被修改過了,恢復時仍然能夠通過解析xtrabackup_logfile保持數(shù)據的一致。

3.2Xtrabackup備份原理

XtraBackup基于InnoDB的crash-recovery功能。它會復制innodb的datafile,由于不鎖表,復制出來的數(shù)據是不一致的,在恢復的時候使用crash-recovery,使得數(shù)據恢復一致。

InnoDB維護了一個redo log,又稱為transactionlog,事務日志,它包含了innodb數(shù)據的所有改動情況。當InnoDB啟動的時候,它會先去檢查data file和transaction log,并且會做二步操作:

XtraBackup在備份的時候,一頁一頁地復制innodb的數(shù)據,而且不鎖定表,與此同時,XtraBackup還有另外一個線程監(jiān)視著transactions log,一旦log發(fā)生變化,就把變化過的logpages復制走。為什么要急著復制走呢?因為transactions log文件大小有限,寫滿之后,就會從頭再開始寫,所以新數(shù)據可能會覆蓋到舊的數(shù)據。 

  在prepare過程中,XtraBackup使用復制到的transactions log對備份出來的innodb data file進行crash recovery。 

3.3實現(xiàn)細節(jié) 

XtraBackup以read-write模式打開innodb的數(shù)據文件,然后對其進行復制。其實它不會修改此文件。也就是說,運行XtraBackup的用戶,必須對innodb的數(shù)據文件具有讀寫權限。之所以采用read-write模式是因為XtraBackup采用了其內置的 innodb庫來打開文件,而innodb庫打開文件的時候就是rw的。 

XtraBackup要從文件系統(tǒng)中復制大量的數(shù)據,所以它盡可能地使用posix_fadvise(),來告訴OS不要緩存讀取到的數(shù)據,從而提升性能。因為這些數(shù)據不會重用到了,OS卻沒有這么聰明。如果要緩存一下的話,幾個G的數(shù)據,會對OS的虛擬內存造成很大的壓力,其它進程,比如mysqld很有可能被swap出去,這樣系統(tǒng)就會受到很大影響了。

在備份innodbpage的過程中,XtraBackup每次讀寫1MB的數(shù)據,1MB/16KB=64個page。這個不可配置。讀1MB數(shù)據之后,XtraBackup一頁一頁地遍歷這1MB數(shù)據,使用innodb的buf_page_is_corrupted()函數(shù)檢查此頁的數(shù)據是否正常,如果數(shù)據不正常,就重新讀取這一頁,最多重新讀取10次,如果還是失敗,備份就失敗了,退出。在復制transactionslog的時候,每次讀寫512KB的數(shù)據。同樣不可以配置。

3.4xtrabackup工具的安裝

1軟件下載地址

http://www.percona.com/downloads/軟件下載地址

2直接下載二進制包安裝

不編譯安裝,編譯安裝需要和MySQL源碼包配合。

wgethttp://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/binary/Linux/x86_64/percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz

tar zxvf percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz

[root@localhost bin]# cd/opt/percona-xtrabackup-2.1.9-Linux-x86_64/bin/

[root@localhostpercona-xtrabackup-2.1.9-Linux-x86_64]# ll bin/

總用量 114072

-rwxr-xr-x 1 root root   168821 5月   2 2014 innobackupex

lrwxrwxrwx 1 root root       12 4月   4 10:11 innobackupex-1.5.1 ->innobackupex

-rwxr-xr-x 1 root root  2226551 5月   2 2014 xbcrypt

-rwxr-xr-x 1 root root  2300304 5月   2 2014 xbstream

-rwxr-xr-x 1 root root 13178050 5月   2 2014 xtrabackup

-rwxr-xr-x 1 root root 16598470 5月   2 2014 xtrabackup_55

-rwxr-xr-x 1 root root 82321834 5月   2 2014 xtrabackup_56

cp innobackupex-1.5.1 /usr/bin/innobackupex

cp xtrabackup_55 /usr/bin/xtrabackup

cp xtrabackup /usr/bin

安裝相關插件

yum install perl-DBI -y

yum install perl-DBD-MySQL -y

yum install perl-Time-HiRes -y

yum install perl-IO-Socket-SSL –y

yum install perl-TermReadKey.x86_64 -y

3.5 innobackupex參數(shù)介紹:

--defaults-file=/etc/my.cnf:指定my.cnf配置文件位置

--user=root:指定鏈接數(shù)據庫的用戶名

--apply-log:對xtrabackup的—prepare參數(shù)的封裝

--copy-back:做數(shù)據恢復時把備份文件拷貝到mysql云服務器的datadir目錄下

--remote-host:通過ssh將備份數(shù)據存儲到遠程的云服務器

--stream:通過指定的數(shù)據格式將備份的數(shù)據輸出到標準輸出

--tmpdir:當指定了—remote-host或者是—stream參數(shù)后,事務日志需要臨時存儲到本地磁盤,此參數(shù)默認使用了mysql云服務器的配置

--use-memory:此參數(shù)結合ibbackup使用,類似于xrtabackup的參數(shù)use-memory參數(shù)

--throttle=IOS:同xtrabackup的參數(shù)throttle

--sleep:是給ibbackup使用的,指定每備份1M數(shù)據,進程停止考唄多少秒,也是為了減少對正常業(yè)務的影響,具體查看ibbackup的手冊

-compass:對備份的數(shù)據進行壓縮,僅僅支持ibbackup,xtrabackup還沒有實現(xiàn)

--include=REGEXP對xtrabackup參數(shù)的封裝,也支持ibbackup;

--database=LIST :列出需要備份的databases,如果沒有制定該參數(shù),則所有包含Myisam和innoDB表的database 都會被備份

--password="*****":訪問mysql的用戶口令,當mysql的root口令為空時省略—password參數(shù)。

PORT=3306 :指定mysql監(jiān)聽的端口

--slave-info:slave-info可以記錄備份恢 復后,作為slave需要的一些信息,根據這些信息,可以很方便的利用備份來重做slave。

/mysql_backup:備份位置

--socket=SOCKET :mysql云服務器的socket文件的位置 

2>/mysql_backup/ innobackupex.log:記錄備份時的輸出 

--databases=數(shù)據庫名:使用這個參數(shù),針對某個數(shù)據庫進行備份,如果不加這個參數(shù)默認就是全部的庫備份

innobackupex  --defaults-file= /data/mysqldata/3307/my.cnf--user=root --password= zywerj@11erwe --databases="cooolyisydb" /backup/full_data/

3.6實例演示操作:

innobackupex  --defaults-file= /data/mysqldata/3307/my.cnf--user=root --password= zywerj@11erwe --databases="cooolyisydb"  /backup/full_data/

[root@localhost backup]# innobackupex  --defaults-file= /data/mysqldata/3307/my.cnf--user=root --password=zywerj@11erwe --databases="cooolyisydb"/backup/full_data/

Can't locate Time/HiRes.pm in @INC (@INC contains:/usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at/usr/bin/innobackupex line 23.

BEGIN failed--compilation aborted at/usr/bin/innobackupex line 23. 

報錯解決辦法:

yum -y install perl-Time-HiRes

參考:http://blog.csdn.net/lwei_998/article/details/40896321

1.單庫備份及恢復過程:

創(chuàng)建備份數(shù)據存放的目錄:

mkdir -p/backup/{full_data,dk_data,zl_data}

注:full_data 全庫備份目錄

dk_data 單庫備份目錄

zl_data 增量備份目錄 

[root@localhost ~]# time innobackupex--defaults-file=/etc/my.cnf --user=root --password=Zy@634#R0ot--socket=/tmp/mysql.sock --port=3306 --databases="mahtu" --slave-info/backup/full_data/ 

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright2003, 2009 Innobase Oy

and Percona LLC and/or its affiliates2009-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

170404 13:28:55 innobackupex: Connecting to MySQL server with DSN'dbi:mysql:;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/tmp/mysql.sock'as 'root'  (using password: YES).

170404 13:28:55 innobackupex: Connected to MySQL server

170404 13:28:55 innobackupex: Executing a version check against the server...

170404 13:28:58 innobackupex: Done.

IMPORTANT: Please check that the backup runcompletes successfully.

           Atthe end of a successful backup run innobackupex

          prints "completed OK!". 

innobackupex: Using mysql server version 5.5.48 

innobackupex: Created backup directory /backup/full_data/2017-04-04_13-28-58 

170404 13:28:58 innobackupex: Starting ibbackup with command: xtrabackup_55  --defaults-file="/etc/my.cnf"  --defaults-group="mysqld" --backup--suspend-at-end --target-dir=/backup/full_data/2017-04-04_13-28-58 --tmpdir=/tmp

innobackupex: Waiting for ibbackup (pid=26398) tosuspend

innobackupex: Suspend file'/backup/full_data/2017-04-04_13-28-58/xtrabackup_suspended_2'

xtrabackup_55 version 2.1.9 for Percona Server5.5.35 Linux (x86_64) (revision id: 744)

xtrabackup: uses posix_fadvise().

xtrabackup: cd to /data/mysql/

xtrabackup: open files limit requested 65535, setto 65535

xtrabackup: using the following InnoDBconfiguration:

xtrabackup:  innodb_data_home_dir = /usr/local/mysql/var

xtrabackup:  innodb_data_file_path = ibdata1:10M:autoextend

xtrabackup:  innodb_log_group_home_dir = /usr/local/mysql/var

xtrabackup:  innodb_log_files_in_group = 2

xtrabackup:  innodb_log_file_size = 268435456

>> log scanned up to (619371504)

[01] Copying/usr/local/mysql/var/ibdata1 to /backup/full_data/2017-04-04_13-28-58/ibdata1

>> log scanned up to (619371504)

.......................

>> log scanned up to (619371544)

[01]       ...done

>> log scanned up to (619371544)

xtrabackup: Creating suspend file'/backup/full_data/2017-04-04_13-28-58/xtrabackup_suspended_2' with pid '26398' 

170404 13:29:13 innobackupex: Continuing after ibbackup has suspended

170404 13:29:13 innobackupex: Starting to lock all tables...

>> log scanned up to (619371544)

170404 13:29:13 innobackupex: All tables locked and flushed to disk

170404 13:29:13 innobackupex: Starting to backup non-InnoDB tables and files

innobackupex: in subdirectories of '/data/mysql/'

innobackupex: Backing up files'/data/mysql//mahtu/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}'(747 files)

>> log scanned up to (619371544)

...............

>> log scanned up to (619371544)

170404 13:29:19 innobackupex: Finished backing up non-InnoDB tables and files

170404 13:29:19 innobackupex: Waiting for log copying to finish

xtrabackup: The latest check point (forincremental): '619371544'

xtrabackup: Stopping log copying thread.

.>> log scanned up to (619371544)

xtrabackup: Creating suspend file'/backup/full_data/2017-04-04_13-28-58/xtrabackup_log_copied' with pid '26398'

xtrabackup: Transaction log of lsn (619371135) to(619371544) was copied.

170404 13:29:20 innobackupex: All tables unlocked 

innobackupex: Backup created in directory'/backup/full_data/2017-04-04_13-28-58'

innobackupex: MySQL slave binlog position: masterhost '192.168.10.20', filename 'mysql-bin.000001', position 35593612

170404 13:29:20 innobackupex: Connection to database server closed

170404 13:29:20 innobackupex: completed OK! 

real  0m25.738s

user 0m2.765s

sys   0m3.927s

備份數(shù)據恢復的報錯提示:

[root@localhost 2017-04-04_16-04-46]#innobackupex --defaults-file=/etc/my.cnf --copy-back  /backup/dk_data/2017-04-04_16-04-46/

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright2003, 2009 Innobase Oy

and Percona LLC and/or its affiliates2009-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 

IMPORTANT: Please check that the copy-back runcompletes successfully.

           Atthe end of a successful copy-back run innobackupex

           prints "completed OK!".

innobackupex: Error: Original data directory'/data/mysql/' is not empty! at /usr/bin/innobackupex line 2113.

root@localhost mysql]# ps -ef|grep"/data/mysql/"|grep -v grep

root    28100     1  0 14:01 pts/1    00:00:00 /bin/sh/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/--pid-file=/data/mysql//localhost.localdomain.pid

mysql   28598 28100 29 14:01 pts/1   01:21:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql--datadir=/data/mysql/ --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql--log-error=/data/mysql//localhost.localdomain.err --open-files-limit=65535--pid-file=/data/mysql//localhost.localdomain.pid --socket=/tmp/mysql.sock--port=3306

原因是xtrabackup在恢復mysql的備份數(shù)據時,mysql環(huán)境必須是全新的環(huán)境,沒有任何數(shù)據的

數(shù)據目錄為/data/mysql,恢復之前必須把這個目錄下的文件全部移走,然后把文件/usr/local/mysql/var/ibdata1 移到其他的目錄,這樣才可以正常的恢復備份的數(shù)據庫 

以下是恢復備份的數(shù)據庫cooolyisydb:

[root@localhost var]# time innobackupex--defaults-file=/etc/my.cnf --copy-back /backup/dk_data/2017-04-04_16-04-46/


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


IMPORTANT: Please check that the copy-back run completes successfully.


           At the end of a successful copy-back run innobackupex


           prints "completed OK!".


innobackupex: Starting to copy files in '/backup/dk_data/2017-04-04_16-04-46'


innobackupex: back to original data directory '/data/mysql/'


innobackupex: Copying '/backup/dk_data/2017-04-04_16-04-46/xtrabackup_slave_info' to '/data/mysql/xtrabackup_slave_info'


innobackupex: Creating directory '/data/mysql/cooolyisydb'


......................

innobackupex: Starting to copy InnoDB log files

innobackupex: in'/backup/dk_data/2017-04-04_16-04-46'

innobackupex: back to original InnoDB log directory'/usr/local/mysql/var'

innobackupex: Finished copying back files. 

170404 17:03:35 innobackupex: completed OK!

real  5m25.809s

user 0m0.303s

sys   0m13.316s

[root@localhost ~]# time mysqldump -uroot-p -B mahtu >test_mahtu.sql

Enter password:

real  0m36.932s

user 0m10.967s

sys   0m1.337s

得出結論:innobackupex 備份比mysqldump備份的速度快

3.7壓縮備份

xbstream 打包:

[root@localhost bin]# innobackupex  --defaults-file=/etc/my.cnf --user=root--password=Zy@66#erot --stream=xbstream --databases="cooolyisydb"/backup/full_data/ > /backup/full_data/backup.xbstream

--compress壓縮參數(shù)

[root@localhost full_data]# innobackupex  --defaults-file=/etc/my.cnf --user=root--password=Zy@66##erot --stream=xbstream --compress--databases="cooolyisydb" /backup/full_data/ >/backup/full_data/backup.xbstream

tar 壓縮方法:

innobackupex --defaults-file=/data/3307/my.cnf --user=system--password=test3306 --databases="oldboy xtra_test" --stream=tar/backup/data/yasuo/|gzip > /backup/data/yasuo/test.dir_$(date +%F).tar.gz

[root@localhost full_data]# innobackupex  --defaults-file=/etc/my.cnf --user=root--password=Zy@66##erot --stream=tar --databases="cooolyisydb"/backup/full_data/|gzip > /backup/full_data/cooolyisydb.$(date +%F).tar.gz

3.8解壓備份的方法:

xbstream 解壓備份方法:

[root@db02 yasuo]#  xbstream -x </backup/data/yasuo/backup.xbstream -C /mysql.test/ -C指定解壓的目錄 

tar解壓備份方法:

tar ixf /backup/data/yasuo/test.dir_2016-01-17.tar.gz -C /mysql.test/ 

3.9備份到遠端的方法

[root@xtrabackup ~]# innobackupex --compress--stream=xbstream /root/backup/ | ssh oldboy@192.168.1.123 "xbstream -x -C

/root/backup/"

比較三種壓縮的方式:

[root@localhost full_data]# du -sh *

5.8G          backup.xbstream

7.3G          backup.xbstream1

2.2G          cooolyisydb.2017-04-04.tar.gz

3.10innobackupex 也可以進行分庫分表備份

innobackupex  --defaults-file=/data/3307/my.cnf--user=system --password=123456 --databases="mahtu student"/mysql.test/

備份到/mysql.test/下

對于以上關于使用xtrabackup工具如何熱備和恢復mysql,大家是不是覺得非常有幫助。如果需要了解更多內容,請繼續(xù)關注我們的行業(yè)資訊,相信你會喜歡上這些內容的。

向AI問一下細節(jié)

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

AI