Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点:
(1)备份过程快速、可靠;
(2)备份过程不会打断正在执行的事务;
(3)能够基于压缩等功能节约磁盘空间和流量;
(4)自动实现备份检验;
(5)还原速度快;
Xtrabackup中主要包含两个工具:
xtrabackup:是用于热备份innodb, xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;
innobackupex:是将xtrabackup进行封装的perl脚本,可以备份和恢复MyISAM表以及数据表结构。
Xtrabackup下载地址:
http://www.percona.com/downloads/XtraBackup/XtraBackup-2.1.9/binary/Linux/i686/
[[email protected] MySQL]# mkdir /usr/local/xtrabackup [[email protected] MySQL]# tar -xvf percona-xtrabackup-2.1.9-744-Linux-i686.tar.gz -C /usr/local/xtrabackup [[email protected] ~]# cd /usr/local/xtrabackup/ percona-xtrabackup-2.1.9-Linux-i686 [[email protected] percona-xtrabackup-2.1.9-Linux-i686]# mv bin/ .. [[email protected] percona-xtrabackup-2.1.9-Linux-i686]# mv share/ .. [[email protected] percona-xtrabackup-2.1.9-Linux-i686]# cd /usr/local/xtrabackup/ [[email protected] xtrabackup]# rmdir percona-xtrabackup-2.1.9-Linux-i686/ [[email protected] xtrabackup]# ls bin share [[email protected] xtrabackup]# cd bin/ [[email protected] bin]# ll total 95804 -rwxr-xr-x. 1 root root 168821 May 2 17:42 innobackupex lrwxrwxrwx. 1 root root 12 May 15 20:15 innobackupex-1.5.1 -> innobackupex -rwxr-xr-x. 1 root root 2004765 May 2 17:42 xbcrypt -rwxr-xr-x. 1 root root 2064576 May 2 17:42 xbstream -rwxr-xr-x. 1 root root 10341667 May 2 17:42 xtrabackup -rwxr-xr-x. 1 root root 13483602 May 2 17:36 xtrabackup_55 -rwxr-xr-x. 1 root root 70026286 May 2 17:40 xtrabackup_56 [[email protected] xtrabackup]# echo "export PATH=$PATH:/usr/local/xtrabackup/bin" >> /etc/profile [[email protected] xtrabackup]# source /etc/profile
到此XtraBackup安装完毕了。
1.用XtraBackup实现完全备份
[[email protected] ~]# innobackupex --user=root --password=asdfgh /backup
这时会出现错误:xtrabackup: Error: Please set parameter ‘datadir‘
解决方法:在/etc/my.cnf里加入datadir参数
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
[[email protected] ~]# innobackupex --user=root --password=asdfgh /backup/ [[email protected] ~]# cd /backup/ [[email protected] backup]# ls 2014-05-15_22-33-56 [[email protected] backup]# cd 2014-05-15_22-33-56/ [[email protected] 2014-05-15_22-33-56]# ll total 321592 -rw-r--r--. 1 root root 260 May 15 22:33 backup-my.cnf drwxr-xr-x. 2 root root 4096 May 15 22:34 employees -rw-r-----. 1 root root 329252864 May 15 22:34 ibdata1 drwxr-xr-x. 2 root root 4096 May 15 22:34 imkh drwxr-xr-x. 2 root root 4096 May 15 22:34 mysql drwxr-xr-x. 2 root root 4096 May 15 22:34 performance_schema drwxr-xr-x. 2 root root 4096 May 15 22:34 sakila drwxr-xr-x. 2 root root 4096 May 15 22:34 test drwxr-xr-x. 2 root root 4096 May 15 22:34 test1 drwxr-xr-x. 2 root root 4096 May 15 22:34 world drwxr-xr-x. 2 root root 4096 May 15 22:34 world_innodb -rw-r--r--. 1 root root 13 May 15 22:34 xtrabackup_binary -rw-r--r--. 1 root root 23 May 15 22:34 xtrabackup_binlog_info -rw-r-----. 1 root root 93 May 15 22:34 xtrabackup_checkpoints -rw-r-----. 1 root root 2560 May 15 22:34 xtrabackup_logfile
(1) backup-my.cnf —— 备份命令用到的配置选项信息;
[[email protected] 2014-05-15_22-33-56]# 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
(2) ibdata —— 备份的表空间文件;
(3) xtrabackup_binary —— 备份中用到的xtrabackup的可执行文件;
[[email protected] 2014-05-15_22-33-56]# more xtrabackup_binary xtrabackup_55
(4) xtrabackup_binlog_info —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置;
[[email protected] 2014-05-15_22-33-56]# more xtrabackup_binlog_info mysql-bin.000017 107
(5) xtrabackup_checkpoints —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
[[email protected] 2014-05-15_22-33-56]# more xtrabackup_checkpoints backup_type = full-backuped from_lsn = 0 to_lsn = 836446404 last_lsn = 836446404 compact = 0
(6) xtrabackup_logfile —— 备份的重做日志文件。
在使用innobackupex进行备份时,还可以使用-no-timestamp选项来阻止命令自动创建一个以时间命名的目录;如此一来,innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据。
[[email protected] ~]# innobackupex --user=root --password=asdfgh --no-timestamp /backup1 //不用先创建/backup1
(7)准备(prepare)一个完全备份
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备(prepare)”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
innobakupex命令的-apply-log选项可用于实现上述功能。
[[email protected] backup]# innobackupex --apply-log 2014-05-15_22-33-56/
在实现“准备”的过程中,innobackupex通常还可以使用–use-memory选项来指定其可以使用的内存的大小,默认通常为100M。如果有足够的内存可用,可以多划分一些内存给prepare的过程,以提高其完成速度。
2.使用XtraBackup实现完全备份的恢复
1)模拟数据库损坏
直接使用删除数据目录文件来模拟损坏:
[[email protected] ~]# cd /usr/local/mysql/data/ [[email protected] data]# rm -rf * [[email protected] data]# mysql -uroot -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)
(2)还原完全备份:
innobackupex命令的–copy-back选项用于执行恢复操作,其通过复制所有数据相关的文件至mysql服务器DATADIR目录中来执行恢复过程。innobackupex通过backup-my.cnf来获取DATADIR目录的相关信息。
[[email protected] ~]# innobackupex --copy-back /backup/2014-05-15_22-33-56/ [[email protected] ~]# cd /usr/local/mysql/data/ [[email protected] data]# ll total 331820 drwxr-xr-x. 2 root root 4096 May 15 23:11 employees -rw-r--r--. 1 root root 329252864 May 15 23:11 ibdata1 -rw-r--r--. 1 root root 5242880 May 15 23:11 ib_logfile0 -rw-r--r--. 1 root root 5242880 May 15 23:11 ib_logfile1 drwxr-xr-x. 2 root root 4096 May 15 23:11 imkh drwxr-xr-x. 2 root root 4096 May 15 23:11 mysql drwxr-xr-x. 2 root root 4096 May 15 23:11 performance_schema drwxr-xr-x. 2 root root 4096 May 15 23:11 sakila drwxr-xr-x. 2 root root 4096 May 15 23:11 test drwxr-xr-x. 2 root root 4096 May 15 23:11 test1 drwxr-xr-x. 2 root root 4096 May 15 23:11 world drwxr-xr-x. 2 root root 4096 May 15 23:11 world_innodb -rw-r--r--. 1 root root 29 May 15 23:11 xtrabackup_binlog_pos_innodb
(3)修改还原后的数据目录权限:
[[email protected] data]# chown -R mysql:mysql . [[email protected] data]# ll total 331820 drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 employees -rw-r--r--. 1 mysql mysql 329252864 May 15 23:11 ibdata1 -rw-r--r--. 1 mysql mysql 5242880 May 15 23:11 ib_logfile0 -rw-r--r--. 1 mysql mysql 5242880 May 15 23:11 ib_logfile1 drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 imkh drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 mysql drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 performance_schema drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 sakila drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 test drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 test1 drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 world drwxr-xr-x. 2 mysql mysql 4096 May 15 23:11 world_innodb -rw-r--r--. 1 mysql mysql 29 May 15 23:11 xtrabackup_binlog_pos_innodb
利用XtraBackup实现MySQL物理备份恢复,布布扣,bubuko.com