《MySQL 系列連載之 XtraBackup 全量熱備 or 恢復(fù)實(shí)踐(2)》要點(diǎn):
本文介紹了MySQL 系列連載之 XtraBackup 全量熱備 or 恢復(fù)實(shí)踐(2),希望對您有用。如果有疑問,可以聯(lián)系我們。
如果您在本文遇到任何問題或疑問請到QQ群中與我們交流.也可在下方進(jìn)行評論.我將在第一時(shí)間和您進(jìn)行交流,共同學(xué)習(xí).
QQ群:201777608、526871767、1689067(加群時(shí)注明:運(yùn)維派)
#系統(tǒng)環(huán)境
[root@master tools]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@master tools]# uname -r
2.6.32-642.el6.x86_64
#主數(shù)據(jù)庫版本
[root@master ~]# mysql -V
mysql Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1
#檢查數(shù)據(jù)庫引擎
mysql> show engines;
#主從數(shù)據(jù)庫同步注意點(diǎn) [mysqld]
#主從之間的id不能相同 server-id
#啟用二進(jìn)制日志 log-bin
#一般在從庫開啟(可選) read_only #推薦使用InnoDB并做好相關(guān)配置
#檢查主從數(shù)據(jù)庫狀態(tài)
[root@master ~]# mysql -e “show global variables like ‘server_id’;”
+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id | 241 |
+—————+——-+
[root@slave01 ~]# mysql -e “show global variables like ‘server_id’;”
+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id | 242 |
+—————+——-+
官網(wǎng)安裝包地址 – https://www.percona.com/downloads/XtraBackup/LATEST/
1)源碼安裝Xtrabackup
將源碼包下載到/usr/local/src下
源碼包下載
cd /usr/local/src
#安裝依賴
yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool zlib-devel libgcrypt-devel libcurl-devel crypt* libgcrypt* python-sphinx openssl imake libxml2-devel expat-devel ncurses5-devel ncurses-devle vim-common libgpg-error-devel libidn-devel perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL#下載源碼包
wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/source/percona-xtrabackup-2.1.9.tar.gz#解壓源碼包
tar -zvxf percona-xtrabackup-2.1.9.tar.gzcd percona-xtrabackup-2.1.9
[root@master percona-xtrabackup-2.1.9]# ./utils/build.sh //執(zhí)行該安裝腳本,會(huì)出現(xiàn)下面信息
Build an xtrabackup binary against the specified InnoDB flavor.
Usage: build.sh CODEBASE
where CODEBASE can be one of the following values or aliases:
innodb51 | plugin build against InnoDB plugin in MySQL 5.1
innodb55 | 5.5 build against InnoDB in MySQL 5.5
innodb56 | 5.6,xtradb56, build against InnoDB in MySQL 5.6
| mariadb100,galera56
xtradb51 | xtradb,mariadb51 build against Percona Server with XtraDB 5.1
| mariadb52,mariadb53
xtradb55 | galera55,mariadb55 build against Percona Server with XtraDB 5.5
根據(jù)上面提示和你使用的存儲(chǔ)引擎及版本,選擇相應(yīng)的參數(shù)即可.因?yàn)槲矣玫氖荕ySQL 5.5版本,所以執(zhí)行如下語句安裝:
[root@master percona-xtrabackup-2.1.9]# ./utils/build.sh innodb55
以上語句執(zhí)行成功后,表示安裝完成.
最后,把生成的二進(jìn)制文件拷貝到一個(gè)自定義目錄下(本例中為/home/mysql/admin/bin/percona-xtrabackup-2.1.9),并把該目錄放到環(huán)境變量PATH中.
mkdir -p /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
cp ./innobackupex /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
mv /usr/local/src/percona-xtrabackup-2.1.9/src/xtrabackup_innodb55 xtrabackup_55
cp /usr/local/src/percona-xtrabackup-2.1.9/src/xtrabackup_55 /usr/local/src/percona-xtrabackup-2.1.9/src/xbstream /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
vim /etc/profile
export PATH=$PATH:/home/mysql/admin/bin/percona-xtrabackup-2.1.9/
刷新profile并測試下innobackupex是否正常使用
source /etc/profile
測試下innobackupex是否正常使用
innobackupex –help
1)全量備份操作
執(zhí)行下面語句進(jìn)行全備: mysql的安裝目錄是/application/mysql/ mysql的配置文件路徑/etc/my.cnf 全量備份后的數(shù)據(jù)存放目錄是/backup/mysql/data
mkdir -p /backup/mysql/data/
innobackupex –defaults-file=/etc/my.cnf –user=root /backup/mysql/data/
170404 12:46:29 innobackupex: Waiting for log copying to finish
xtrabackup: The latest check point (for incremental): ‘1639325’
xtrabackup: Stopping log copying thread.
.>> log scanned up to (1639325)
xtrabackup: Creating suspend file ‘/backup/mysql/data/2017-04-04_12-46-24/xtrabackup_log_copied’ with pid ‘21223’
xtrabackup: Transaction log of lsn (1639325) to (1639325) was copied.
170404 12:46:30 innobackupex: All tables unlocked
innobackupex: Backup created in directory ‘/backup/mysql/data/2017-04-04_12-46-24’
innobackupex: MySQL binlog position: filename ‘mysql-bin.000019’, position 967
170404 12:46:30 innobackupex: Connection to database server closed
170404 12:46:30 innobackupex: completed OK!
出現(xiàn)上面的信息,表示備份已經(jīng)ok.
上面執(zhí)行的備份語句會(huì)將mysql數(shù)據(jù)文件(即由my.cnf里的變量datadir指定)拷貝至備份目錄下(/backup/mysql/data)
注意:如果不指定–defaults-file,默認(rèn)值為/etc/my.cnf. 備份成功后,將在備份目錄下創(chuàng)建一個(gè)時(shí)間戳目錄(本例創(chuàng)建的目錄為/backup/mysql/data/2017-04-04_12-46-24),在該目錄下存放備份文件.
還可以在遠(yuǎn)程進(jìn)行全量備份,命令如下:
innobackupex –defaults-file=/etc/my.cnf –user=root –host=127.0.0.1 –parallel=2 –throttle=200 /backup/mysql/data 2>/backup/mysql/data/bak.log 1>/backup/mysql/data/`data +%Y-%m-%d_%H-%M%S`
參數(shù)解釋:
–user=root 備份操作用戶名,一般都是root用戶
–host=127.0.0.1 主機(jī)ip,本地可以不加(適用于遠(yuǎn)程備份).注意要提前在mysql中授予連接的權(quán)限,最好備份前先測試用命令中的用戶名、密碼和host能否正常連接mysql.
–parallel=2 –throttle=200 并行個(gè)數(shù),根據(jù)主機(jī)配置選擇合適的,默認(rèn)是1個(gè),多個(gè)可以加快備份速度.
/backup/mysql/data 備份存放的目錄
2>/backup/mysql/data/bak.log 備份日志,將備份過程中的輸出信息重定向到bak.log
這種備份跟上面相比,備份成功后,不會(huì)自動(dòng)在備份目錄下創(chuàng)建一個(gè)時(shí)間戳目錄,需要如上命令中自己定義.
[root@master src]# ll /backup/mysql/data/
總用量 8
drwxr-xr-x 6 root root 4096 4月 4 12:46 2017-04-04_12-46-24
-rw-r–r– 1 root root 106 4月 4 12:57 bak.log //備份信息都記錄在這個(gè)日志里,如果備份失敗,可以到這里日志里查詢
2)全量備份后的恢復(fù)操作
#進(jìn)入數(shù)據(jù)庫
[root@master data]# mysql#查看當(dāng)前所有數(shù)據(jù)庫
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| xtra_test |
+——————–+
5 rows in set (0.09 sec)#切換到 xtra_test數(shù)據(jù)庫下
mysql> use xtra_test;
Database changed#查看當(dāng)前數(shù)據(jù)庫里的表
mysql> show tables;
+———————+
| Tables_in_xtra_test |
+———————+
| I |
| M |
+———————+
2 rows in set (0.04 sec)#刪除整個(gè) xtra_test庫
mysql> drop database xtra_test;
Query OK, 2 rows affected (0.34 sec)#現(xiàn)在已經(jīng)看不到 xtra_test庫了
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.00 sec)
注意:恢復(fù)之前
1)要先關(guān)閉數(shù)據(jù)庫
2)要?jiǎng)h除數(shù)據(jù)文件和日志文件(也可以mv移到別的地方,只要確保清空mysql數(shù)據(jù)存放目錄就行)
[root@master data]# ps -ef|grep mysqld
root 10929 1 0 10:32 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe –datadir=/application/mysql/data –pid-file=/application/mysql/data/master.pid
mysql 11227 10929 0 10:32 pts/0 00:00:14 /application/mysql/bin/mysqld –basedir=/application/mysql –datadir=/application/mysql/data –plugin-dir=/application/mysql/lib/plugin –user=mysql –log-error=/application/mysql/data/master.err –pid-file=/application/mysql/data/master.pid –port=3306
root 21514 1896 0 13:55 pts/0 00:00:00 grep mysqld
由上面可以看出mysql的數(shù)據(jù)和日志存放目錄是/application/mysql/data
#關(guān)閉MySQL數(shù)據(jù)庫服務(wù)
[root@master data]# service mysqld stop
Shutting down MySQL…. SUCCESS!#移動(dòng)數(shù)據(jù)文件和日志文件到/tmp(當(dāng)然刪除也可以)
[root@master data]# mv /application/mysql/data/* /tmp/[root@master data]# innobackupex –defaults-file=/etc/my.cnf –user=root –use-memory=1G –apply-log /backup/mysql/data/2017-04-04_13-04-05/
[root@master data]# innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back /backup/mysql/data/2017-04-04_13-04-05/
[root@master ~]# chown -R mysql.mysql /application/mysql/data/
可能報(bào)錯(cuò):
sh: xtrabackup: command not found
innobackupex: Error: no ‘mysqld’ group in MySQL options at /home/mysql/admin/bin/percona-xtrabackup-2.1.9/innobackupex line 4350.
解決:將xtrabackup_55復(fù)制成xtrabackup即可
[root@master src]# ls /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
innobackupex xbstream xtrabackup_55 xtrabackup_innodb55[root@master src]# cd /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
[root@master percona-xtrabackup-2.1.9]# cp xtrabackup_55 xtrabackup
[root@master percona-xtrabackup-2.1.9]# ls
innobackupex xbstream xtrabackup xtrabackup_55 xtrabackup_innodb55
檢驗(yàn):執(zhí)行之后就OK了
[root@master percona-xtrabackup-2.1.9]# innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back /backup/mysql/data/2017-04-04_13-04-05/
innobackupex: Copying ‘/backup/mysql/data/2017-04-04_13-04-05/ib_logfile1’ to ‘/application/mysql/data/ib_logfile1’
innobackupex: Copying ‘/backup/mysql/data/2017-04-04_13-04-05/ib_logfile0’ to ‘/application/mysql/data/ib_logfile0’
innobackupex: Finished copying back files.170404 14:24:07 innobackupex: completed OK!
出現(xiàn)上面的信息,說明數(shù)據(jù)恢復(fù)成功了!!
從上面的恢復(fù)操作可以看出,執(zhí)行恢復(fù)分為兩個(gè)步驟:
1)第一步恢復(fù)步驟是應(yīng)用日志(apply-log),為了加快速度,一般建議設(shè)置–use-memory(如果系統(tǒng)內(nèi)存充足,可以使用加大內(nèi)存進(jìn)行備份 ),這個(gè)步驟完成之后,目錄/backup/mysql/data/2017-04-04_13-04-05/下的備份文件已經(jīng)準(zhǔn)備就緒.
2)第二步恢復(fù)步驟是拷貝文件(copy-back),即把備份文件拷貝至原數(shù)據(jù)目錄下.
最后,啟動(dòng)mysql,查看數(shù)據(jù)是否恢復(fù)回來了
#啟動(dòng)數(shù)據(jù)庫
[root@master ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!#進(jìn)入MySQL
[root@master ~]# mysql#顯示所有數(shù)據(jù)庫(可以看到我們之前刪除的xtra_test 庫已經(jīng)恢復(fù)了)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| xtra_test |
+——————–+
5 rows in set (0.00 sec)#進(jìn)入xtra_test 庫
mysql> use xtra_test;
Database changed#查看xtra_test 庫內(nèi)表內(nèi)容(已經(jīng)全部恢復(fù))
mysql> show tables;
+———————+
| Tables_in_xtra_test |
+———————+
| I |
| M |
+———————+
2 rows in set (0.00 sec)
第一篇 MySQL 系列連載之 XtraBackup 備份原理(1)
第二篇 MySQL 系列連載之 XtraBackup全量熱備 or 恢復(fù)實(shí)踐(2)
第三篇 MySQL 系列連載之 XtraBackup 增量熱備 or 恢復(fù)實(shí)踐(3)
感謝您閱讀.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/4261.html