轉(zhuǎn)載:https://www.cnblogs.com/zhoujinyi/p/4088866.html
一 簡(jiǎn)介:
Xtrabackup是一個(gè)對(duì)InnoDB做數(shù)據(jù)備份的工具,支持在線(xiàn)熱備份(備份時(shí)不影響數(shù)據(jù)讀寫(xiě))箩做,是商業(yè)備份工具InnoDB Hotbackup的一個(gè)很好的替代品莽红。它能對(duì)InnoDB和XtraDB存儲(chǔ)引擎的數(shù)據(jù)庫(kù)非阻塞地備份(對(duì)于MyISAM的備份同樣需要加表鎖)。XtraBackup支持所有的Percona Server邦邦、MySQL洞渤、MariaDB和Drizzle贺归。幾年前使用過(guò)汇四,但現(xiàn)在忘記的差不多了踏烙,所以就重新拾起看看。
xtrabackup有兩個(gè)主要的工具:xtrabackup黔龟、innobackupex
(1).xtrabackup只能備份InnoDB和XtraDB 兩種數(shù)據(jù)表
(2).innobackupex則封裝了xtrabackup,同時(shí)可以備份MyISAM數(shù)據(jù)表
Innobackupex完整備份后生成了幾個(gè)重要的文件:
xtrabackup_binlog_info:記錄當(dāng)前最新的LOG Position
xtrabackup_binlog_pos_innodb:innodb log postion
xtrabackup_checkpoints: 存放備份的起始位置beginlsn和結(jié)束位置endlsn,增量備份需要這個(gè)lsn[增量備份可以在這里面看from和to兩個(gè)值的變化]
Xtrabackup特點(diǎn):
(1)備份過(guò)程快速妇智、可靠
(2)備份過(guò)程不會(huì)打斷正在執(zhí)行的事務(wù)
(3)能夠基于壓縮等功能節(jié)約磁盤(pán)空間和流量
(4)自動(dòng)實(shí)現(xiàn)備份檢驗(yàn)
(5)還原速度快
二 安裝:?http://www.percona.com/downloads/
下載安裝:
1)直接下載二進(jìn)制包滥玷,不編譯,編譯需要和MySQL源碼包配合巍棱。
wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/binary/Linux/x86_64/percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz
1)tar zxvf percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz
2)ls-lh
drwxr-xr-x2root root4.0K? 5月22014 bin
drwxr-xr-x4root root4.0K? 5月22014 share3)ls-lh bin/-rwxr-xr-x1root root 165K? 5月22014 innobackupex
lrwxrwxrwx 1root root125月22014innobackupex-1.5.1-> innobackupex-rwxr-xr-x1root root2.2M5月22014 xbcrypt-rwxr-xr-x1root root2.2M5月22014 xbstream-rwxr-xr-x1root root? 13M? 5月22014 xtrabackup-rwxr-xr-x1root root? 16M? 5月22014 xtrabackup_55-rwxr-xr-x1root root? 79M? 5月22014 xtrabackup_564)cpinnobackupex-1.5.1/usr/bin/innobackupexcpxtrabackup_55 /usr/bin/xtrabackup
#cpxtrabackup /usr/bin
2)Ubuntu 下的安裝:http://www.percona.com/doc/percona-xtrabackup/2.1/installation/apt_repo.html
1:執(zhí)行
apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A2:在vi/etc/apt/sources.list 添加
deb http://repo.percona.com/aptVERSIONmaindeb-src http://repo.percona.com/aptVERSIONmain3:安裝
$ apt-get update
$ apt-getinstallpercona-xtrabackup
支持的版本有:
Debian
6.0 (squeeze)
7.0 (wheezy)
Ubuntu
10.04LTS (lucid)
12.04LTS (precise)
12.10 (quantal)
13.04 (raring)
13.10 (saucy)
把2中的VERSION 按照自己的系統(tǒng)把上面的代入惑畴。
deb http://repo.percona.com/aptprecisemaindeb-src http://repo.percona.com/aptprecisemain
# precise是Ubuntu-12.04的版本代號(hào),如果是其它系統(tǒng)版本航徙,需要更換如贷。
三 使用說(shuō)明:
安裝完之后會(huì)生成幾個(gè)工具:http://www.percona.com/doc/percona-xtrabackup/2.1/manual.html
innobackupex:這個(gè)是其實(shí)是下面三個(gè)工具的一個(gè)perl腳本封裝,可以備份MyISAM, InnoDB, XtraDB表到踏。但在處理Myisam時(shí)需要加一個(gè)讀鎖倒得。
xtrabackup:一個(gè)由C編譯而來(lái)的二進(jìn)制文件,只能備份InnoDB和XtraDB數(shù)據(jù)夭禽。
xbcrypt:用來(lái)加密或解密備份的數(shù)據(jù)。
xbstream:用來(lái)解壓或壓縮xbstream格式的壓縮文件谊路。
innobackupex :
xtrabackup命令只備份數(shù)據(jù)文件,并不備份數(shù)據(jù)表結(jié)構(gòu)(.frm)讹躯,所以使用xtrabackup恢復(fù)的時(shí)候必須有對(duì)應(yīng)表結(jié)構(gòu)文件(.frm)。用innobackupex命令缠劝,此命令相當(dāng)于冷備份潮梯,復(fù)制數(shù)據(jù)目錄的索引,數(shù)據(jù)惨恭,結(jié)構(gòu)文件秉馏,但會(huì)有短暫的鎖表(時(shí)間依賴(lài)于MyISAM大小)脱羡。
參數(shù):
--defaults-file:指定my.cnf參數(shù)文件的位置[此配置文件里必須指定datadir]--apply-log:同xtrabackup的--prepare參數(shù),一般情況下,在備份完成后萝究,數(shù)據(jù)尚且不能用于恢復(fù)操作,因?yàn)閭浞莸臄?shù)據(jù)中可能會(huì)包含尚未提交的事務(wù)或已經(jīng)提交但尚未同步至數(shù)據(jù)文件中的事務(wù)锉罐。因此帆竹,此時(shí)數(shù)據(jù) 文件仍處理不一致?tīng)顟B(tài)。--apply-log的作用是通過(guò)回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件使數(shù)據(jù)文件處于一致性狀態(tài)脓规。--copy-back:做數(shù)據(jù)恢復(fù)時(shí)將備份數(shù)據(jù)文件拷貝到MySQL服務(wù)器的datadir--remote-host=HOSTNAME: 通過(guò)ssh將備份數(shù)據(jù)存儲(chǔ)到進(jìn)程服務(wù)器上--stream=[tar]:備份文件輸出格式, 該文件可在XtarBackup binary文件中獲得. 在使用參數(shù)stream=tar備份的時(shí)候,你的xtrabackup_logfile可能會(huì)臨時(shí)放在/tmp目錄下,如果你備份的時(shí)候并發(fā)寫(xiě)入較大的話(huà),xtrabackup_logfile可能會(huì)很大(5G+),很可能會(huì)撐滿(mǎn)你的/tmp目錄,可以通過(guò)參數(shù)--tmpdir指定目錄來(lái)解決這個(gè)問(wèn)題.--tmpdir=DIRECTORY:當(dāng)有指定--remote-host or --stream時(shí), 事務(wù)日志臨時(shí)存儲(chǔ)的目錄, 默認(rèn)采用MySQL配置文件中所指定的臨時(shí)目錄tmpdir--redo-only --apply-log:強(qiáng)制備份日志時(shí)只redo,跳過(guò)rollback,這在做增量備份時(shí)非常必要--use-memory=*:該參數(shù)在prepare的時(shí)候使用,控制prepare時(shí)innodb實(shí)例使用的內(nèi)存--databases=LIST:列出需要備份的databases,如果沒(méi)有指定該參數(shù),所有包含MyISAM和InnoDB表的database都會(huì)被備份--slave-info:備份從庫(kù), 加上--slave-info備份目錄下會(huì)多生成一個(gè)xtrabackup_slave_info 文件, 這里會(huì)保存主日志文件以及偏移, 文件內(nèi)容類(lèi)似于:CHANGE MASTER TO MASTER_LOG_FILE='', MASTER_LOG_POS=0--socket=SOCKET:指定mysql.sock所在位置栽连,以便備份進(jìn)程登錄mysql.
更多參數(shù)見(jiàn):http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/innobackupex_option_reference.html
四 測(cè)試:
?1)全量備份&還原
? ?#初始化
mysql>createdatabasextra_testdefault charset utf8;
Query OK, 1row affected (0.00 sec)
mysql>use xtra_testDatabase changed
mysql>createtableM(idint,namevarchar(10))engine=myisam;
Query OK, 0rows affected (0.00 sec)
mysql>createtableI(idint,namevarchar(10))engine=innodb;
Query OK, 0rows affected (0.00 sec)
mysql>insertintoMvalues(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e');
Query OK, 5rows affected (0.00 sec)
Records: 5Duplicates:0Warnings:0mysql>insertintoIvalues(11,'A'),(22,'B'),(33,'C'),(44,'D'),(55,'E');
Query OK, 5rows affected (0.00 sec)
Records: 5Duplicates:0Warnings:0mysql>select*from M;+------+------+|id|name|+------+------+|1|a||2|b||3|c||4|d||5|e|+------+------+5rowsinset(0.00 sec)
mysql>select*from I;+------+------+|id|name|+------+------+|11|A||22|B||33|C||44|D||55|E|+------+------+5rowsinset(0.00sec)
??#備份
root@ha1:~#innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --databases=xtra_test? /home/zhoujy/xtrabackup/InnoDB BackupUtility v1.5.1-xtrabackup; Copyright2003,2009 Innobase OyandPercona LLCand/orits affiliates2009-2013.All Rights Reserved.
This software is published under
the GNU GENERAL PUBLICLICENSE Version2, June1991.
Get the latest version ofPercona XtraBackup, documentation,and help resources:
http://www.percona.com/xb/p14111111:56:58innobackupex: ConnectingtoMySQL serverwithDSN'dbi:mysql:;mysql_read_default_file=/etc/mysql/my.cnf;mysql_read_default_group=xtrabackup;host=192.168.200.51'as'zjy'? (using password: YES).14111111:56:58innobackupex: Connectedto MySQL server14111111:56:58innobackupex: Executing a versioncheck against the server...14111111:56:58? innobackupex: Done.14111111:56:58innobackupex: Starting thebackup operation
IMPORTANT: Please checkthat thebackup run completes successfully.
? ? ? ? ? At the endofa successfulbackup run innobackupex
? ? ? ? ? prints "completed OK!".
innobackupex:? Using server version 5.5.38-0ubuntu0.12.04.1-loginnobackupex: Created backupdirectory/home/zhoujy/xtrabackup/2014-11-11_11-56-5814111111:56:58innobackupex: Starting ibbackupwithcommand: xtrabackup--defaults-file="/etc/mysql/my.cnf"? --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58 --innodb_log_file_size="5242880" --innodb_data_file_path="ibdata1:10M:autoextend" --tmpdir=/tmp --extra-lsndir='/tmp' --databases='xtra_test'innobackupex: Waitingforibbackup (pid=10539)to suspend
innobackupex: Suspend file'/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2'xtrabackup version 2.2.6basedonMySQL server5.6.21 Linux (x86_64) (revision id: )
xtrabackup: uses posix_fadvise().
xtrabackup: cd to/var/lib/mysql
xtrabackup: openfiles limit requested0,setto1024xtrabackup: using the following InnoDB configuration:
xtrabackup:? innodb_data_home_dir =./xtrabackup:? innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:? innodb_log_group_home_dir =./xtrabackup:? innodb_log_files_in_group =2xtrabackup:? innodb_log_file_size =5242880>>logscanned upto(7363097930)
xtrabackup: Generating a list of tablespaces[01]Copying ./ibdata1to/home/zhoujy/xtrabackup/2014-11-11_11-56-58/ibdata1>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)>>logscanned upto(7363097930)[01]? ? ? ? ...done>>logscanned upto(7363097930)
xtrabackup: Creating suspend file'/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2'withpid'10540'14111111:57:11? innobackupex: Continuing after ibbackup has suspended14111111:57:11innobackupex: Executing FLUSH TABLESWITHREAD LOCK...14111111:57:11innobackupex:Alltables lockedandflushedtodisk14111111:57:11innobackupex: Startingtobackupnon-InnoDB tablesand files
innobackupex: insubdirectoriesof'/var/lib/mysql/'innobackupex: Backing up file'/var/lib/mysql//xtra_test/db.opt'innobackupex: Backing up file'/var/lib/mysql//xtra_test/M.MYI'>>logscanned upto(7363097930)
innobackupex: Backing up file'/var/lib/mysql//xtra_test/M.frm'innobackupex: Backing up file'/var/lib/mysql//xtra_test/I.frm'innobackupex: Backing up file'/var/lib/mysql//xtra_test/M.MYD'14111111:57:11innobackupex: Finished backing up non-InnoDB tablesand files14111111:57:11? innobackupex: Executing FLUSH ENGINE LOGS...14111111:57:11innobackupex: Waitingforlogcopyingto finish
xtrabackup: The latest checkpoint (forincremental):'7363097930'xtrabackup: Stopping log copying thread.
.>>logscanned upto(7363097930)
xtrabackup: Creating suspend file'/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_log_copied'withpid'10540'xtrabackup: Transactionlogoflsn (7363097930)to(7363097930) was copied.14111111:57:12innobackupex:All tables unlocked
innobackupex: Backupcreatedindirectory'/home/zhoujy/xtrabackup/2014-11-11_11-56-58'innobackupex: MySQL binlog position: filename 'mysql-bin51.000001', position96714111111:57:12innobackupex: Connectiontodatabase server closed14111111:57:12innobackupex: completed OK!
查看備份出來(lái)的文件:
ls-lh2014-11-11_11-56-58/total 1.2G-rw-r--r-- 1 root root? 188 11月 11 11:56 backup-my.cnf-rw-r----- 1 root root 1.2G 11月 11 11:57 ibdata1-rw-r--r-- 1 root root? 25 11月 11 11:57 xtrabackup_binlog_info-rw-r----- 1 root root? 95 11月 11 11:57 xtrabackup_checkpoints-rw-r--r-- 1 root root? 666 11月 11 11:57 xtrabackup_info-rw-r----- 1 root root 2.5K 11月 11 11:57 xtrabackup_logfiledrwxr-xr-x2root root4.0K 11月1111:57xtra_test
/home/zhoujy/xtrabackup/ 備份存放的位置,備份會(huì)在該目錄下生成一個(gè)按照時(shí)間命名的文件夾侨舆。用--no-timestamp參數(shù)可以指定到自己想要的備份文件夾秒紧,不受時(shí)間命名的文件夾限制。
利用?--apply-log的作用是通過(guò)回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件使數(shù)據(jù)文件處于一致性狀態(tài)挨下。
root@ha1:/home/zhoujy/xtrabackup#innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_11-56-58/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, June1991.
Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p14111112:02:42innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
? ? ? ? ? At the end of a successful apply-log run innobackupex
? ? ? ? ? prints "completed OK!".14111112:02:42innobackupex: Starting ibbackup with command: xtrabackup? --defaults-file="/home/zhoujy/xtrabackup/2014-11-11_11-56-58/backup-my.cnf"--defaults-group="mysqld"--prepare --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58xtrabackup version 2.2.6based on MySQL server5.6.21Linux (x86_64) (revisionid: )
xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(7363097930)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:? innodb_data_home_dir = ./xtrabackup:? innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:? innodb_log_group_home_dir = ./xtrabackup:? innodb_log_files_in_group =1xtrabackup:? innodb_log_file_size =2097152xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:? innodb_data_home_dir = ./xtrabackup:? innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:? innodb_log_group_home_dir = ./xtrabackup:? innodb_log_files_in_group =1xtrabackup:? innodb_log_file_size =2097152xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600bytesforbuffer pool (set by --use-memory parameter)
InnoDB: Using atomics to ref count buffer pool pages
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Memory barrier is not used
InnoDB: Compressed tables use zlib 1.2.3.4InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size =100.0MInnoDB: Completed initialization of buffer pool
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 7363094028and7363094028inibdata filesdonot match the log sequence number7363097930inthe ib_logfiles!InnoDB: Database was not shutdown normally!InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer...
InnoDB: Last MySQL binlog fileposition0967,filename /var/log/mysql/mysql-bin51.000001InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.21started; log sequence number7363097930[notice (again)]
? If you use binary log and don't use any hack of group commit,? the binary log position seems to be:
InnoDB: Last MySQL binlog fileposition0967,filename /var/log/mysql/mysql-bin51.000001xtrabackup: starting shutdown with innodb_fast_shutdown =1InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 736310060314111112:02:44innobackupex: Restarting xtrabackup with command: xtrabackup? --defaults-file="/home/zhoujy/xtrabackup/2014-11-11_11-56-58/backup-my.cnf"--defaults-group="mysqld"--prepare --target-dir=/home/zhoujy/xtrabackup/2014-11-11_11-56-58forcreating ib_logfile*xtrabackup version 2.2.6based on MySQL server5.6.21Linux (x86_64) (revisionid: )
xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58xtrabackup: This target seems to be already prepared.
xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:? innodb_data_home_dir = ./xtrabackup:? innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:? innodb_log_group_home_dir = ./xtrabackup:? innodb_log_files_in_group =2xtrabackup:? innodb_log_file_size =5242880xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup:? innodb_data_home_dir = ./xtrabackup:? innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup:? innodb_log_group_home_dir = ./xtrabackup:? innodb_log_files_in_group =2xtrabackup:? innodb_log_file_size =5242880xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600bytesforbuffer pool (set by --use-memory parameter)
InnoDB: Using atomics to ref count buffer pool pages
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Memory barrier is not used
InnoDB: Compressed tables use zlib 1.2.3.4InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size =100.0MInnoDB: Completed initialization of buffer pool
InnoDB: Setting log file./ib_logfile101 size to5 MB
InnoDB: Setting log file./ib_logfile1 size to5 MB
InnoDB: Renaming log file./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=7363100603InnoDB: Highest supported file format is Barracuda.
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.21started; log sequence number7363100684[notice (again)]
? If you use binary log and don't use any hack of group commit,? the binary log position seems to be:
InnoDB: Last MySQL binlog fileposition0967,filename /var/log/mysql/mysql-bin51.000001xtrabackup: starting shutdown with innodb_fast_shutdown =1InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 736310341714111112:02:45innobackupex: completed OK!
應(yīng)用完之后再查看備份文件看是否有變化:
root@ha1:/home/zhoujy/xtrabackup#ls-lh2014-11-11_11-56-58/total 1.2G-rw-r--r--1root root18811月1111:56backup-my.cnf-rw-r-----1root root1.2G 11月1112:02 ibdata1-rw-r--r--1root root5.0M11月1112:02 ib_logfile0-rw-r--r--1root root5.0M11月1112:02 ib_logfile1-rw-r--r--1root root2511月1111:57 xtrabackup_binlog_info-rw-r--r--1root root3811月1112:02 xtrabackup_binlog_pos_innodb-rw-r-----1root root9511月1112:02 xtrabackup_checkpoints-rw-r--r--1root root66611月1111:57 xtrabackup_info-rw-r-----1root root2.0M11月1112:02 xtrabackup_logfile
drwxr-xr-x2root root4.0K 11月1111:57xtra_test
看到redo log已經(jīng)出現(xiàn)熔恢。xtrabackup_開(kāi)頭的幾個(gè)文件記錄了一些日志偏移量的信息和日志名和時(shí)間等信息。在xtrabackup_checkpoints中記錄了備份的模式:
backup_type = full-backuped臭笆。
xtrabackup_binlog_info:記錄當(dāng)前最新的LOG Position
xtrabackup_binlog_pos_innodb:innodb log postion
xtrabackup_checkpoints: 存放備份的起始位置beginlsn和結(jié)束位置endlsn,增量備份需要這個(gè)lsn[增量備份可以在這里面看from和to兩個(gè)值的變化
xtrabackup_info:備份的一些具體信息日志
?#還原
mysql>dropdatabase xtra_test;
Query OK, 2rows affected (0.00sec)
1) 刪除原始目錄里的文件:
root@ha1:/var/lib/mysql#rmib_logfile0 ib_logfile1 ibdata1
關(guān)閉數(shù)據(jù)庫(kù):1) root@ha1:/var/lib/mysql# /etc/init.d/mysql stop
開(kāi)始恢復(fù)2)1:innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /home/zhoujy/xtrabackup/2014-11-11_11-56-58/? 執(zhí)行上面的命令需要保證數(shù)據(jù)庫(kù)目錄是空的绩聘,否則會(huì)失敗沥割,因?yàn)槟夸浝镉衎inlog日志,要么移走凿菩,要么就用手動(dòng)復(fù)制需要的文件机杜。所以說(shuō)日志不要和數(shù)據(jù)放在一起!
? 2:cpib_logfile0 ib_logfile1 ibdata1 /var/lib/mysql/cp-R xtra_test /var/lib/mysql/修改權(quán)限和開(kāi)啟數(shù)據(jù)庫(kù)3)? root@ha1:/var/lib/mysql#chown-R mysql.mysql ib*? ? root@ha1:/var/lib/mysql#chown-R mysql.mysql xtra_test/開(kāi)啟數(shù)據(jù)庫(kù)
? ? root@ha1:/var/lib/mysql# /etc/init.d/mysql start
? ? ...
? ? mysql start/running, process15937
數(shù)據(jù)已恢復(fù):
mysql>use xtra_testDatabase changed
mysql>select*from I;+------+------+|id|name|+------+------+|11|A||22|B||33|C||44|D||55|E|+------+------+5rowsinset(0.00sec)
2)增量備份&還原
?#備份
在做增量備份的時(shí)候需要做一次全量備份衅谷,和上面一樣椒拗,只是針對(duì)所有庫(kù)的備份:
root@ha1:~#innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf /home/zhoujy/xtrabackup/...
innobackupex: MySQL binlog position: filename
'mysql-bin51.000002', position10714111113:57:24? innobackupex: Connection to database server closed14111113:57:24innobackupex: completed OK!
增量備份這里開(kāi)始
1.數(shù)據(jù)庫(kù)操作:
mysql>select*from I;+------+------+|id|name|+------+------+|11|A||22|B||33|C||44|D||55|E|+------+------+5rowsinset(0.00 sec)
mysql>insertintoIvalues(111,'A'),(222,'B'),(333,'C'),(444,'D'),(555,'E');
Query OK, 5rows affected (0.01 sec)
Records: 5Duplicates:0Warnings:0mysql>select*from M;+------+------+|id|name|+------+------+|1|a||2|b||3|c||4|d||5|e|+------+------+5rowsinset(0.00 sec)
mysql>updateMsetname=upper(name);
Query OK, 5rows affected (0.00 sec)
Rows matched: 5Changed:5Warnings:0mysql>createtableX(namevarchar(20))default charset utf8;
Query OK, 0rows affected (0.01 sec)
mysql>insertintoXvalues('zjy');
Query OK, 1row affected (0.00sec)
2:增量備份(基于全量的增量備份)
root@ha1:~#innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --incremental --incremental-basedir=/home/zhoujy/xtrabackup/2014-11-11_13-57-11/? /home/zhoujy/increment_data/...
...
trabackup: Creating suspend file'/home/zhoujy/increment_data/2014-11-11_14-14-56/xtrabackup_log_copied'with pid'19492'xtrabackup: Transaction log of lsn (7363106612) to (7363106612) was copied.14111114:15:06? innobackupex: All tables unlocked
innobackupex: Backup created indirectory'/home/zhoujy/increment_data/2014-11-11_14-14-56'innobackupex: MySQL binlog position: filename 'mysql-bin51.000002', position98114111114:15:06? innobackupex: Connection to database server closed14111114:15:06innobackupex: completed OK!
?其中,--incremental指明是增量備份获黔,--incremental-basedir指定上次完整備份或者增量備份文件的位置蚀苛。這里的增量備份其實(shí)只針對(duì)的是InnoDB,對(duì)于MyISAM來(lái)說(shuō)玷氏,還是完整備份堵未。
增量備份的文件:
root@ha1:/home/zhoujy/increment_data#ls-lh2014-11-11_14-14-56/total 440K-rw-r--r--1root root18811月1114:14backup-my.cnf-rw-r-----1root root 400K 11月1114:15 ibdata1.delta-rw-r-----1root root4411月1114:14 ibdata1.meta
drwxr-xr-x2root root4.0K 11月1114:15 mha_test
drwxr-xr-x2root root4.0K 11月1114:15 mysql
drwxr-xr-x2root root4.0K 11月1114:15 performance_schema-rw-r--r--1root root2511月1114:15 xtrabackup_binlog_info-rw-r-----1root root10211月1114:15 xtrabackup_checkpoints-rw-r--r--1root root73811月1114:15 xtrabackup_info-rw-r-----1root root2.5K 11月1114:15 xtrabackup_logfile
drwxr-xr-x2root root4.0K 11月1114:15 xtra_test
root@ha1:/home/zhoujy/increment_data#cat2014-11-11_14-14-56/xtrabackup_checkpointsbackup_type = incrementalfrom_lsn =7363103427? 對(duì)應(yīng)增量中的to_lsnto_lsn =7363106612last_lsn =7363106612compact =0
3:繼續(xù)增量備份
數(shù)據(jù)庫(kù)操作
mysql>insertintoXvalues('dxy');
Query OK, 1row affected (0.01 sec)
mysql>insertintoXvalues('浙江');
Query OK, 1row affected (0.01sec)
增量備份(基于增量的增量備份)
root@ha1:~#innobackupex --user=zjy --password=123456# --host=192.168.200.51 --defaults-file=/etc/mysql/my.cnf --incremental --incremental-basedir=/home/zhoujy/increment_data/2014-11-11_14-14-56/? /home/zhoujy/increment_data/14111114:25:46? innobackupex: Connection to database server closed14111114:25:46innobackupex: completed OK!
增量備份的文件
root@ha1:/home/zhoujy/increment_data#cat2014-11-11_14-25-36/xtrabackup_checkpoints
backup_type = incremental
from_lsn =7363106612? ? ? 對(duì)應(yīng)上一個(gè)備份的to_lsnto_lsn =7363107216last_lsn =7363107216compact =0
要是在第2次做增量備份的時(shí)候--incremental-basedir 指向全量備份,則第一次增量備份中的數(shù)據(jù)會(huì)被第2次包含盏触,只需要還原一次就可以恢復(fù)渗蟹,現(xiàn)在則需要還原2次增量備份。
#還原
#還原全量備份:
root@ha1:/home/zhoujy# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/#第一次的增量備份應(yīng)用日志,應(yīng)用完日志后,將合并到全備上,恢復(fù)使用全備恢復(fù)?
root@ha1:~# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/zhoujy/increment_data/2014-11-11_14-14-56/#第二次的增量備份應(yīng)用日志,應(yīng)用完日志后,將合并到全備上,恢復(fù)使用全備恢復(fù)?
root@ha1:~# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/ --incremental-dir=/home/zhoujy/increment_data/2014-11-11_14-25-36/此時(shí)兩次增量備份其實(shí)都合并到全備上了,恢復(fù)是只需要使用全備進(jìn)行恢復(fù)就可以了
還原操作執(zhí)行完之后赞辩,查看其lsn的信息:
root@ha1:/home/zhoujy/xtrabackup/2014-11-11_13-57-11#cat xtrabackup_checkpoints
backup_type = full-prepared
from_lsn =0to_lsn =7363107216last_lsn =7363107216compact =0
說(shuō)明了雌芽,還原操作執(zhí)行沒(méi)問(wèn)題。按照之前還原操作的步驟來(lái)恢復(fù)數(shù)據(jù):先刪除數(shù)據(jù)辨嗽,再把文件復(fù)制過(guò)去世落,看看是否成功。測(cè)試下來(lái)糟需,還原成功屉佳。
3)壓縮備份:必須使用-i參數(shù)拆包解壓
打包(Tar)備份
innobackupex --user=zjy --password=123456# --host=192.168.200.51--defaults-file=/etc/mysql/my.cnf --databases=xtra_test--stream=tar/home/zhoujy/xtrabackup/1>/home/zhoujy/xtrabackup/xtra_test.tar
拆包還原
root@ha1:/home/zhoujy/xtrabackup#tarixvfxtra_test.tar
./backup-my.cnf
ibdata1
xtra_test/db.opt
xtra_test/M.MYI
xtra_test/M.frm
xtra_test/I.frm
xtra_test/M.MYD
./xtrabackup_binlog_info
xtrabackup_logfile
xtrabackup_checkpoints
./xtrabackup_info
還原方法和上面一樣,就不多做說(shuō)明了洲押。
打包壓縮:
root@ha1:/home/zhoujy/xtrabackup# innobackupex --user=zjy --password=123456# --host=192.168.200.51--defaults-file=/etc/mysql/my.cnf --databases=xtra_test--stream=tar/home/zhoujy/xtrabackup/|gzip>/home/zhoujy/xtrabackup/xtra_test.tar.gz
解壓:
root@ha1:/home/zhoujy/xtrabackup#tarizxvfxtra_test.tar.gz
./backup-my.cnf
ibdata1
xtra_test/db.opt
xtra_test/M.MYI
xtra_test/M.frm
xtra_test/I.frm
xtra_test/M.MYD
./xtrabackup_binlog_info
xtrabackup_logfile
xtrabackup_checkpoints
./xtrabackup_info
還原方法和上面一樣忘古。?
更多信息見(jiàn):
http://mysql.taobao.org/monthly/2016/03/07/
http://op.baidu.com/2014/07/xtrabackup%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E6%96%BD/
http://www.drupal001.com/2014/02/percona-xtrabackup-mysql/
http://www.xuchanggang.cn/archives/700.html
http://blog.csdn.net/mr_mablevi/article/details/5860571
http://blog.csdn.net/yongsheng0550/article/details/6682162
~~~~~~~~~~~~~~~ 萬(wàn)物之中,希望至美 ~~~~~~~~~~~~~~~