您好,登錄后才能下訂單哦!
一、簡(jiǎn)介
Xtrabackup是由percona提供的mysql數(shù)據(jù)庫備份工具,據(jù)官方介紹,這也是世界上惟一一款開源的能夠?qū)nnodb和xtradb數(shù)據(jù)庫進(jìn)行熱備的工具。其主要特點(diǎn)是備份過程快速、可靠,不會(huì)打斷正在執(zhí)行的事務(wù),能夠基于壓縮等功能節(jié)約磁盤空間和流量,自動(dòng)實(shí)現(xiàn)備份檢測(cè)及其還原速度快。
二、安裝及備份、恢復(fù)實(shí)現(xiàn)
安裝:其最新版的軟件可從 http://www.percona.com/software/percona-xtrabackup/ 獲得。本文基于centos6.x的系統(tǒng),因此,直接下載相應(yīng)版本的rpm包安裝即可,這里不再演示其過程。
yum -y install percona-toolkit-2.2.4-1.noarch.rpm percona-xtrabackup-2.1.8-733.rhel6.x86_64.rpm
完全備份及刪除數(shù)據(jù)目錄實(shí)現(xiàn)恢復(fù):
mysql>set session_sql_log_bin=0; #導(dǎo)入數(shù)據(jù)時(shí)讓其不記錄二進(jìn)制日志 mysql>source /root/hellodb.sql #導(dǎo)入數(shù)據(jù) mysql>set session_sql_log_bin=1; #開啟二進(jìn)制日志 [root@centos6]#innobackupex --user=root /mybackups/ #全量備份 [root@centos6]#service mysqld stop #停止數(shù)據(jù)庫,刪除數(shù)據(jù)目錄 [root@centos6]#rm -rf /mydata/data/* [root@centos6]#innobackupex --apply-log /mybackups/2016-11-22_15-39-09/ #準(zhǔn)備完全備份 [root@centos6]#innobackupex --copy-back /mybackups/2016-11-22_15-39-09/ #從完全備份中恢復(fù)數(shù)據(jù) [root@centos6 data]# ll #數(shù)據(jù)已然恢復(fù)成功 總用量 28688 drwxr-xr-x. 2 root root 4096 11月 22 15:43 hellodb -rw-r--r--. 1 root root 18874368 11月 22 15:43 ibdata1 -rw-r--r--. 1 root root 5242880 11月 22 15:43 ib_logfile0 -rw-r--r--. 1 root root 5242880 11月 22 15:43 ib_logfile1 drwxr-xr-x. 2 root root 4096 11月 22 15:43 mysql drwxr-xr-x. 2 root root 4096 11月 22 15:43 performance_schema drwxr-xr-x. 2 root root 4096 11月 22 15:43 test [root@centos6 data]# chown -R mysql.mysql ./* #改變屬組和屬組 [root@centos6 data]# ll 總用量 28688 drwxr-xr-x. 2 mysql mysql 4096 11月 22 15:43 hellodb -rw-r--r--. 1 mysql mysql 18874368 11月 22 15:43 ibdata1 -rw-r--r--. 1 mysql mysql 5242880 11月 22 15:43 ib_logfile0 -rw-r--r--. 1 mysql mysql 5242880 11月 22 15:43 ib_logfile1 drwxr-xr-x. 2 mysql mysql 4096 11月 22 15:43 mysql drwxr-xr-x. 2 mysql mysql 4096 11月 22 15:43 performance_schema drwxr-xr-x. 2 mysql mysql 4096 11月 22 15:43 test [root@centos6 data]#service mysqld start #啟動(dòng)數(shù)據(jù)庫,在恢復(fù)數(shù)據(jù)庫時(shí)數(shù)據(jù)庫無需啟動(dòng)
從安全角度考慮,如果要使用一個(gè)最小權(quán)限的用戶進(jìn)行備份,可創(chuàng)建此用戶進(jìn)行完全備份
mysql>create user 'bkuser'@'localhost' identified by 'passw ord' mysql>revoke all privileges,grant option from 'bkuser'; mysql>grant reload,lock tables,replication clinet on *.* to 'bkuser'@'localhost' mysql>flush privileges;
xtrabackup備份文件說明:
使用innobakupex備份時(shí),其會(huì)調(diào)用xtrabackup備份所有的InnoDB表,復(fù)制所有關(guān)于表結(jié)構(gòu)定義的相關(guān)文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關(guān)文件,同時(shí)還會(huì)備份觸發(fā)器和數(shù)據(jù)庫配置信息相關(guān)的文件。這些文件會(huì)被保存至一個(gè)以時(shí)間命令的目錄中。
[root@centos6 2016-11-22_19-06-45]# ll 總用量 18472 -rw-r--r--. 1 root root 260 11月 22 19:06 backup-my.cnf drwx------. 2 root root 4096 11月 22 19:06 hellodb -rw-r-----. 1 root root 18874368 11月 22 19:06 ibdata1 drwx------. 2 root root 4096 11月 22 19:06 mydb drwxr-xr-x. 2 root root 4096 11月 22 19:06 mysql drwxr-xr-x. 2 root root 4096 11月 22 19:06 performance_schema drwxr-xr-x. 2 root root 4096 11月 22 19:06 test -rw-r--r--. 1 root root 13 11月 22 19:06 xtrabackup_binary -rw-r--r--. 1 root root 24 11月 22 19:06 xtrabackup_binlog_info -rw-r-----. 1 root root 89 11月 22 19:06 xtrabackup_checkpoints -rw-r-----. 1 root root 2560 11月 22 19:06 xtrabackup_logfile [root@centos6 2016-11-22_19-06-45]# cat xtrabackup_checkpoints backup_type = full-backuped #備份類型,例如完全備份、增量備份等 from_lsn = 0 #日志序列號(hào)從0開始 to_lsn = 1649842 #日志序列號(hào)到哪 last_lsn = 1649842 #日志序列號(hào)到哪結(jié)束 compact = 0 [root@centos6 2016-11-22_19-06-45]# cat xtrabackup_binlog_info #當(dāng)前正使用的二進(jìn)制日志文件及其二進(jìn)制位置 master-bin.000005 245 [root@centos6 2016-11-22_19-06-45]# cat xtrabackup_binary #備份中用到的xtrabackup的可執(zhí)行文件 xtrabackup_55 [root@centos6 2016-11-22_19-06-45]# cat backup-my.cnf #備份命令中用到的配置選項(xiàng)信息 # This MySQL options file was generated by innobackupex. # The MySQL server [mysqld] innodb_data_file_path=ibdata1:10M:autoextend innodb_log_files_in_group=2 innodb_log_file_size=5242880 innodb_fast_checksum=0 innodb_page_size=16384 innodb_log_block_size=512 [root@centos6 2016-11-22_19-06-45]#
使用innobackupex進(jìn)行增量備份及數(shù)據(jù)恢復(fù):
每個(gè)InnoDB的頁面都會(huì)包含一個(gè)LSN信息,每當(dāng)相關(guān)的數(shù)據(jù)發(fā)生改變,相關(guān)的頁面的LSN就會(huì)自動(dòng)增長(zhǎng)。這正是InnoDB表可以進(jìn)行增量備份的基礎(chǔ),即innobackupex通過備份上次完全備份之后發(fā)生改變的頁面來實(shí)現(xiàn)。
要實(shí)現(xiàn)第一次增量備份,可以使用下面的命令進(jìn)行:
# innobackupex --incremental /backup --incremental-basedir=BASEDIR
其中,BASEDIR指的是完全備份所在的目錄,此命令執(zhí)行結(jié)束后,innobackupex命令會(huì)在/backup目錄中創(chuàng)建一個(gè)新的以時(shí)間命名的目錄以存放所有的增量備份數(shù)據(jù)。另外,在執(zhí)行過增量備份之后再一次進(jìn)行增量備份時(shí),其--incremental-basedir應(yīng)該指向上一次的增量備份所在的目錄。
需要注意的是,增量備份僅能應(yīng)用于InnoDB或XtraDB表,對(duì)于MyISAM表而言,執(zhí)行增量備份時(shí)其實(shí)進(jìn)行的是完全備份。
“準(zhǔn)備”(prepare)增量備份與整理完全備份有著一些不同,尤其要注意的是:
(1)需要在每個(gè)備份(包括完全和各個(gè)增量備份)上,將已經(jīng)提交的事務(wù)進(jìn)行“重放”?!爸胤拧敝?,所有的備份數(shù)據(jù)將合并到完全備份上。
(2)基于所有的備份將未提交的事務(wù)進(jìn)行“回滾”。
于是,操作就變成了:
# innobackupex --apply-log --redo-only BASE-DIR
接著執(zhí)行:
# innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1
而后是第二個(gè)增量:
# innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-2
innobackupex --copy-back /path/to/BACKUP-DIR #最起始的數(shù)據(jù)
其中BASE-DIR指的是完全備份所在的目錄,而INCREMENTAL-DIR-1指的是第一次增量備份的目錄,INCREMENTAL-DIR-2指的是第二次增量備份的目錄,其它依次類推,即如果有多次增量備份,每一次都要執(zhí)行如上操作
實(shí)例說明:本次實(shí)戰(zhàn)練習(xí)增量數(shù)據(jù)的備份和恢復(fù),在實(shí)驗(yàn)過程中經(jīng)歷過兩次增量,并對(duì)相應(yīng)的數(shù)據(jù)庫做出了修改,增量完成后,停止mysql數(shù)據(jù)庫,刪除數(shù)據(jù)目錄,進(jìn)行數(shù)據(jù)恢復(fù)操作。
實(shí)現(xiàn)代碼如下:
innobackupex --user=root /mybackups/ #第一次完全備份 innobackupex --incremental /mybackups/ --incremental-basedir=/mybackups/2016-11-22_19-24-37/ #第一次增量備份,向hellodb數(shù)據(jù)庫中插入了表及其刪除了表t1 innobackupex --incremental /mybackups/ --incremental-basedir=/mybackups/2016-11-22_19-27-55/ #第二次增量備份,此時(shí)修改了數(shù)據(jù)庫mydbs,創(chuàng)建了表和插入了數(shù)據(jù) innobackupex --apply-log --redo-only /mybackups/2016-11-22_19-24-37/ #將已近提交的事務(wù)重放 innobackupex --apply-log --redo-only /mybackups/2016-11-22_19-24-37/ --incremental-dir=/mybackups/2016-11-22_19-27-55/ #重放之后,所有的備份數(shù)據(jù)合并到完全備份中 innobackupex --apply-log --redo-only /mybackups/2016-11-22_19-24-37/ --incremental-dir=/mybackups/2016-11-22_19-29-57/ #重放之后,所有的備份數(shù)據(jù)合并到完全備份中 innobackupex --copy-back /mybackups/2016-11-22_19-24-37/ #此時(shí)數(shù)據(jù)已經(jīng)和最后一次增量同步,使用此數(shù)據(jù)進(jìn)行恢復(fù) [root@centos6 data]# cd /mybackups/2016-11-22_19-24-37/ [root@centos6 2016-11-22_19-24-37]# cat xtrabackup_checkpoints backup_type = full-prepared #第一次完全備份,重放后將和最后一次增量備份中的序列號(hào)信息一致 from_lsn = 0 to_lsn = 1708653 last_lsn = 1708653 compact = 0 [root@centos6 2016-11-22_19-24-37]# cat ../2016-11-22_19-29-57/xtrabackup_checkpoints backup_type = incremental from_lsn = 1703868 to_lsn = 1708653 last_lsn = 1708653 compact = 0 [root@centos6 2016-11-22_19-24-37]# cat xtrabackup_binlog_info master-bin.000006 8434 [root@centos6 2016-11-22_19-24-37]# cat ../2016-11-22_19-29-57/xtrabackup_binlog_info master-bin.000006 8434 [root@centos6 2016-11-22_19-24-37]# [root@centos6 data]# ll 總用量 18456 drwxr-xr-x. 2 root root 4096 11月 22 19:37 hellodb -rw-r--r--. 1 root root 18874368 11月 22 19:37 ibdata1 drwxr-xr-x. 2 root root 4096 11月 22 19:37 mydb drwxr-xr-x. 2 root root 4096 11月 22 19:37 mydbs drwxr-xr-x. 2 root root 4096 11月 22 19:37 mysql drwxr-xr-x. 2 root root 4096 11月 22 19:37 performance_schema drwxr-xr-x. 2 root root 4096 11月 22 19:37 test [root@centos6 data]# chown -R mysql.mysql ./* [root@centos6 data]# cd ../binlogs/ [root@centos6 binlogs]# mysqlbinlog --start-position=245 master-bin.000007 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #161122 19:31:08 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.32-MariaDB-log created 161122 19:31:08 # Warning: this binlog is either in use or was not closed properly. BINLOG ' fCw0WA8BAAAA8QAAAPUAAAABAAQANS41LjMyLU1hcmlhREItbG9nAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAA+1uD6g== '/*!*/; # at 245 #161122 19:31:49 server id 1 end_log_pos 355 Querythread_id=9exec_time=0error_code=0 use `mydbs`/*!*/; SET TIMESTAMP=1479814309/*!*/; SET @@session.pseudo_thread_id=9/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create table students(id int,name varchar(20)) /*!*/; # at 355 #161122 19:32:26 server id 1 end_log_pos 424 Querythread_id=9exec_time=0error_code=0 SET TIMESTAMP=1479814346/*!*/; BEGIN /*!*/; # at 424 #161122 19:32:26 server id 1 end_log_pos 537 Querythread_id=9exec_time=0error_code=0 SET TIMESTAMP=1479814346/*!*/; insert into students values (1,'tom'),(2,'jerry') /*!*/; # at 537 #161122 19:32:26 server id 1 end_log_pos 564 Xid = 155 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; [root@centos6 binlogs]# mysqlbinlog --start-position=245 master-bin.000007>/root/incr.sql [root@centos6 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.32-MariaDB-log MariaDB Server Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>set sql_log_bin=0; MariaDB [hellodb]>source /root/incr.sql MariaDB [hellodb]>set sql_log_bin=1; MariaDB [hellodb]>use mydbs; MariaDB [mydbs]> show tables; +-----------------+ | Tables_in_mydbs | +-----------------+ | students | | t1 | +-----------------+ 2 rows in set (0.00 sec) MariaDB [mydbs]> select * from studnets; ERROR 1146 (42S02): Table 'mydbs.studnets' doesn't exist MariaDB [mydbs]> select * from students; +------+-------+ | id | name | +------+-------+ | 1 | tom | | 2 | jerry | +------+-------+ 2 rows in set (0.00 sec) MariaDB [mydbs]> use hellodb; Database changed MariaDB [hellodb]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | tbl | | teachers | | toc | +-------------------+ 8 rows in set (0.00 sec) MariaDB [hellodb]>
經(jīng)過查看數(shù)據(jù)和刪除數(shù)據(jù)目錄前進(jìn)行對(duì)比,如數(shù)據(jù)一致則代表數(shù)據(jù)恢復(fù)成功,xtrabackup+二進(jìn)制日志可實(shí)現(xiàn)數(shù)據(jù)的完全備份、增量備份、完全備份恢復(fù)和增量數(shù)據(jù)恢復(fù),同時(shí)在使用此款備份工具時(shí),不會(huì)影響客戶的正常訪問,達(dá)到提高用戶體驗(yàn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。