频道栏目
首页 > 资讯 > 工具软件 > 正文

mysql备份工具之Xtrabackup

15-04-27        来源:[db:作者]  
收藏   我要投稿

Xtrabackup是一个对InnoDB存储引擎做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品.Xtrabackup有两个主要工具:xtrabackup,innobackupex,且只能备份InnoDB和XtraDB两种存储引擎的表,而不能备份MyISAM数据表.使用innobackupex工具必须在mysql配置文件中指定数据目录,xtrabackup工具只能备份数据,不能备份表结构.
 
1.下载安装xtrabackup工具

[root@tong2 ~]# wgethttp://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 5Server version: 5.6.22 Source distributionCopyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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 tongDatabase changedmysql> 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: 0mysql> 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> exitBye[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.cnfdrwxr-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_logfile1drwxr-xr-x. 2 root root     4096 Apr 24 13:12 mysqldrwxr-xr-x. 2 root root     4096 Apr 24 13:12 performance_schemadrwxr-xr-x. 2 root root     4096 Apr 24 13:12 testdrwx------. 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 1Server version: 5.6.22 Source distributionCopyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> \u tongReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+| a    |+------+|    1 ||    2 ||    3 ||    4 |+------+4 rows in set (0.00 sec)mysql> exitBye[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 ibdata1drwx------. 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.cnfdrwxr-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.metadrwxr-xr-x. 2 root root   4096 Apr 24 14:11 mysqldrwxr-xr-x. 2 root root   4096 Apr 24 14:11 performance_schemadrwxr-xr-x. 2 root root   4096 Apr 24 14:11 testdrwx------. 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.cnfibdata1tong/t.ibdtong/t1.ibdtong/t.frmtong/t1.frmtong/db.opttong12/t.frmtong12/t1.frmtong12/db.opt./xtrabackup_binlog_infoxtrabackup_logfilextrabackup_checkpoints./xtrabackup_info[root@tong1 123]#

 

相关TAG标签
上一篇:怎样用 WPScan,Nmap 和 Nikto 扫描和检查一个 WordPress 站点的安全性
下一篇:Oracle两种临时表的创建与使用详解
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站