熱備份與恢複
熱備份的方式也是直接複制資料實體檔案,和冷備份一樣,但是熱備份可以不停機直接複制,一般用于7*24小時不間斷的重要核心業務。Mysql的社群版本熱備份工具InnoDB Hot Backup是付費的,隻能試用30天,商業版才可以永久的使用,
Percona公司釋出一個xtrabackup熱備工具,和付費的工具一樣,支援線上熱備(備份時不影響資料的讀寫)是商業工具InnoDB Hot Backup的一個很好的替代。
xtrabackup熱備工具是一個開源的工具,他可以非常快速的備份和恢複mysql資料庫。Xrabackup 中包含兩個工具:
xtrabackup适用于熱備份InnoDB及XtraDB表中資料的工具,不能備份其他類型的表,也不能備份資料表結構
innobakupex是将xtrabackup進行封裝的perl腳本,他提供備份MyISAM表的能力,由于innobakupex功能更完善,是以一般采用innobakupex來進行備份,但在處理myisam時需要加一個讀鎖。并且加入了一些使用的選項。如slave-info可以記錄備份恢複後,作為slave需要的一些資訊,根據這些資訊,可以很友善的利用備份來重做slave。
Xtrabackup可以做什麼:
線上(熱)備份整個庫的InnoDB、 XtraDB表
在xtrabackup的上一次整庫備份基礎上做增量備份(innodbonly)
以流的形式産生備份,可以直接儲存到遠端機器上(本機硬碟空間不足時很有用)
MySQL資料庫本身提供的工具并不支援真正的增量備份,二進制日志恢複是point-in-time(時間點)的恢複而不是增量備份。Xtrabackup工具支援對InnoDB存儲引擎的增量備份,工作原理如下:
(1)首先完成一個完全備份,并記錄下此時檢查點的LSN(LogSequenceNumber)。
(2)在程序增量備份時,比較表空間中每個頁的LSN是否大于上次備份時的LSN,如果是,則備份該頁,同時記錄目前檢查點的LSN。
首先,在logfile中找到并記錄最後一個checkpoint(“last checkpoint LSN”),然後開始從LSN的位置開始拷貝InnoDB的logfile到xtrabackup_logfile;接着,開始拷貝全部的資料檔案.ibd;在拷貝全部資料檔案結束之後,才停止拷貝logfile。
因為logfile裡面記錄全部的資料修改情況,是以,即時在備份過程中資料檔案被修改過了,恢複時仍然能夠通過解析xtrabackup_logfile保持資料的一緻。
XtraBackup基于InnoDB的crash-recovery功能。它會複制innodb的datafile,由于不鎖表,複制出來的資料是不一緻的,在恢複的時候使用crash-recovery,使得資料恢複一緻。
InnoDB維護了一個redo log,又稱為transactionlog,事務日志,它包含了innodb資料的所有改動情況。當InnoDB啟動的時候,它會先去檢查data file和transaction log,并且會做二步操作:
XtraBackup在備份的時候,一頁一頁地複制innodb的資料,而且不鎖定表,與此同時,XtraBackup還有另外一個線程監視着transactions log,一旦log發生變化,就把變化過的logpages複制走。為什麼要急着複制走呢?因為transactions log檔案大小有限,寫滿之後,就會從頭再開始寫,是以新資料可能會覆寫到舊的資料。
在prepare過程中,XtraBackup使用複制到的transactions log對備份出來的innodb data file進行crash recovery。
XtraBackup以read-write模式打開innodb的資料檔案,然後對其進行複制。其實它不會修改此檔案。也就是說,運作XtraBackup的使用者,必須對innodb的資料檔案具有讀寫權限。之是以采用read-write模式是因為XtraBackup采用了其内置的 innodb庫來打開檔案,而innodb庫打開檔案的時候就是rw的。
XtraBackup要從檔案系統中複制大量的資料,是以它盡可能地使用posix_fadvise(),來告訴OS不要緩存讀取到的資料,進而提升性能。因為這些資料不會重用到了,OS卻沒有這麼聰明。如果要緩存一下的話,幾個G的資料,會對OS的虛拟記憶體造成很大的壓力,其它程序,比如mysqld很有可能被swap出去,這樣系統就會受到很大影響了。
在備份innodbpage的過程中,XtraBackup每次讀寫1MB的資料,1MB/16KB=64個page。這個不可配置。讀1MB資料之後,XtraBackup一頁一頁地周遊這1MB資料,使用innodb的buf_page_is_corrupted()函數檢查此頁的資料是否正常,如果資料不正常,就重新讀取這一頁,最多重新讀取10次,如果還是失敗,備份就失敗了,退出。在複制transactionslog的時候,每次讀寫512KB的資料。同樣不可以配置。
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/
[[email protected]_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
--defaults-file=/etc/my.cnf:指定my.cnf配置檔案位置
--user=root:指定連結資料庫的使用者名
--apply-log:對xtrabackup的—prepare參數的封裝
--copy-back:做資料恢複時把備份檔案拷貝到mysql伺服器的datadir目錄下
--remote-host:通過ssh将備份資料存儲到遠端的伺服器上
--stream:通過指定的資料格式将備份的資料輸出到标準輸出
--tmpdir:當指定了—remote-host或者是—stream參數後,事務日志需要臨時存儲到本地磁盤,此參數預設使用了mysql伺服器的配置
--use-memory:此參數結合ibbackup使用,類似于xrtabackup的參數use-memory參數
--throttle=IOS:同xtrabackup的參數throttle
--sleep:是給ibbackup使用的,指定每備份1M資料,程序停止考呗多少秒,也是為了減少對正常業務的影響,具體檢視ibbackup的手冊
-compass:對備份的資料進行壓縮,僅僅支援ibbackup,xtrabackup還沒有實作
--include=REGEXP對xtrabackup參數的封裝,也支援ibbackup;
--database=LIST :列出需要備份的databases,如果沒有制定該參數,則所有包含Myisam和innoDB表的database 都會被備份
--password="*****":通路mysql的使用者密碼,當mysql的root密碼為空時省略—password參數。
PORT=3306 :指定mysql監聽的端口
--slave-info:slave-info可以記錄備份恢 複後,作為slave需要的一些資訊,根據這些資訊,可以很友善的利用備份來重做slave。
/mysql_backup:備份位置
--socket=SOCKET :mysql伺服器的socket檔案的位置
2>/mysql_backup/ innobackupex.log:記錄備份時的輸出
--databases=資料庫名:使用這個參數,針對某個資料庫進行備份,如果不加這個參數預設就是全部的庫備份
innobackupex --defaults-file= /data/mysqldata/3307/my.cnf--user=root --password= zywerj@11erwe --databases="cooolyisydb" /backup/full_data/
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
建立備份資料存放的目錄:
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 (619371544)
[01] ...done
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...
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)
...............
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
備份資料恢複的報錯提示:
[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/
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
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的備份資料時,mysql環境必須是全新的環境,沒有任何資料的
資料目錄為/data/mysql,恢複之前必須把這個目錄下的檔案全部移走,然後把檔案/usr/local/mysql/var/ibdata1 移到其他的目錄,這樣才可以正常的恢複備份的資料庫
[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.
Get the latest version of Percona XtraBackup, documentation, and help resources:
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備份的速度快
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
[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
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
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/
[root@xtrabackup ~]# innobackupex --compress--stream=xbstream /root/backup/ | ssh [email protected] "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
innobackupex --defaults-file=/data/3307/my.cnf--user=system --password=123456 --databases="mahtu student"/mysql.test/
備份到/mysql.test/下
參考文檔:
http://www.cnblogs.com/galengao/p/5755835.html
<a href="https://yq.aliyun.com/articles/45746" target="_blank">https://yq.aliyun.com/articles/45746</a>
參考資料:
<a href="https://yq.aliyun.com/articles/45746?spm=5176.100239.blogcont47287.13.Hl2bkt" target="_blank">https://yq.aliyun.com/articles/45746?spm=5176.100239.blogcont47287.13.Hl2bkt</a>
本文轉自 wjw555 51CTO部落格,原文連結:http://blog.51cto.com/wujianwei/1934084