Xtrabackup 是由percona提供的mysql数据库备份工具,是一个开源的工具,能够对innodb和xtradb数据库进行热备和增量备份,对于MyISAM, 仅支持到温备,对MyISAM使用增量备份时,其实是完全备份。Xtrabackup使用简单,功能强大。
官方地址:http://www.percona.com/software/percona-xtrabackup/
安装:
percona-toolkit-2.2.17-1.noarch.rpm percona-xtrabackup-2.2.9-5067.el6.x86_64.rpm [root@martin tools]# yum localinstall percona-*
innobackupex运行条件: 需要MySQL服务处于运行状态
如果要使用一个最小权限的用户进行备份,则可基于如下命令创建此类用户:
mysql> create user 'bkpuser'@'localhost' identified by '222222'; mysql> grant reload,lock tables,replication client on *.* to 'bkpuser'@localhost; mysql> flush privileges;
一个简单的完全备份及还原实现过程:
1.完全备份:
[root@martin mydata]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock /mydata/mybackups/ [root@martin mybackups]# ls 2016-05-24_17-12-47
2.准备一个完全还原备份:
[root@martin mybackups]# innobackupex --apply-log /mydata/mybackups/2016-05-24_17-12-47/ ...... xtrabackup: starting shutdown with innodb_fast_shutdown = 1 InnoDB: FTS optimize thread exiting. InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1653270 160524 17:35:57 innobackupex: completed OK!
模拟数据崩溃:
[root@martin mybackups]# cd /mydata/mysql/data/ [root@martin data]# ls auto.cnf ib_logfile0 ib_logfile1 ibdata1 martin martin.err mysql performance_schema test [root@martin data]# rm -fr *
3.还原:
[root@martin data]# innobackupex --copy-back /mydata/mybackups/2016-05-24_17-12-47/
/mydata/mysql/data [root@martin data]# chown -R mysql.mysql * [root@martin data]# service mysqld start Starting MySQL. SUCCESS! [root@martin data]# ls auto.cnf ib_logfile1 martin martin.pid performance_schema xtrabackup_binlog_pos_innodb ib_logfile0 ibdata1 martin.err mysql test xtrabackup_info
下面实现增量备份过程:
一次完全备份,2次增量备份
[root@martin mydata]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock /mydata/mybackups/ [root@martin mybackups]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock --incremental /mydata/mybackups/ --incremental-basedir=2016-05-24_17-46-48/ #上一次 #.......插入数据 [root@martin mybackups]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock --incremental /mydata/mybackups/ --incremental-basedir=2016-05-24_17-53-02/
还原:
第一次的完全备份:
[root@martin mybackups]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock --apply-log --redo-only /mydata/mybackups/2016-05-24_17-46-48/
合并第一次增量:
[root@martin mybackups]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock --apply-log --redo-only /mydata/mybackups/2016-05-24_17-46-48/ --incremental-dir=/mydata/mybackups/2016-05-24_17-53-02/
合并第二次增量:
[root@martin mybackups]# innobackupex --user=bkpuser --password=222222 --socket=/tmp/mysql.sock --apply-log --redo-only /mydata/mybackups/2016-05-24_17-46-48/ --incremental-dir=/mydata/mybackups/2016-05-24_18-04-24/
执行最后还原:
[root@martin data]# innobackupex --copy-back /mydata/mybackups/2016-05-24_17-46-48/
[root@martin data]# chown -R mysql.mysql * [root@martin data]# ll total 12308 -rw-r--r-- 1 mysql mysql 12582912 May 24 18:14 ibdata1 drwxr-xr-x 2 mysql mysql 4096 May 24 18:14 martin drwxr-xr-x 2 mysql mysql 4096 May 24 18:14 mysql drwxr-xr-x 2 mysql mysql 4096 May 24 18:14 performance_schema drwxr-xr-x 2 mysql mysql 4096 May 24 18:14 test -rw-r--r-- 1 mysql mysql 653 May 24 18:14 xtrabackup_info [root@martin data]# /etc/init.d/mysqld start Starting MySQL. SUCCESS!
[root@martin data]# cat /mydata/mysql/data/xtrabackup_info uuid = e65af8bc-2196-11e6-add0-000c294b9b35 name = tool_name = innobackupex tool_command = --user=bkpuser --password=... --socket=/tmp/mysql.sock --incremental /mydata/mybackups/ --incremental-basedir=2016-05-24_17-53-02/ tool_version = 1.5.1-xtrabackup ibbackup_version = xtrabackup version 2.2.9 based on MySQL server 5.6.22 Linux (x86_64) (revision id: ) server_version = 5.6.30-log start_time = 2016-05-24 18:04:24 end_time = 2016-05-24 18:04:27 lock_time = 1 binlog_pos = filename 'master-bin.000010', position 796 innodb_from_lsn = 1653280 innodb_to_lsn = 1653280 partial = N incremental = Y format = file compact = N compressed = N encrypted = N
更新完毕 如果还有二进制中的文件要继续更新上去
[root@martin log-bin]# mysqlbinlog --start-position=796 master-bin.000010>~/a.bin.sql mysql> set session sql_log_bin=0; mysql> source ~/a.sql mysql> source ~/a.bin.sql mysql> set session sql_log_bin=1;
从完整数据中的可以看到合并到最后更新时间:
[root@martin 2016-05-24_17-46-48]# cat xtrabackup_info uuid = e65af8bc-2196-11e6-add0-000c294b9b35 name = tool_name = innobackupex tool_command = --user=bkpuser --password=... --socket=/tmp/mysql.sock --incremental /mydata/mybackups/ --incremental-basedir=2016-05-24_17-53-02/ tool_version = 1.5.1-xtrabackup ibbackup_version = xtrabackup version 2.2.9 based on MySQL server 5.6.22 Linux (x86_64) (revision id: ) server_version = 5.6.30-log start_time = 2016-05-24 18:04:24 end_time = 2016-05-24 18:04:27 #可以看到最后的增量备份时间 lock_time = 1 binlog_pos = filename 'master-bin.000010', position 796 innodb_from_lsn = 1653280 innodb_to_lsn = 1653280 partial = N incremental = Y format = file compact = N compressed = N