XtraBackup全备与增量备份
一、XtraBackup安裝
下載地址:http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.8/source/
安裝步驟:
================================ How to build XtraBackup on Linux ================================Prerequisites -------------$ yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses5-develCompiling with CMake -------------------- $ cmake -DBUILD_CONFIG=xtrabackup_release && make -j4
Installation ------------ $ make install
? will install all XtraBackup binaries, the innobackupex script and tests?to /usr/local/xtrabackup. You can override this either with “make?DESTDIR=... install” or by changing the installation layout with “cmake?-DINSTALL_LAYOUT=...”.
?
如果出現(xiàn)下面報(bào)錯(cuò),需要?yum install crypt* ;?yum install libgcrypt*
CMake Error at cmake/gcrypt.cmake:25 (MESSAGE):Cannot find gcrypt.h in /usr/include;/usr/local/include;/opt/local/include.You can use libgcrypt-config --cflags to get the necessary path and pass itto CMake with -DGCRYPT_INCLUDE_PATH=<path> Call Stack (most recent call first):storage/innobase/xtrabackup/src/CMakeLists.txt:20 (FIND_GCRYPT)CMake Error at cmake/gcrypt.cmake:36 (MESSAGE):Cannot find libgcrypt shared libraries in/usr/lib;/usr/local/lib;/opt/local/lib. You can use libgcrypt-config--libs to get the necessary path and pass it to CMake with-DGCRYPT_LIB_PATH=<path> Call Stack (most recent call first):storage/innobase/xtrabackup/src/CMakeLists.txt:20 (FIND_GCRYPT)?
二、xtrabackup全備測(cè)試
普通備份(全量備份)
[root@server mysql]# mkdir /data/mysql/databak
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --target-dir=/data/mysql/databak/
[root@server mysql]# cp -rp data/test/students.frm databak/test/
注意:xtrabackup只備份數(shù)據(jù)文件,并不備份數(shù)據(jù)表結(jié)構(gòu)(.frm),所以這里要手動(dòng)備份一下,以便xtrabackup恢復(fù)的時(shí)候使用,如果是做了分表分文件保存的話,就需要挑出.frm表結(jié)構(gòu),表數(shù)據(jù)文件.ibd不需要復(fù)制
全量備份恢復(fù)
實(shí)施對(duì)備份文件進(jìn)行恢復(fù)前的準(zhǔn)備
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/
從備份目錄復(fù)制對(duì)應(yīng)數(shù)據(jù)庫表結(jié)構(gòu)到默認(rèn)的數(shù)據(jù)目錄[如果是做了分表分文件,則需要全部復(fù)制到默認(rèn)的數(shù)據(jù)目錄]
cp -r /data/mysql/databak/test /data/mysql/data/
刪除默認(rèn)數(shù)據(jù)目錄中對(duì)應(yīng)的數(shù)據(jù)文件并復(fù)制備份的數(shù)據(jù)文件到默認(rèn)數(shù)據(jù)目錄
rm /data/mysql/data/ib*
cp /data/mysql/databak/ib* /data/mysql/data/
修改數(shù)據(jù)目錄權(quán)限[注意細(xì)節(jié):mysql庫的所有者是mysql,組是root]
chown -R mysql:mysql /data/mysql/data
重啟MySQL
[root@server data]# /etc/init.d/mysqld restart
三、xtrabackup增量備份測(cè)試
1. 全備
[root@server databak]# mkdir /data/mysql/databak/
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --target-dir=/data/mysql/databak/
[root@server mysql]# cp -rp data/test/students.frm databak/test/
2. 增量備份
[root@server databak]# mkdir /data/mysql/databak/delta/
[root@server databak]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --incremental-basedir=/data/mysql/databak/ --target-dir=/data/mysql/databak/delta/
3. 備份恢復(fù)準(zhǔn)備[這里沒有完成增量備份,增量備份數(shù)據(jù)恢復(fù)失敗]
[root@server data]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/
[root@server data]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/ --incremental-dir/=/data/mysql/databak/delta/
4. 恢復(fù)數(shù)據(jù)庫
從備份目錄復(fù)制對(duì)應(yīng)數(shù)據(jù)庫表結(jié)構(gòu)到默認(rèn)的數(shù)據(jù)目錄[如果是做了分表分文件,則需要全部復(fù)制到默認(rèn)的數(shù)據(jù)目錄]
cp -r /data/mysql/databak/test /data/mysql/data/
刪除默認(rèn)數(shù)據(jù)目錄中對(duì)應(yīng)的數(shù)據(jù)文件并復(fù)制備份的數(shù)據(jù)文件到默認(rèn)數(shù)據(jù)目錄
rm /data/mysql/data/ib*
cp /data/mysql/databak/ib* /data/mysql/data/
修改數(shù)據(jù)目錄權(quán)限[注意細(xì)節(jié):mysql庫的所有者是mysql,組是root]
chown -R mysql:mysql /data/mysql/data
四、用innobackupex 執(zhí)行全備與增量備份
innobackupex全備和增量備份
1. 全備
?
執(zhí)行失敗,需要指定配置文件路徑:
[root@server xtrabackup]# innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=redhat /data/mysql/databak/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.150204 19:00:03 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/usr/local/mysql/my.cnf;mysql_read_default_group=xtrabackup' as 'root' (using password: YES). 150204 19:00:03 innobackupex: Connected to MySQL server 150204 19:00:03 innobackupex: Executing a version check against the server... 150204 19:00:03 innobackupex: Done. 150204 19:00:03 innobackupex: Starting the backup operationIMPORTANT: Please check that the backup run completes successfully.At the end of a successful backup run innobackupexprints "completed OK!".innobackupex: Using server version 5.5.30innobackupex: Created backup directory /data/mysql/databak/2015-02-04_19-00-03150204 19:00:03 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/usr/local/mysql/my.cnf" --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/data/mysql/databak/2015-02-04_19-00-03 --tmpdir=/tmp --extra-lsndir='/tmp' innobackupex: Waiting for ibbackup (pid=19209) to suspend innobackupex: Suspend file '/data/mysql/databak/2015-02-04_19-00-03/xtrabackup_suspended_2'xtrabackup version 2.2.8 based on MySQL server 5.6.22 Linux (x86_64) (revision id: ) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /data/mysql/data/ xtrabackup: open files limit requested 0, set to 1024 xtrabackup: using the following InnoDB configuration: xtrabackup: innodb_data_home_dir = /data/mysql/data xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 xtrabackup: using O_DIRECT >> log scanned up to (1602080) xtrabackup: Generating a list of tablespaces [01] Copying /data/mysql/data/ibdata1 to /data/mysql/databak/2015-02-04_19-00-03/ibdata1 >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) >> log scanned up to (1602080) [01] ...done >> log scanned up to (1602080) [01] Copying ./test/students.ibd to /data/mysql/databak/2015-02-04_19-00-03/test/students.ibd [01] ...done >> log scanned up to (1602080) xtrabackup: Creating suspend file '/data/mysql/databak/2015-02-04_19-00-03/xtrabackup_suspended_2' with pid '19209'150204 19:00:17 innobackupex: Continuing after ibbackup has suspended 150204 19:00:17 innobackupex: Executing FLUSH TABLES WITH READ LOCK... 150204 19:00:17 innobackupex: All tables locked and flushed to disk150204 19:00:17 innobackupex: Starting to backup non-InnoDB tables and files innobackupex: in subdirectories of '/data/mysql/data/' innobackupex: Backing up files '/data/mysql/data//performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files) >> log scanned up to (1602080) innobackupex: Backing up file '/data/mysql/data//test/students.frm' innobackupex: Backing up files '/data/mysql/data//mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (72 files) 150204 19:00:17 innobackupex: Finished backing up non-InnoDB tables and files150204 19:00:17 innobackupex: Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS... 150204 19:00:17 innobackupex: Waiting for log copying to finishxtrabackup: The latest check point (for incremental): '1602080' xtrabackup: Stopping log copying thread. .>> log scanned up to (1602080)xtrabackup: Creating suspend file '/data/mysql/databak/2015-02-04_19-00-03/xtrabackup_log_copied' with pid '19209' xtrabackup: Transaction log of lsn (1602080) to (1602080) was copied. 150204 19:00:18 innobackupex: All tables unlockedinnobackupex: Backup created in directory '/data/mysql/databak/2015-02-04_19-00-03' 150204 19:00:18 innobackupex: Connection to database server closed 150204 19:00:18 innobackupex: completed OK!?
2. 第一次增量備份
#--incremental:增量備份的文件夾
#--incremental-dir:針對(duì)哪個(gè)做增量備份
?
3. 模擬數(shù)據(jù)丟失
mysql> drop database test; Query OK, 1 row affected (0.14 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)?
4. 將全備數(shù)據(jù)執(zhí)行prepare
[root@server 2015-02-04_19-03-58]# innobackupex --apply-log --redo-only /data/mysql/databak/2015-02-04_19-00-03/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.150204 19:10:15 innobackupex: Starting the apply-log operationIMPORTANT: Please check that the apply-log run completes successfully.At the end of a successful apply-log run innobackupexprints "completed OK!".150204 19:10:15 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/data/mysql/databak/2015-02-04_19-00-03/backup-my.cnf" --defaults-group="mysqld" --prepare --target-dir=/data/mysql/databak/2015-02-04_19-00-03 --apply-log-onlyxtrabackup version 2.2.8 based on MySQL server 5.6.22 Linux (x86_64) (revision id: ) xtrabackup: cd to /data/mysql/databak/2015-02-04_19-00-03 xtrabackup: This target seems to be not prepared yet. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1602080) xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer 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 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 100.0M InnoDB: Completed initialization of buffer pool InnoDB: Highest supported file format is Barracuda. InnoDB: The log sequence numbers 1602070 and 1602070 in ibdata files do not match the log sequence number 1602080 in the 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...[notice (again)]If you use binary log and don't use any hack of group commit, the binary log position seems to be:xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1602080 150204 19:10:16 innobackupex: completed OK!?
5. 將增量備份數(shù)據(jù)合并到全備中
[root@server 2015-02-04_19-03-58]# innobackupex --apply-log --redo-only --incremental /data/mysql/databak/2015-02-04_19-00-03/ --incremental-dir=/data/mysql/databak/2015-02-04_19-03-58/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.150204 19:10:19 innobackupex: Starting the apply-log operationIMPORTANT: Please check that the apply-log run completes successfully.At the end of a successful apply-log run innobackupexprints "completed OK!".150204 19:10:19 innobackupex: Starting ibbackup with command: xtrabackup --defaults-file="/data/mysql/databak/2015-02-04_19-00-03/backup-my.cnf" --defaults-group="mysqld" --prepare --target-dir=/data/mysql/databak/2015-02-04_19-00-03 --apply-log-only --incremental-dir=/data/mysql/databak/2015-02-04_19-03-58/xtrabackup version 2.2.8 based on MySQL server 5.6.22 Linux (x86_64) (revision id: ) incremental backup from 1602080 is enabled. xtrabackup: cd to /data/mysql/databak/2015-02-04_19-00-03 xtrabackup: This target seems to be already prepared. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1602919) xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend xtrabackup: innodb_log_group_home_dir = /data/mysql/databak/2015-02-04_19-03-58/ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: Generating a list of tablespaces xtrabackup: page size for /data/mysql/databak/2015-02-04_19-03-58//ibdata1.delta is 16384 bytes Applying /data/mysql/databak/2015-02-04_19-03-58//ibdata1.delta to ./ibdata1... xtrabackup: page size for /data/mysql/databak/2015-02-04_19-03-58//test/students.ibd.delta is 16384 bytes Applying /data/mysql/databak/2015-02-04_19-03-58//test/students.ibd.delta to ./test/students.ibd... xtrabackup: using the following InnoDB configuration for recovery: xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend xtrabackup: innodb_log_group_home_dir = /data/mysql/databak/2015-02-04_19-03-58/ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer 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 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 100.0M InnoDB: Completed initialization of buffer pool InnoDB: Highest supported file format is Barracuda. InnoDB: The log sequence numbers 1602070 and 1602070 in ibdata files do not match the log sequence number 1602919 in the 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...[notice (again)]If you use binary log and don't use any hack of group commit, the binary log position seems to be:xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1602919 innobackupex: Starting to copy non-InnoDB files in '/data/mysql/databak/2015-02-04_19-03-58/' innobackupex: to the full backup directory '/data/mysql/databak/2015-02-04_19-00-03' innobackupex: Copying '/data/mysql/databak/2015-02-04_19-03-58/xtrabackup_info' to '/data/mysql/databak/2015-02-04_19-00-03/xtrabackup_info' innobackupex: Copying '/data/mysql/databak/2015-02-04_19-03-58/performance_schema/db.opt' to '/data/mysql/databak/2015-02-04_19-00-03/performance_schema/db.opt' innobackupex: Copying '/data/mysql/databak/2015-02-04_19-03-58/performance_schema/events_waits_summary_by_thread_by_event_name.frm' to '/data/mysql/databak/2015-02-04_19-00-03/performance_schema/events_waits_summary_by_thread_by_event_name.frm' innobackupex: Copying '/data/mysql/databak/2015-02-04_19-03-58/performance_schema/file_summary_by_instance.frm' to '/data/mysql/databak/2015-02-04_19-00-03/performance_schema/file_summary_by_instance.frm'..........
150204 19:10:20 innobackupex: completed OK!
6. 停止mysqld服務(wù),恢復(fù)數(shù)據(jù)
[root@server 2015-02-04_19-03-58]# /etc/init.d/mysqld stop Shutting down MySQL... [ OK ] [root@server data]# innobackupex --defaults-file=/usr/local/mysql/my.cnf --copy-back /data/mysql/databak/2015-02-04_19-00-03/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.150204 19:13:47 innobackupex: Starting the copy-back operationIMPORTANT: Please check that the copy-back run completes successfully.At the end of a successful copy-back run innobackupexprints "completed OK!".innobackupex: Starting to copy files in '/data/mysql/databak/2015-02-04_19-00-03'.........innobackupex: Starting to copy InnoDB system tablespace innobackupex: in '/data/mysql/databak/2015-02-04_19-00-03' innobackupex: back to original InnoDB data directory '/data/mysql/data' innobackupex: Copying '/data/mysql/databak/2015-02-04_19-00-03/ibdata1' to '/data/mysql/data/ibdata1'innobackupex: Starting to copy InnoDB undo tablespaces innobackupex: in '/data/mysql/databak/2015-02-04_19-00-03' innobackupex: back to '/data/mysql/data/'innobackupex: Starting to copy InnoDB log files innobackupex: in '/data/mysql/databak/2015-02-04_19-00-03' innobackupex: back to original InnoDB log directory '/data/mysql/data/' innobackupex: Finished copying back files.150204 19:13:53 innobackupex: completed OK!
?
7. 對(duì)恢復(fù)的文件修改權(quán)限
[root@server data]# chown -R mysql:mysql * [root@server data]# chown -R mysql:root mysql/?
8. 啟動(dòng)mysql服務(wù)
[root@server data]# /etc/init.d/mysqld start Starting MySQL... [ OK ]?
done ^_^
轉(zhuǎn)載于:https://www.cnblogs.com/forilen/p/4273133.html
總結(jié)
以上是生活随笔為你收集整理的XtraBackup全备与增量备份的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拉钩网的起薪
- 下一篇: 博客园客户端UAP开发随笔 -- App