日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Xtrabackup 安装使用

發布時間:2024/6/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Xtrabackup 安装使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一 簡介:
? ? ? Xtrabackup是一個對InnoDB做數據備份的工具,支持在線熱備份(備份時不影響數據讀寫),是商業備份工具InnoDB Hotbackup的一個很好的替代品。它能對InnoDB和XtraDB存儲引擎的數據庫非阻塞地備份(對于MyISAM的備份同樣需要加表鎖)。XtraBackup支持所有的Percona Server、MySQL、MariaDB和Drizzle。幾年前使用過,但現在忘記的差不多了,所以就重新拾起看看。

xtrabackup有兩個主要的工具:xtrabackup、innobackupex

(1).xtrabackup只能備份InnoDB和XtraDB 兩種數據表(2).innobackupex則封裝了xtrabackup,同時可以備份MyISAM數據表 Innobackupex完整備份后生成了幾個重要的文件: xtrabackup_binlog_info:記錄當前最新的LOG Positionxtrabackup_binlog_pos_innodb:innodb log postionxtrabackup_checkpoints: 存放備份的起始位置beginlsn和結束位置endlsn,增量備份需要這個lsn[增量備份可以在這里面看from和to兩個值的變化] Xtrabackup特點: (1)備份過程快速、可靠(2)備份過程不會打斷正在執行的事務(3)能夠基于壓縮等功能節約磁盤空間和流量(4)自動實現備份檢驗(5)還原速度快

?二 安裝: ?http://www.percona.com/downloads/

下載安裝:

1)直接下載二進制包,不編譯,編譯需要和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 1tar zxvf percona-xtrabackup-2.1.9-744-Linux-x86_64.tar.gz2ls -lh drwxr-xr-x 2 root root 4.0K 5月 2 2014 bin drwxr-xr-x 4 root root 4.0K 5月 2 2014 share3ls -lh bin/ -rwxr-xr-x 1 root root 165K 5月 2 2014 innobackupex lrwxrwxrwx 1 root root 12 5月 2 2014 innobackupex-1.5.1 -> innobackupex -rwxr-xr-x 1 root root 2.2M 5月 2 2014 xbcrypt -rwxr-xr-x 1 root root 2.2M 5月 2 2014 xbstream -rwxr-xr-x 1 root root 13M 5月 2 2014 xtrabackup -rwxr-xr-x 1 root root 16M 5月 2 2014 xtrabackup_55 -rwxr-xr-x 1 root root 79M 5月 2 2014 xtrabackup_564cp innobackupex-1.5.1 /usr/bin/innobackupex cp xtrabackup_55 /usr/bin/xtrabackup #cp xtrabackup /usr/bin

2)Ubuntu 下的安裝:http://www.percona.com/doc/percona-xtrabackup/2.1/installation/apt_repo.html

1:執行 apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A2:在 vi /etc/apt/sources.list 添加 deb http://repo.percona.com/apt VERSION main deb-src http://repo.percona.com/apt VERSION main3:安裝 $ apt-get update $ apt-get install percona-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 按照自己的系統把上面的代入。
deb http://repo.percona.com/apt precise main deb-src http://repo.percona.com/apt precise main

# precise是Ubuntu-12.04的版本代號,如果是其它系統版本,需要更換。

三 使用說明:

安裝完之后會生成幾個工具:http://www.percona.com/doc/percona-xtrabackup/2.1/manual.html

innobackupex:這個是其實是下面三個工具的一個perl腳本封裝,可以備份MyISAM, InnoDB, XtraDB表。但在處理Myisam時需要加一個讀鎖。xtrabackup:一個由C編譯而來的二進制文件,只能備份InnoDB和XtraDB數據。xbcrypt:用來加密或解密備份的數據。xbstream:用來解壓或壓縮xbstream格式的壓縮文件。

innobackupex :

xtrabackup命令只備份數據文件,并不備份數據表結構(.frm),所以使用xtrabackup恢復的時候必須有對應表結構文件(.frm)。用innobackupex命令,此命令相當于冷備份,復制數據目錄的索引,數據,結構文件,但會有短暫的鎖表(時間依賴于MyISAM大小)。

參數:

--defaults-file:指定my.cnf參數文件的位置[此配置文件里必須指定datadir] --apply-log:同xtrabackup的--prepare參數,一般情況下,在備份完成后,數據尚且不能用于恢復操作,因為備份的數據中可能會包含尚未提交的事務或已經提交但尚未同步至數據文件中的事務。因此,此時數據 文件仍處理不一致狀態。--apply-log的作用是通過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處于一致性狀態。 --copy-back:做數據恢復時將備份數據文件拷貝到MySQL服務器的datadir --remote-host=HOSTNAME: 通過ssh將備份數據存儲到進程服務器上 --stream=[tar]:備份文件輸出格式, 該文件可在XtarBackup binary文件中獲得. 在使用參數stream=tar備份的時候,你的xtrabackup_logfile可能會臨時放在/tmp目錄下,如果你備份的時候并發寫入較大的話,xtrabackup_logfile可能會很大(5G+),很可能會撐滿你的/tmp目錄,可以通過參數--tmpdir指定目錄來解決這個問題. --tmpdir=DIRECTORY:當有指定--remote-host or --stream時, 事務日志臨時存儲的目錄, 默認采用MySQL配置文件中所指定的臨時目錄tmpdir --redo-only --apply-log:強制備份日志時只redo,跳過rollback,這在做增量備份時非常必要 --use-memory=*:該參數在prepare的時候使用,控制prepare時innodb實例使用的內存 --databases=LIST:列出需要備份的databases,如果沒有指定該參數,所有包含MyISAM和InnoDB表的database都會被備份 --slave-info:備份從庫, 加上--slave-info備份目錄下會多生成一個xtrabackup_slave_info 文件, 這里會保存主日志文件以及偏移, 文件內容類似于:CHANGE MASTER TO MASTER_LOG_FILE='', MASTER_LOG_POS=0 --socket=SOCKET:指定mysql.sock所在位置,以便備份進程登錄mysql.

更多參數見:http://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/innobackupex_option_reference.html

四 測試:

?1)全量備份&還原

? ?#初始化

mysql> create database xtra_test default charset utf8; Query OK, 1 row affected (0.00 sec)mysql> use xtra_test Database changed mysql> create table M(id int,name varchar(10))engine=myisam; Query OK, 0 rows affected (0.00 sec)mysql> create table I(id int,name varchar(10))engine=innodb; Query OK, 0 rows affected (0.00 sec)mysql> insert into M values(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0mysql> insert into I values(11,'A'),(22,'B'),(33,'C'),(44,'D'),(55,'E'); Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0mysql> select * from M; +------+------+ | id | name | +------+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | +------+------+ 5 rows in set (0.00 sec)mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec)

? ? ?#備份

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 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/p141111 11:56:58 innobackupex: Connecting to MySQL server with DSN '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). 141111 11:56:58 innobackupex: Connected to MySQL server 141111 11:56:58 innobackupex: Executing a version check against the server... 141111 11:56:58 innobackupex: Done. 141111 11:56:58 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.38-0ubuntu0.12.04.1-loginnobackupex: Created backup directory /home/zhoujy/xtrabackup/2014-11-11_11-56-58141111 11:56:58 innobackupex: Starting ibbackup with command: 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: Waiting for ibbackup (pid=10539) to suspend innobackupex: Suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2'xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /var/lib/mysql xtrabackup: open files limit requested 0, set to 1024 xtrabackup: 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 = 2 xtrabackup: innodb_log_file_size = 5242880 >> log scanned up to (7363097930) xtrabackup: Generating a list of tablespaces [01] Copying ./ibdata1 to /home/zhoujy/xtrabackup/2014-11-11_11-56-58/ibdata1 >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) >> log scanned up to (7363097930) [01] ...done >> log scanned up to (7363097930) xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_suspended_2' with pid '10540'141111 11:57:11 innobackupex: Continuing after ibbackup has suspended 141111 11:57:11 innobackupex: Executing FLUSH TABLES WITH READ LOCK... 141111 11:57:11 innobackupex: All tables locked and flushed to disk141111 11:57:11 innobackupex: Starting to backup non-InnoDB tables and files innobackupex: in subdirectories of '/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' >> log scanned up to (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' 141111 11:57:11 innobackupex: Finished backing up non-InnoDB tables and files141111 11:57:11 innobackupex: Executing FLUSH ENGINE LOGS... 141111 11:57:11 innobackupex: Waiting for log copying to finishxtrabackup: The latest check point (for incremental): '7363097930' xtrabackup: Stopping log copying thread. .>> log scanned up to (7363097930)xtrabackup: Creating suspend file '/home/zhoujy/xtrabackup/2014-11-11_11-56-58/xtrabackup_log_copied' with pid '10540' xtrabackup: Transaction log of lsn (7363097930) to (7363097930) was copied. 141111 11:57:12 innobackupex: All tables unlockedinnobackupex: Backup created in directory '/home/zhoujy/xtrabackup/2014-11-11_11-56-58' innobackupex: MySQL binlog position: filename 'mysql-bin51.000001', position 967 141111 11:57:12 innobackupex: Connection to database server closed 141111 11:57:12 innobackupex: completed OK!

查看備份出來的文件:

ls -lh 2014-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_logfile drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test

/home/zhoujy/xtrabackup/ 備份存放的位置,備份會在該目錄下生成一個按照時間命名的文件夾。用--no-timestamp參數可以指定到自己想要的備份文件夾,不受時間命名的文件夾限制。

?

利用?--apply-log的作用是通過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處于一致性狀態。

root@ha1:/home/zhoujy/xtrabackup# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_11-56-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.Get the latest version of Percona XtraBackup, documentation, and help resources: http://www.percona.com/xb/p141111 12:02:42 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!".141111 12:02:42 innobackupex: 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.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58 xtrabackup: 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 = 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:10M: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.4 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 7363094028 and 7363094028 in ibdata files do not match the log sequence number 7363097930 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... InnoDB: Last MySQL binlog file position 0 967, file name /var/log/mysql/mysql-bin51.000001 InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.21 started; log sequence number 7363097930[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 file position 0 967, file name /var/log/mysql/mysql-bin51.000001xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 7363100603141111 12:02:44 innobackupex: 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-58 for creating ib_logfile*xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) xtrabackup: cd to /home/zhoujy/xtrabackup/2014-11-11_11-56-58 xtrabackup: 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 = 2 xtrabackup: innodb_log_file_size = 5242880 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 = 2 xtrabackup: innodb_log_file_size = 5242880 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.4 InnoDB: Using CPU crc32 instructions InnoDB: Initializing buffer pool, size = 100.0M InnoDB: Completed initialization of buffer pool InnoDB: Setting log file ./ib_logfile101 size to 5 MB InnoDB: Setting log file ./ib_logfile1 size to 5 MB InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 InnoDB: New log files created, LSN=7363100603 InnoDB: Highest supported file format is Barracuda. InnoDB: 128 rollback segment(s) are active. InnoDB: Waiting for purge to start InnoDB: 5.6.21 started; log sequence number 7363100684[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 file position 0 967, file name /var/log/mysql/mysql-bin51.000001xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 7363103417 141111 12:02:45 innobackupex: completed OK!

應用完之后再查看備份文件看是否有變化:

root@ha1:/home/zhoujy/xtrabackup# ls -lh 2014-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 12:02 ibdata1 -rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile0 -rw-r--r-- 1 root root 5.0M 11月 11 12:02 ib_logfile1 -rw-r--r-- 1 root root 25 11月 11 11:57 xtrabackup_binlog_info -rw-r--r-- 1 root root 38 11月 11 12:02 xtrabackup_binlog_pos_innodb -rw-r----- 1 root root 95 11月 11 12:02 xtrabackup_checkpoints -rw-r--r-- 1 root root 666 11月 11 11:57 xtrabackup_info -rw-r----- 1 root root 2.0M 11月 11 12:02 xtrabackup_logfile drwxr-xr-x 2 root root 4.0K 11月 11 11:57 xtra_test

看到redo log已經出現。xtrabackup_開頭的幾個文件記錄了一些日志偏移量的信息和日志名和時間等信息。在xtrabackup_checkpoints中記錄了備份的模式:

backup_type = full-backuped。

?

xtrabackup_binlog_info:記錄當前最新的LOG Position xtrabackup_binlog_pos_innodb:innodb log postion xtrabackup_checkpoints: 存放備份的起始位置beginlsn和結束位置endlsn,增量備份需要這個lsn[增量備份可以在這里面看from和to兩個值的變化 xtrabackup_info:備份的一些具體信息日志

? ? ?#還原

mysql> drop database xtra_test; Query OK, 2 rows affected (0.00 sec)

1) 刪除原始目錄里的文件:

root@ha1:/var/lib/mysql# rm ib_logfile0 ib_logfile1 ibdata1 關閉數據庫: 1) root@ha1:/var/lib/mysql# /etc/init.d/mysql stop開始恢復 2) 1:innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /home/zhoujy/xtrabackup/2014-11-11_11-56-58/執行上面的命令需要保證數據庫目錄是空的,否則會失敗,因為目錄里有binlog日志,要么移走,要么就用手動復制需要的文件。所以說日志不要和數據放在一起!2cp ib_logfile0 ib_logfile1 ibdata1 /var/lib/mysql/cp -R xtra_test /var/lib/mysql/ 修改權限和開啟數據庫 3) root@ha1:/var/lib/mysql# chown -R mysql.mysql ib*root@ha1:/var/lib/mysql# chown -R mysql.mysql xtra_test/ 開啟數據庫root@ha1:/var/lib/mysql# /etc/init.d/mysql start...mysql start/running, process 15937

數據已恢復:

mysql> use xtra_test Database changed mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec)

2)增量備份&還原

?#備份

在做增量備份的時候需要做一次全量備份,和上面一樣,只是針對所有庫的備份:

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', position 107 141111 13:57:24 innobackupex: Connection to database server closed 141111 13:57:24 innobackupex: completed OK!

增量備份這里開始

1.數據庫操作:

mysql> select * from I; +------+------+ | id | name | +------+------+ | 11 | A | | 22 | B | | 33 | C | | 44 | D | | 55 | E | +------+------+ 5 rows in set (0.00 sec)mysql> insert into I values(111,'A'),(222,'B'),(333,'C'),(444,'D'),(555,'E'); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from M; +------+------+ | id | name | +------+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | +------+------+ 5 rows in set (0.00 sec) mysql> update M set name = upper(name); Query OK, 5 rows affected (0.00 sec) Rows matched: 5 Changed: 5 Warnings: 0mysql> create table X(name varchar(20))default charset utf8; Query OK, 0 rows affected (0.01 sec)mysql> insert into X values('zjy'); Query OK, 1 row affected (0.00 sec)

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. 141111 14:15:06 innobackupex: All tables unlockedinnobackupex: Backup created in directory '/home/zhoujy/increment_data/2014-11-11_14-14-56' innobackupex: MySQL binlog position: filename 'mysql-bin51.000002', position 981 141111 14:15:06 innobackupex: Connection to database server closed 141111 14:15:06 innobackupex: completed OK!

? ? ? 其中,--incremental指明是增量備份,--incremental-basedir指定上次完整備份或者增量備份文件的位置。這里的增量備份其實只針對的是InnoDB,對于MyISAM來說,還是完整備份。

增量備份的文件:

root@ha1:/home/zhoujy/increment_data# ls -lh 2014-11-11_14-14-56/ total 440K -rw-r--r-- 1 root root 188 11月 11 14:14 backup-my.cnf -rw-r----- 1 root root 400K 11月 11 14:15 ibdata1.delta -rw-r----- 1 root root 44 11月 11 14:14 ibdata1.meta drwxr-xr-x 2 root root 4.0K 11月 11 14:15 mha_test drwxr-xr-x 2 root root 4.0K 11月 11 14:15 mysql drwxr-xr-x 2 root root 4.0K 11月 11 14:15 performance_schema -rw-r--r-- 1 root root 25 11月 11 14:15 xtrabackup_binlog_info -rw-r----- 1 root root 102 11月 11 14:15 xtrabackup_checkpoints -rw-r--r-- 1 root root 738 11月 11 14:15 xtrabackup_info -rw-r----- 1 root root 2.5K 11月 11 14:15 xtrabackup_logfile drwxr-xr-x 2 root root 4.0K 11月 11 14:15 xtra_testroot@ha1:/home/zhoujy/increment_data# cat 2014-11-11_14-14-56/xtrabackup_checkpoints backup_type = incremental from_lsn = 7363103427 對應增量中的to_lsn to_lsn = 7363106612 last_lsn = 7363106612 compact = 0

3:繼續增量備份

數據庫操作

mysql> insert into X values('dxy'); Query OK, 1 row affected (0.01 sec)mysql> insert into X values('浙江'); Query OK, 1 row affected (0.01 sec)

增量備份(基于增量的增量備份)

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/141111 14:25:46 innobackupex: Connection to database server closed 141111 14:25:46 innobackupex: completed OK!

增量備份的文件

root@ha1:/home/zhoujy/increment_data# cat 2014-11-11_14-25-36/xtrabackup_checkpoints backup_type = incremental from_lsn = 7363106612 對應上一個備份的to_lsn to_lsn = 7363107216 last_lsn = 7363107216 compact = 0

要是在第2次做增量備份的時候?--incremental-basedir 指向全量備份,則第一次增量備份中的數據會被第2次包含,只需要還原一次就可以恢復,現在則需要還原2次增量備份。

#還原

#還原全量備份: root@ha1:/home/zhoujy# innobackupex --apply-log /home/zhoujy/xtrabackup/2014-11-11_13-57-11/#第一次的增量備份應用日志,應用完日志后,將合并到全備上,恢復使用全備恢復 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/#第二次的增量備份應用日志,應用完日志后,將合并到全備上,恢復使用全備恢復 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/此時兩次增量備份其實都合并到全備上了,恢復是只需要使用全備進行恢復就可以了

還原操作執行完之后,查看其lsn的信息:

root@ha1:/home/zhoujy/xtrabackup/2014-11-11_13-57-11# cat xtrabackup_checkpoints backup_type = full-prepared from_lsn = 0 to_lsn = 7363107216 last_lsn = 7363107216 compact = 0 說明了,還原操作執行沒問題。按照之前還原操作的步驟來恢復數據:先刪除數據,再把文件復制過去,看看是否成功。測試下來,還原成功。

3)壓縮備份:必須使用-i參數拆包解壓

打包(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# tar ixvf xtra_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

還原方法和上面一樣,就不多做說明了。

打包壓縮:

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# tar izxvf xtra_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

還原方法和上面一樣。?

?

更多信息見:

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

轉載于:https://www.cnblogs.com/zhoujinyi/p/4088866.html

總結

以上是生活随笔為你收集整理的Xtrabackup 安装使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。