ホームページ >データベース >mysql チュートリアル > MySQL 备份和恢复(三)Xtrabackup实现热备:完全备份+增量备份
PerconaXtraBackupistheworld’sonlyopen-source,freeMySQLhotbackupsoftwarethatperformsnon-blockingbackupsforInnoDBandXtraDBdatabases.WithPerconaXtraBack
Percona XtraBackup is the world’s only open-source, free MySQL hot backup software that performs non-blocking backups for InnoDB and XtraDB databases. With Percona XtraBackup, you can achieve the following benefits:
以上摘自官方文档对Xtrabackup的描述。
Xtrabackup是Percona公司的开源mysql热备软件,软件自身支持完全备份和增量备份,功能强大,使用简单,且备份结束会自动检查备份的可用性。对于InnoDB和XtraDB可以实现无阻塞的备份。
Xtrabackup会在备份目录下自动生成以当前日期和时间为名的目录,目录下包含当前备份所有数据文件和丰富的当前备份时的状态信息。
实例演示:
xtrabackup的安装,安装包可从官网下载,官方提供了二进制和源码包,根据自己需要下载
/* 下载官方的rpm包安装,xtrabackup要依赖perl-DBD-mysql包,不想手动解决依赖关系可以yum localinstall安装 */ [root@console ~]# yum localinstall --nogpgcheck percona-xtrabackup-2.1.4-656.rhel6.x86_64.rpm -y /* xtrabackup安装后生成的文件不多 */ [root@console ~]# rpm -ql percona-xtrabackup /usr/bin/innobackupex /* 备份时用的命令,会根据mysql版本自动调用xtrabackup_{55,56} */ /usr/bin/innobackupex-1.5.1 /usr/bin/xbcrypt /* 提供备份过程加密支持 */ /usr/bin/xbstream /* 支持流式备份 */ /usr/bin/xtrabackup /usr/bin/xtrabackup_55 / * 这个和下面那个才是备份过程实际调用的备份程序 */ /usr/bin/xtrabackup_56 /usr/share/doc/percona-xtrabackup-2.1.4 /usr/share/doc/percona-xtrabackup-2.1.4/COPYING /* 文档只有一个软件授权文件,没有man文档,不过--help给出的帮助信息也比较丰富 */备份过程:
/* 在库db1中准备一张表tb1,做效果比较用 */ MariaDB [(none)]> CREATE DATABASE db1; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> USE db1 Database changed MariaDB [db1]> CREATE TABLE `tb1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(30) NOT NULL, `Gender` enum('F','M','O') NOT NULL, PRIMARY KEY (`id`) ); Query OK, 0 rows affected (0.22 sec) MariaDB [db1]> DESC tb1; +--------+-------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(30) | NO | | NULL | | | Gender | enum('F','M','O') | NO | | NULL | | +--------+-------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) MariaDB [db1]> INSERT INTO tb1 (Name,Gender) VALUES ('Tom','M'),('Jerry','F'); Query OK, 2 rows affected (0.05 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [db1]> SELECT * FROM tb1; +----+-------+--------+ | id | Name | Gender | +----+-------+--------+ | 1 | Tom | M | | 2 | Jerry | F | +----+-------+--------+ 2 rows in set (0.00 sec) /* 创建一个用于备份的最小权限的用户 */ MariaDB [(none)]> CREATE USER 'bakuser'@'localhost' IDENTIFIED BY 'backupass'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> REVOKE ALL PRIVILEGES,GRANT OPTION FROM 'bakuser'@'localhost'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'bakuser'@'localhost'; Query OK, 0 rows affected (0.00 sec)现在先做一次完全备份:
/* 全备只需指定用于备份的用户名、密码和备份路径即可,最后出现innobackupex: completed OK! 则代表备份成功 */ [root@node1 ~]# innobackupex --user=bakuser --password=backupass /innobackup/ innobackupex: Backup created in directory '/innobackup/2014-07-31_09-27-36' innobackupex: MySQL binlog position: filename 'mysql-bin.000001', position 1270 140731 09:27:39 innobackupex: Connection to database server closed 140731 09:27:40 innobackupex: completed OK! /* 看一下备份都生成了哪些文件,除了数据库文件外还多了一些文件,注意由于我用root登陆系统,备份后的文件属主属组都是root,恢复后要改为运行mysqld进程的用户,不然mysql起不来 */ [root@node1 ~]# cd /innobackup/2014-07-31_09-27-36/ [root@node1 2014-07-31_09-27-36]# ll total 18476 -rw-r--r--. 1 root root 260 Jul 31 09:27 backup-my.cnf /* 备份命令用到的配置选项信息 */ drwx------. 2 root root 4096 Jul 31 09:27 db1 drwx------. 2 root root 4096 Jul 31 09:27 hellodb -rw-r-----. 1 root root 18874368 Jul 31 09:27 ibdata1 drwx------. 2 root root 4096 Jul 31 09:27 mydb drwxr-xr-x. 2 root root 4096 Jul 31 09:27 mysql drwxr-xr-x. 2 root root 4096 Jul 31 09:27 performance_schema drwxr-xr-x. 2 root root 4096 Jul 31 09:27 test -rw-r--r--. 1 root root 13 Jul 31 09:27 xtrabackup_binary /* 记录备份过程实际用的备份程序 */ -rw-r--r--. 1 root root 24 Jul 31 09:27 xtrabackup_binlog_info /* 记录备份时的二进制日志文件和当前的位置 */ -rw-r-----. 1 root root 89 Jul 31 09:27 xtrabackup_checkpoints /* 记录备份类型、 状态(是否prepared)、是否压缩、备份的LSN(Log Serial Number日志序列号)范围等信息 */ -rw-r-----. 1 root root 2560 Jul 31 09:27 xtrabackup_logfile /* xtrabackup自己的日志文件,新版本中不直接可见 */ [root@node1 2014-07-31_09-27-36]# 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@node1 2014-07-31_09-27-36]# cat xtrabackup_binary xtrabackup_55[root@node1 2014-07-31_09-27-36]# [root@node1 2014-07-31_09-27-36]# cat xtrabackup_binlog_info mysql-bin.000001 1270 [root@node1 2014-07-31_09-27-36]# cat xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 1660869 last_lsn = 1660869 compact = 0 [root@node1 2014-07-31_09-27-36]# file xtrabackup_logfile xtrabackup_logfile: data