上一篇簡單介紹了一下MySQLdump進行數(shù)據(jù)庫的備份和恢復,這一篇說一下另一種備份工具xtrabackup,在InnoDB事務引擎泛濫的時代,xtrabackup可以很好的支持數(shù)據(jù)庫的熱備份,這就很討人喜,
從網(wǎng)站建設到定制行業(yè)解決方案,為提供成都網(wǎng)站設計、網(wǎng)站制作服務體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設解決方案,助力業(yè)務快速發(fā)展。成都創(chuàng)新互聯(lián)將不斷加快創(chuàng)新步伐,提供優(yōu)質的建站服務。
Xtrabackup在epel源中有,所以直接用yum安裝即可;
完全備份就是直接連接MySQL服務,給定一個目標目錄即可;
[root@www ~]# innobackupex --user=root --host=localhost --port=3306 --password='123456' /data/mydata 181105 20:31:30 innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully. At the end of a successful backup run innobackupex prints "completed OK!". … 181105 20:31:39 Executing UNLOCK TABLES 181105 20:31:39 All tables unlocked 181105 20:31:39 Backup created in directory '/data/mydata/2018-11-05_20-31-30' 181105 20:31:39 [00] Writing backup-my.cnf 181105 20:31:39 [00] ...done 181105 20:31:39 [00] Writing xtrabackup_info 181105 20:31:39 [00] ...done xtrabackup: Transaction log of lsn (9424992) to (9424992) was copied. 181105 20:31:39 completed OK!
然后我們就可以在備份的目錄中看到幾個文件
[root@www ~]# ls /data/mydata/2018-11-05_20-31-30/ backup-my.cnf ibdata1 performance_schema xtrabackup_checkpoints xtrabackup_logfile hellodb mysql wpsdb xtrabackup_info zabbix
除了數(shù)據(jù)庫之外,還有幾個特殊的文件:
xtrabackup_binlog_info:記錄了整個備份過程中的一些屬性信息;
[root@www ~]# cd /data/mydata/2018-11-05_20-31-30/ [root@www 2018-11-05_20-31-30]# cat xtrabackup_info uuid = 725248b0-da18-11e8-9fcc-000c29ceaa48 name = tool_name = innobackupex tool_command = --user=root --host=localhost --port=3306 --password=... /data/mydata tool_version = 2.3.6 ibbackup_version = 2.3.6 server_version = 5.5.60-MariaDB start_time = 2018-11-05 20:31:31 end_time = 2018-11-05 20:31:39 lock_time = 0 binlog_pos = innodb_from_lsn = 0 innodb_to_lsn = 9424992 partial = N incremental = N format = file compact = N compressed = N encrypted = N
backup-my.cnf:記錄與InnoDB存儲引擎相關的重要配置參數(shù);
[root@www 2018-11-05_20-31-30]# cat backup-my.cnf # This MySQL options file was generated by innobackupex. # The MySQL server [mysqld] innodb_checksum_algorithm=innodb innodb_log_checksum_algorithm=innodb innodb_data_file_path=ibdata1:10M:autoextend innodb_log_files_in_group=2 innodb_log_file_size=5242880 innodb_fast_checksum=false innodb_page_size=16384 innodb_log_block_size=512 innodb_undo_directory=. innodb_undo_tablespaces=0
xtrabackup_checkpoints:記錄了此次的類型及起始和終止的LSN;
[root@www 2018-11-05_20-31-30]# cat xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 9424992 last_lsn = 9424992 compact = 0 recover_binlog_info = 0 xtrabackup_binlog_info:記錄當前使用的二進制日志的一致性坐標; [root@www 2018-11-05_20-55-04]# cat xtrabackup_binlog_info binlog.000001 245
完全備份的恢復(mysql的數(shù)據(jù)目錄為空):
恢復數(shù)據(jù)的過程只需要添加一個--copy-back即可完成;
[root@www 2018-11-05_20-55-04]# innobackupex --copy-back ./ 181105 20:59:30 innobackupex: Starting the copy-back operation IMPORTANT: Please check that the copy-back run completes successfully. At the end of a successful copy-back run innobackupex prints "completed OK!".
增量備份:
相比于之前的完全備份,需要--incremental和--incremental-basedir兩個選項即可;
[root@www ~]# innobackupex --incremental /data/mydata --incremental-basedir=/data/mydata/2018-11-05_20-31-30 181105 20:54:41 innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully. At the end of a successful backup run innobackupex prints "completed OK!". … 181105 20:55:14 Executing UNLOCK TABLES 181105 20:55:14 All tables unlocked 181105 20:55:14 Backup created in directory '/data/mydata/2018-11-05_20-55-04' MySQL binlog position: filename 'binlog.000001', position '245' 181105 20:55:14 [00] Writing backup-my.cnf 181105 20:55:14 [00] ...done 181105 20:55:14 [00] Writing xtrabackup_info 181105 20:55:14 [00] ...done xtrabackup: Transaction log of lsn (9424992) to (9424992) was copied. 181105 20:55:14 completed OK!
通過查看xtrabackup-checkpoints文件內容即可;
[root@www 2018-11-05_20-55-04]# cat xtrabackup_checkpoints backup_type = incremental from_lsn = 9424992 to_lsn = 9424992 last_lsn = 9424992 compact = 0 recover_binlog_info = 0
增量備份的恢復過程同上,也是一個--copy-back即可;
xtrabackup是一個相當好的備份工具,在如今事務引擎遍地的時代,良好的熱備份機制和簡單化的操作,對我們來說很友好,所以,我很推薦這個工具,而mysqldump大多用來做測試用,因為mysqldump需要鎖表,所以溫備份機制在這方便不如xtrabackup的熱備份機制。但是,在我們實驗環(huán)境做測試的情況下,這個直接打包成數(shù)據(jù)庫,copy到其他主機就可以釋放,還是很不錯的。