1. 程式人生 > >MySQL innobackupex全量備份恢復

MySQL innobackupex全量備份恢復

ann sql sta important 其他 fin rect exec --

轉自 http://blog.itpub.net/27099995/viewspace-1295099/

先簡單介紹一下這個工具:innobackupex
innobackupex比xtarbackup有更強的功能,它整合了xtrabackup和其他的一些功能,他不但可以全量備份/恢復,還可以基於時間的增量備份與恢復。
innobackupex備份原理
innobackupex首先調用xtrabackup來備份innodb數據文件,當xtrabackup完成後,innobackupex就查看文件xtrabackup_suspended ;然後執行“FLUSH TABLES WITH READ LOCK”來備份其他的文件

innobackupex恢復原理
innobackupex首先讀取my.cnf,查看變量(datadir,innodb_data_home_dir,innodb_data_file_path,innodb_log_group_home_dir)對應的目錄是存在,確定相關目錄存在後,然後先copy myisam表和索引,然後在copy innodb的表、索引和日誌。

介紹一下環境:
MySQL:5.6.19
安裝路徑:/u01/mysql
數據文件:/u01/mysql/data
備份源:/u02/backup
我是異機恢復,和本機操作一樣。
一、 全量備份
步驟:
./innobackupex --user=root --password=root --host=172.17.210.112 --parallel=4 --throttle=400 --stream=tar /mysqlbak/innobackupex 2>/mysqlbak/innobackupex/bak.log 1>/mysqlbak/innobackupex/fullbak.tar
註釋一下,常用的參數。
--user=root 備份操作用戶名,一般都是root用戶
--password=root123 密碼
--host=172.17.210.112 主機ip,本地可以不加
--parallel=4 --throttle=400 並行個數,根據主機配置選擇合適的,默認是1個,多個可以加快備份速度。
--stream=tar 壓縮類型,這裏選擇tar格式,可以加,可不加。加上文件就小一點,在備份的時候就已經打包好了。(這裏是打包,不是壓縮)
/mysqlbak/innobackupex 備份存放的目錄
2>/mysqlbak/innobackupex/bak.log 備份日誌,將備份過程中的輸出信息重定向到bak.log
1>/mysqlbak/innobackupex/fullbak.tar 備份文件壓縮後的名字
給出不壓縮的全備:
./innobackupex --user=root --password=root --host=172.17.210.112 --parallel=4 --throttle=400 /mysqlbak/innobackupex 2>/mysqlbak/innobackupex/bak.log 1>/mysqlbak/innobackupex/

查看日誌信息,會出現
141011 09:44:02 innobackupex: Executing FLUSH ENGINE LOGS...
141011 09:44:02 innobackupex: Waiting for log copying to finish

xtrabackup: The latest check point (for incremental): ‘14275993522‘
xtrabackup: Stopping log copying thread.
.>> log scanned up to (14275993522)

xtrabackup: Creating suspend file ‘/tmp/xtrabackup_log_copied‘ with pid ‘19659‘
141011 09:44:03 innobackupex: All tables unlocked
141011 09:44:03 innobackupex: Waiting for ibbackup (pid=19659) to finish
xtrabackup: Transaction log of lsn (14275990028) to (14275993522) was copied.

innobackupex: Backup created in directory ‘/mysqlbak/innobackupex‘
141011 09:44:04 innobackupex: Connection to database server closed
innobackupex: You must use -i (--ignore-zeros) option for extraction of the tar stream.
141011 09:44:04 innobackupex: completed OK!
表示備份成功。

二、全量恢復
恢復備份文件要保證datadir文件為空,否則會報如下的錯誤
[root@newbidb data]# innobackupex --user=root /data/backup/

IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".

Original data directory is not empty! at /usr/bin/innobackupex line 568.

恢復第一步:應用日誌。
[root@newbidb bin]# ./innobackupex --user=root --password=root1 --defaults-file=/etc/my.cnf --apply-log /u02/backup/
恢復第二步:拷貝文件。
[root@newbidb bin]# ./innobackupex --user=root --password=root1 --defaults-file=/etc/my.cnf --copy-back /u02/backup/
註釋一下,常用的參數。
--defaults-file=/etc/my.cnf 恢復會使用my.cnf文件把需要恢復的文件,恢復到my.cnf指定的位置。
--apply-log 這是備份時產生的日誌,
--copy-back 這是備份源,解壓後的備份文件。

恢復需要一點點的時間,出現下面信息表示恢復成功。
innobackupex: Starting to copy InnoDB system tablespace
innobackupex: in ‘/u02/backup‘
innobackupex: back to original InnoDB data directory ‘/u01/mysql/data‘
innobackupex: Copying ‘/u02/backup/ibdata1‘ to ‘/u01/mysql/data/ibdata1‘

innobackupex: Starting to copy InnoDB undo tablespaces
innobackupex: in ‘/u02/backup‘
innobackupex: back to ‘/u01/mysql/data‘

innobackupex: Starting to copy InnoDB log files
innobackupex: in ‘/u02/backup‘
innobackupex: back to original InnoDB log directory ‘/u01/mysql/data‘
innobackupex: Copying ‘/u02/backup/ib_logfile1‘ to ‘/u01/mysql/data/ib_logfile1‘
innobackupex: Copying ‘/u02/backup/ib_logfile0‘ to ‘/u01/mysql/data/ib_logfile0‘
innobackupex: Finished copying back files.

恢復第三步:修改文件權限。
cd 到data目錄
chown -R mysql.mysql data/

可能出現的報錯:
1、出現下面錯誤,先初始化一下mysql
[root@newbidb support-files]# ./mysql.server start
Starting MySQL...The server quit without updating PID file (/u01/mysql/data/newbidb.pid).[FAILED]
[root@newbidb script]# ./mysql_install_db --basedir=/u01/mysql --no-defaults --skip-name-resolve --user=mysql --datadir=/u01/mysql/data

2、權限:應該恢復使用的是root用戶,但是MySQL需要MySQL用戶去訪問。
[root@newbidb support-files]# ./mysql.server restart
MySQL server PID file could not be found![FAILED]
Starting MySQL.The server quit without updating PID file (/u01/mysql/data/newbidb.pid).[FAILED]

3、連接MySQL的時候
[root@newbidb bin]# ./mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
沒有這個文件,touch mysql.sock 並修改文件權限。

MySQL innobackupex全量備份恢復