2.XtraBackup备份工具
问题
1)安装XtraBackup软件包。
2)使用XtraBackup执行完整备份、增量备份。
3)准备数据恢复目录。
步骤
实现此案例需要按照如下步骤进行。
步骤一:安装XtraBackup软件包
1)了解软件包描述信息
[root@dbsvr1 pub]# rpm -qpi percona-xtrabackup-2.1.6-702.rhel6.x86_64.rpm
Name : percona-xtrabackup Relocations: (not relocatable)
Version : 2.1.6 Vendor: (none)
Release : 702.rhel6 Build Date: 2013年11月20日 星期三 21时20分45秒
Install Date: (not installed) Build Host: jhc-new-2-centos6-64.novalocal
Group : Server/Databases Source RPM: percona-xtrabackup-2.1.6-702.rhel6.src.rpm
Size : 25077849 License: GPLv2
Signature : (none)
Packager : Percona Development Team mysql-dev@percona.com
URL : http://www.percona.com/software/percona-xtrabackup/
Summary : XtraBackup online backup for MySQL / InnoDB
Description :
Percona XtraBackup is OpenSource online (non-blockable) backup solution for InnoDB and XtraDB engines.
2)安装依赖包perl-DBD-MySQL
使用RHEL 6自带的即可,yum方式安装:
[root@dbsvr1 pub]# yum -y install perl-DBD-MySQL
… …
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : perl-DBD-MySQL-4.013-3.el6.x86_64 1/1
Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 1/1
Installed:
perl-DBD-MySQL.x86_64 0:4.013-3.el6
Complete!
如果未安装此依赖包,则直接安装percona-xtrabackup时会报错:
[root@dbsvr1 pub]# rpm -ivh percona-xtrabackup-2.1.6-702.rhel6.x86_64.rpm
error: Failed dependencies:
perl(DBD::mysql) is needed by percona-xtrabackup-2.1.6-702.rhel6.x86_64
3)安装perl-DBD-MySQL
[root@dbsvr1 pub]# rpm -ivh percona-xtrabackup-2.1.6-702.rhel6.x86_64.rpm
Preparing… ########################################### [100%]
1:percona-xtrabackup ########################################### [100%]
4)确认安装的主要程序/脚本
[root@dbsvr1 pub]# rpm -ql percona-xtrabackup
/usr/bin/innobackupex
/usr/bin/innobackupex-1.5.1
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/bin/xtrabackup_55 //适用 MySQL 5.5版本
/usr/bin/xtrabackup_56 //适用 MySQL 5.6版本
/usr/share/doc/percona-xtrabackup-2.1.6
/usr/share/doc/percona-xtrabackup-2.1.6/COPYING
步骤二:使用XtraBackup执行数据库备份
1)做一个完整备份
创建存放备份的文件夹:
[root@dbsvr1 ~]# mkdir -p /backup/mysql
将所有库完整备份到 /backup/mysql/文件夹下,选项 --backup表示备份,–datadir指定MySQL的数据库存放目录,–target-dir指定目标文件夹:
[root@dbsvr1 ~]# xtrabackup_56 –backup
–datadir=/var/lib/mysql/
–target-dir=/backup/mysql/
xtrabackup_56 version 2.1.6 for MySQL server 5.6.11 Linux (x86_64) (revision id: 702)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql/
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
log scanned up to (1787687)
InnoDB: Allocated tablespace 1, old maximum was 0
[01] Copying ./ibdata1 to /backup/mysql/ibdata1
[01] …done
[01] Copying ./mysql/innodb_table_stats.ibd to /backup/mysql/mysql/innodb_table_stats.ibd
[01] …done
[01] Copying ./mysql/innodb_index_stats.ibd to /backup/mysql/mysql/innodb_index_stats.ibd
[01] …done
[01] Copying ./mysql/slave_worker_info.ibd to /backup/mysql/mysql/slave_worker_info.ibd
[01] …done
[01] Copying ./mysql/slave_relay_log_info.ibd to /backup/mysql/mysql/slave_relay_log_info.ibd
[01] …donelog scanned up to (1787687)
[01] Copying ./mysql/slave_master_info.ibd to /backup/mysql/mysql/slave_master_info.ibd
[01] …done
[01] Copying ./db1/tb1.ibd to /backup/mysql/db1/tb1.ibd
[01] …done
[01] Copying ./userdb2/userlist.ibd to /backup/mysql/userdb2/userlist.ibd
[01] …done
[01] Copying ./userdb2/userlist2.ibd to /backup/mysql/userdb2/userlist2.ibd
[01] …done
xtrabackup: The latest check point (for incremental): ‘1787687’
xtrabackup: Stopping log copying thread.
.>> log scanned up to (1787687)
xtrabackup: Transaction log of lsn (1787687) to (1787687) was copied.
确认备份好的文件数据:
[root@dbsvr1 ~]# ls /backup/mysql/
db1 ibdata1 mysql userdb2 xtrabackup_checkpoints xtrabackup_logfile
2)做一个增量备份(基于前一步的完整备份)
随意做一些新增或更改库表的操作,比如在test库中新建一个mytb的表:
mysql> USE test;
Database changed
mysql> CREATE TABLE mytb(id int(4), name varchar(24));
Query OK, 0 rows affected (0.18 sec)
以前一次保存到/backup/mysql/的完整备份为基础,做一个增量备份,保存到/backup/inc01/,指定增量备份参照的基本目录(完整备份目录)需要用到选项–incremental-basedir。相关操作如下:
[root@dbsvr1 ~]# xtrabackup_56 --backup
–datadir=/var/lib/mysql/
–target-dir=/backup/inc01/
–incremental-basedir=/backup/mysql/
xtrabackup_56 version 2.1.6 for MySQL server 5.6.11 Linux (x86_64) (revision id: 702)
incremental backup from 1787687 is enabled.
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql/
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
log scanned up to (1793843)
InnoDB: Allocated tablespace 13, old maximum was 0
xtrabackup: using the full scan for incremental backup
[01] Copying ./ibdata1 to /backup/inc01/ibdata1.delta
[01] …done
[01] Copying ./test/mytb.ibd to /backup/inc01/test/mytb.ibd.delta
[01] …done
[01] Copying ./mysql/innodb_table_stats.ibd to /backup/inc01/mysql/innodb_table_stats.ibd.delta
[01] …done
[01] Copying ./mysql/innodb_index_stats.ibd to /backup/inc01/mysql/innodb_index_stats.ibd.delta
[01] …done
[01] Copying ./mysql/slave_worker_info.ibd to /backup/inc01/mysql/slave_worker_info.ibd.delta
[01] …done
[01] Copying ./mysql/slave_relay_log_info.ibd to /backup/inc01/mysql/slave_relay_log_info.ibd.delta
[01] …done
[01] Copying ./mysql/slave_master_info.ibd to /backup/inc01/mysql/slave_master_info.ibd.delta
[01] …done
[01] Copying ./db1/tb1.ibd to /backup/inc01/db1/tb1.ibd.deltalog scanned up to (1793843)
[01] …done
[01] Copying ./userdb2/userlist.ibd to /backup/inc01/userdb2/userlist.ibd.delta
[01] …done
[01] Copying ./userdb2/userlist2.ibd to /backup/inc01/userdb2/userlist2.ibd.delta
[01] …done
xtrabackup: The latest check point (for incremental): ‘1793843’
xtrabackup: Stopping log copying thread.
.>> log scanned up to (1793843)
xtrabackup: Transaction log of lsn (1793843) to (1793843) was copied.
确认备份好的文件数据:
[root@dbsvr1 ~]# ls /backup/inc01/
db1 ibdata1.meta test xtrabackup_checkpoints
ibdata1.delta mysql userdb2 xtrabackup_logfile
对比完整备份、增量备份的大小:
[root@dbsvr1 ~]# du -sh /backup/mysql/ /backup/inc01/
13M /backup/mysql/ //完整备份的大小
964K /backup/inc01/ //增量备份的大小
步骤三:准备用于恢复的数据库目录
通过XtraBackup工具备份的数据库目录,若要恢复到另一个MySQL服务器,需要先做一个“–prepare”的准备操作。
1)准备恢复“完整备份”
完成准备以后,最终/backup/mysql/可用来重建MySQL服务器。这种情况下,官方建议连做两次–prepare,以确保数据一致性:
[root@dbsvr1 ~]# xtrabackup_56 --prepare
–target-dir=/backup/mysql/ //第1次准备
xtrabackup_56 version 2.1.6 for MySQL server 5.6.11 Linux (x86_64) (revision id: 702)
xtrabackup: cd to /backup/mysql/
xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1787687)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 1767513 and 1767513 in ibdata files do not match the log sequence number 1787687 in the ib_logfiles!
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files…
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer…
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.11 started; log sequence number 1787687
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 1787814
[root@dbsvr1 ~]# xtrabackup_56 --prepare
–target-dir=/backup/mysql/ //第2次准备
… …
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 1787926
2)准备恢复“完整备份+增量备份”
完成准备以后,最终仍然是/backup/mysql/用来重建MySQL服务器,但这种情况下需提前合并相关增量备份的数据:
先准备完整备份目录,添加–apply-log-only仅应用日志:
[root@dbsvr1 ~]# xtrabackup_56 --prepare
–target-dir=/backup/mysql --apply-log-only
xtrabackup_56 version 2.1.6 for MySQL server 5.6.11 Linux (x86_64) (revision id: 702)
xtrabackup: cd to /backup/mysql
xtrabackup: This target seems to be already prepared.
xtrabackup: notice: xtrabackup_logfile was already used to ‘–prepare’.
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 50331648
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Highest supported file format is Barracuda.
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 1787926
然后整合增量备份的数据,通过–incremental-dir选项指定增量位置:
[root@dbsvr1 ~]# xtrabackup_56 --prepare
–target-dir=/backup/mysql --apply-log-only
–incremental-dir=/backup/inc01
xtrabackup_56 version 2.1.6 for MySQL server 5.6.11 Linux (x86_64) (revision id: 702)
incremental backup from 1787687 is enabled.
xtrabackup: cd to /backup/mysql
xtrabackup: This target seems to be already prepared.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1793843)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = /backup/inc01
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
InnoDB: Allocated tablespace 1, old maximum was 0
xtrabackup: page size for /backup/inc01/ibdata1.delta is 16384 bytes
Applying /backup/inc01/ibdata1.delta to ./ibdata1…
xtrabackup: page size for /backup/inc01/test/mytb.ibd.delta is 16384 bytes
Applying /backup/inc01/test/mytb.ibd.delta to ./test/mytb.ibd…
xtrabackup: page size for /backup/inc01/mysql/innodb_table_stats.ibd.delta is 16384 bytes
Applying /backup/inc01/mysql/innodb_table_stats.ibd.delta to ./mysql/innodb_table_stats.ibd…
xtrabackup: page size for /backup/inc01/mysql/slave_relay_log_info.ibd.delta is 16384 bytes
Applying /backup/inc01/mysql/slave_relay_log_info.ibd.delta to ./mysql/slave_relay_log_info.ibd…
xtrabackup: page size for /backup/inc01/mysql/slave_worker_info.ibd.delta is 16384 bytes
Applying /backup/inc01/mysql/slave_worker_info.ibd.delta to ./mysql/slave_worker_info.ibd…
xtrabackup: page size for /backup/inc01/mysql/slave_master_info.ibd.delta is 16384 bytes
Applying /backup/inc01/mysql/slave_master_info.ibd.delta to ./mysql/slave_master_info.ibd…
xtrabackup: page size for /backup/inc01/mysql/innodb_index_stats.ibd.delta is 16384 bytes
Applying /backup/inc01/mysql/innodb_index_stats.ibd.delta to ./mysql/innodb_index_stats.ibd…
xtrabackup: page size for /backup/inc01/db1/tb1.ibd.delta is 16384 bytes
Applying /backup/inc01/db1/tb1.ibd.delta to ./db1/tb1.ibd…
xtrabackup: page size for /backup/inc01/userdb2/userlist2.ibd.delta is 16384 bytes
Applying /backup/inc01/userdb2/userlist2.ibd.delta to ./userdb2/userlist2.ibd…
xtrabackup: page size for /backup/inc01/userdb2/userlist.ibd.delta is 16384 bytes
Applying /backup/inc01/userdb2/userlist.ibd.delta to ./userdb2/userlist.ibd…
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = /backup/inc01
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 1767513 and 1767513 in ibdata files do not match the log sequence number 1793843 in the ib_logfiles!
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files…
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer…
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1009, file name mysql-bin.000002
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 1793843
完成上述操作以后,/backup/mysql/目录即已包含增量备份的数据。