一、簡介
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:空間域名、虛擬主機、營銷軟件、網(wǎng)站建設、周寧網(wǎng)站維護、網(wǎng)站推廣。
Xtrabackup是由percona提供的MySQL數(shù)據(jù)庫備份工具,據(jù)官方介紹,這也是世界上惟一一款開源的能夠?qū)nnodb和xtradb數(shù)據(jù)庫進行熱備的工具。其主要特點是備份過程快速、可靠,不會打斷正在執(zhí)行的事務,能夠基于壓縮等功能節(jié)約磁盤空間和流量,自動實現(xiàn)備份檢測及其還原速度快。
二、安裝及備份、恢復實現(xiàn)
安裝:其最新版的軟件可從 http://www.percona.com/software/percona-xtrabackup/ 獲得。本文基于centos6.x的系統(tǒ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ù)目錄實現(xiàn)恢復:
mysql>set session_sql_log_bin=0; #導入數(shù)據(jù)時讓其不記錄二進制日志 mysql>source /root/hellodb.sql #導入數(shù)據(jù) mysql>set session_sql_log_bin=1; #開啟二進制日志 [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/ #準備完全備份 [root@centos6]#innobackupex --copy-back /mybackups/2016-11-22_15-39-09/ #從完全備份中恢復數(shù)據(jù) [root@centos6 data]# ll #數(shù)據(jù)已然恢復成功 總用量 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 #啟動數(shù)據(jù)庫,在恢復數(shù)據(jù)庫時數(shù)據(jù)庫無需啟動
從安全角度考慮,如果要使用一個最小權(quán)限的用戶進行備份,可創(chuàng)建此用戶進行完全備份
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備份時,其會調(diào)用xtrabackup備份所有的InnoDB表,復制所有關(guān)于表結(jié)構(gòu)定義的相關(guān)文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關(guān)文件,同時還會備份觸發(fā)器和數(shù)據(jù)庫配置信息相關(guān)的文件。這些文件會被保存至一個以時間命令的目錄中。
[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 #日志序列號從0開始 to_lsn = 1649842 #日志序列號到哪 last_lsn = 1649842 #日志序列號到哪結(jié)束 compact = 0 [root@centos6 2016-11-22_19-06-45]# cat xtrabackup_binlog_info #當前正使用的二進制日志文件及其二進制位置 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 #備份命令中用到的配置選項信息 # 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進行增量備份及數(shù)據(jù)恢復:
每個InnoDB的頁面都會包含一個LSN信息,每當相關(guān)的數(shù)據(jù)發(fā)生改變,相關(guān)的頁面的LSN就會自動增長。這正是InnoDB表可以進行增量備份的基礎,即innobackupex通過備份上次完全備份之后發(fā)生改變的頁面來實現(xiàn)。
要實現(xiàn)第一次增量備份,可以使用下面的命令進行:
# innobackupex --incremental /backup --incremental-basedir=BASEDIR
其中,BASEDIR指的是完全備份所在的目錄,此命令執(zhí)行結(jié)束后,innobackupex命令會在/backup目錄中創(chuàng)建一個新的以時間命名的目錄以存放所有的增量備份數(shù)據(jù)。另外,在執(zhí)行過增量備份之后再一次進行增量備份時,其--incremental-basedir應該指向上一次的增量備份所在的目錄。
需要注意的是,增量備份僅能應用于InnoDB或XtraDB表,對于MyISAM表而言,執(zhí)行增量備份時其實進行的是完全備份。
“準備”(prepare)增量備份與整理完全備份有著一些不同,尤其要注意的是:
(1)需要在每個備份(包括完全和各個增量備份)上,將已經(jīng)提交的事務進行“重放”。“重放”之后,所有的備份數(shù)據(jù)將合并到完全備份上。
(2)基于所有的備份將未提交的事務進行“回滾”。
于是,操作就變成了:
# innobackupex --apply-log --redo-only BASE-DIR
接著執(zhí)行:
# innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1
而后是第二個增量:
# 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í)行如上操作
實例說明:本次實戰(zhàn)練習增量數(shù)據(jù)的備份和恢復,在實驗過程中經(jīng)歷過兩次增量,并對相應的數(shù)據(jù)庫做出了修改,增量完成后,停止mysql數(shù)據(jù)庫,刪除數(shù)據(jù)目錄,進行數(shù)據(jù)恢復操作。
實現(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ù)據(jù)庫mydbs,創(chuàng)建了表和插入了數(shù)據(jù) innobackupex --apply-log --redo-only /mybackups/2016-11-22_19-24-37/ #將已近提交的事務重放 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ù)據(jù)已經(jīng)和最后一次增量同步,使用此數(shù)據(jù)進行恢復 [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 #第一次完全備份,重放后將和最后一次增量備份中的序列號信息一致 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ù)目錄前進行對比,如數(shù)據(jù)一致則代表數(shù)據(jù)恢復成功,xtrabackup+二進制日志可實現(xiàn)數(shù)據(jù)的完全備份、增量備份、完全備份恢復和增量數(shù)據(jù)恢復,同時在使用此款備份工具時,不會影響客戶的正常訪問,達到提高用戶體驗。
分享文章:percona-xtrabackup完全及增量備份與恢復
鏈接URL:http://www.rwnh.cn/article12/gpodgc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、標簽優(yōu)化、網(wǎng)站設計、外貿(mào)網(wǎng)站建設、微信公眾號、品牌網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)