Xtrabackup是一个对InnoDB存储引擎做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品.Xtrabackup有两个主要工具:xtrabackup,innobackupex,且只能备份InnoDB和XtraDB两种存储引擎的表,而不能备份MyISAM数据表.使用innobackupex工具必须在mysql配置文件中指定数据目录,xtrabackup工具只能备份数据,不能备份表结构.
mysql dba技术群 378190849
武汉-linux运维群 236415619
1.下载安装xtrabackup工具
[root@tong2 ~]# wget
http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.7/binary/redhat/6/x86_64/Percona-XtraBackup-2.2.7-r5050-el6-x86_64-bundle.tar
[root@tong2 ~]# tar xvf Percona-XtraBackup-2.2.7-r5050-el6-x86_64-bundle.tar
[root@tong2 ~]# ll percona-xtrabackup-*
-rw-rw-r--. 1 root root 4863276 Dec 4 22:18 percona-xtrabackup-2.2.7-5050.el6.x86_64.rpm
-rw-rw-r--. 1 root root 648012 Dec 4 22:18 percona-xtrabackup-debuginfo-2.2.7-5050.el6.x86_64.rpm
-rw-rw-r--. 1 root root 961432 Dec 4 22:18 percona-xtrabackup-test-2.2.7-5050.el6.x86_64.rpm[root@tong2 ~]# yum localinstall percona-xtrabackup-* -y
[root@tong2 ~]# rpm -ql percona-xtrabackup-2.2.7-5050.el6.x86_64
/usr/bin/innobackupex
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup
/usr/share/doc/percona-xtrabackup-2.2.7
/usr/share/doc/percona-xtrabackup-2.2.7/COPYING
[root@tong2 ~]#
2.创建数据库和innodb表
[root@tong1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.22 Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database tong;
Query OK, 1 row affected (0.00 sec)
mysql> \u tong
Database changed
mysql> create table t (a int);
Query OK, 0 rows affected (0.21 sec)
mysql> insert into t values(1),(2),(3),(4);
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@tong1 ~]#
3.用innobackupex工具完整备份innodb存储引擎的表
[root@tong2 ~]# innobackupex --help |more --查看帮助信息
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost /opt/all/ --备份所有数据库
[root@tong1 mysql-5.6.22]# ll /opt/all1/2015-04-24_13-12-46/
total 112680
-rw-r--r--. 1 root root 295 Apr 24 13:12 backup-my.cnf
drwxr-xr-x. 2 root root 4096 Apr 24 13:12 database
-rw-r-----. 1 root root 12582912 Apr 24 13:13 ibdata1
-rw-r--r--. 1 root root 50331648 Apr 24 13:13 ib_logfile0
-rw-r--r--. 1 root root 50331648 Apr 24 13:13 ib_logfile1
drwxr-xr-x. 2 root root 4096 Apr 24 13:12 mysql
drwxr-xr-x. 2 root root 4096 Apr 24 13:12 performance_schema
drwxr-xr-x. 2 root root 4096 Apr 24 13:12 test
drwx------. 2 root root 4096 Apr 24 13:12 tong
-rw-r--r--. 1 root root 21 Apr 24 13:12 xtrabackup_binlog_info
-rw-r--r--. 1 root root 21 Apr 24 13:13 xtrabackup_binlog_pos_innodb
-rw-r-----. 1 root root 89 Apr 24 13:13 xtrabackup_checkpoints --备份数据的检查点-rw-r--r--. 1 root root 592 Apr 24 13:12 xtrabackup_info --备份信息
-rw-r-----. 1 root root 2097152 Apr 24 13:13 xtrabackup_logfile --日志文件
4.删除数据和还原所有数据库
[root@tong1 ~]# cd /usr/local/mysql-5.6.22/
[root@tong1 mysql-5.6.22]# mysqladmin -u root -p shutdown
Enter password:
[root@tong1 mysql-5.6.22]# mv data data3
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --apply-log /opt/all1/2015-04-24_13-12-46/
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --copy-back /opt/all1/2015-04-24_13-12-46/
[root@tong1 mysql-5.6.22]# chown -R mysql:mysql data
[root@tong1 mysql-5.6.22]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[root@tong1 mysql-5.6.22]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.22 Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> exit
Bye
[root@tong1 ~]#
5.备份单个数据库
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "create table tong.t1(q int)"
Enter password:
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "flush tables"
Enter password:
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "insert into tong.t1 values(9),(8)"
Enter password:
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "select * from tong.t1"
Enter password:
+------+
| q |
+------+
| 9 |
| 8 |
+------+
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --database=tong /opt/tong/ --socket=/tmp/mysql.sock
[root@tong1 mysql-5.6.22]# ll /opt/tong/2015-04-24_14-30-00/
total 12312
-rw-r--r--. 1 root root 295 Apr 24 14:30 backup-my.cnf
-rw-r-----. 1 root root 12582912 Apr 24 14:30 ibdata1
drwx------. 2 root root 4096 Apr 24 14:30 tong
-rw-r--r--. 1 root root 21 Apr 24 14:30 xtrabackup_binlog_info
-rw-r-----. 1 root root 89 Apr 24 14:30 xtrabackup_checkpoints
-rw-r--r--. 1 root root 608 Apr 24 14:30 xtrabackup_info
-rw-r-----. 1 root root 2560 Apr 24 14:30 xtrabackup_logfile
[root@tong1 mysql-5.6.22]#
6.恢复单个数据库
[root@tong1 data]# mysqladmin -u root -p shutdown -S /tmp/mysql.sock --关闭数据库
Enter password:
[root@tong1 data]#[root@tong1 mysql-5.6.22]# mv data desdata --备份目前的数据目录
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --database=tong --apply-log /opt/tong/2015-04-24_13-31-46/
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --database=tong --copy-back /opt/tong/2015-04-24_13-31-46/
[root@tong1 mysql-5.6.22]# mv data tong --将恢复后的数据库重命名
[root@tong1 mysql-5.6.22]# mv desdata data --将以前的数据目录恢复
[root@tong1 mysql-5.6.22]# cd data
[root@tong1 data]# cp -a ../tong/tong . --将恢复后的tong数据库移动到目前的数据目录中
[root@tong1 data]# chown -R mysql:mysql tong
[root@tong1 data]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[root@tong1 data]# mysql -u root -p -e "select * from tong.t1"
Enter password:
+------+
| q |
+------+
| 9 |
| 8 |
+------+
[root@tong1 data]#
7.在完整备份中添加增量备份
[root@tong1 data]# mysql -u root -p -e "select * from tong.t"
Enter password:
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
[root@tong1 data]#
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "insert into tong.t values(6),(7)"
Enter password:
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "select * from tong.t"
Enter password:
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --incremental --incremental-basedir=/opt/all1/2015-04-24_13-12-46/ /opt/increm/ --socket=/tmp/mysql.sock
--incremental --增量备份
--incremental-basedir --完整备份文件的路径
/opt/tong --增量备份文件存放处
[root@tong1 mysql-5.6.22]# ll /opt/increm/2015-04-24_14-11-07/
total 172
-rw-r--r--. 1 root root 295 Apr 24 14:11 backup-my.cnf
drwxr-xr-x. 2 root root 4096 Apr 24 14:11 database
-rw-r-----. 1 root root 131072 Apr 24 14:11 ibdata1.delta
-rw-r-----. 1 root root 44 Apr 24 14:11 ibdata1.meta
drwxr-xr-x. 2 root root 4096 Apr 24 14:11 mysql
drwxr-xr-x. 2 root root 4096 Apr 24 14:11 performance_schema
drwxr-xr-x. 2 root root 4096 Apr 24 14:11 test
drwx------. 2 root root 4096 Apr 24 14:11 tong
-rw-r--r--. 1 root root 21 Apr 24 14:11 xtrabackup_binlog_info
-rw-r-----. 1 root root 93 Apr 24 14:11 xtrabackup_checkpoints
-rw-r--r--. 1 root root 667 Apr 24 14:11 xtrabackup_info
-rw-r-----. 1 root root 2560 Apr 24 14:11 xtrabackup_logfile
[root@tong1 mysql-5.6.22]#
8.将增量备份的数据恢复
[root@tong1 mysql-5.6.22]# mysqladmin -u root -p shutdown -S /tmp/mysql.sock
Enter password:
[root@tong1 mysql-5.6.22]# mv data srcdata --移走目录的数据文件
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --apply-log /opt/all1/2015-04-24_13-12-46/ --首先执行完全恢复
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --apply-log --redo-only /opt/all1/2015-04-24_13-12-46/ --incremental-dir=/opt/increm/2015-04-24_14-11-07/ --再执行增量恢复
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --copy-back /opt/all1/2015-04-24_13-12-46/ --最后考贝数据文件
[root@tong1 mysql-5.6.22]# chown -R mysql:mysql data --修改权限
[root@tong1 mysql-5.6.22]# /etc/init.d/mysqld restart --启动服务
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!
[root@tong1 mysql-5.6.22]# mysql -u root -p -e "select * from tong.t"
Enter password:
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 | --数据回来了
+------+
[root@tong1 mysql-5.6.22]#
9.全备份时压缩以节省资源
[root@tong1 mysql-5.6.22]# innobackupex --user=root --password= --host=localhost --stream=tar --include=tong --socket=/tmp/mysql.sock /home | bzip2 -z > /home/tong.tar.bzip2 --备份指定的数据库,压缩备份
--stream=tar --压缩备份
--include=tong --只备份tong数据库
[root@tong1 mysql-5.6.22]# ll /home/tong* -d
drwx------. 3 mysql mysql 4096 Apr 24 13:41 /home/tong
-rw-r--r--. 1 root root 11464 Apr 24 14:45 /home/tong.tar.bzip2
[root@tong1 mysql-5.6.22]#
10.解压数据文件
[root@tong1 mysql-5.6.22]# cd /home/
[root@tong1 home]# mkdir 123
[root@tong1 home]# mv tong.tar.bzip2 123/
[root@tong1 home]# cd 123/
[root@tong1 123]# tar xvfi tong.tar.bzip2 --必须加-i参数
./backup-my.cnf
ibdata1
tong/t.ibd
tong/t1.ibd
tong/t.frm
tong/t1.frm
tong/db.opt
tong12/t.frm
tong12/t1.frm
tong12/db.opt
./xtrabackup_binlog_info
xtrabackup_logfile
xtrabackup_checkpoints
./xtrabackup_info
[root@tong1 123]#