mysql备份工具之innobackupex

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

    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]#










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1562538,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
23天前
|
关系型数据库 MySQL
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
20 0
|
2月前
|
关系型数据库 MySQL 数据库
rds迁移数据迁移工具选择
rds迁移数据迁移工具选择
67 3
|
3月前
|
关系型数据库 MySQL 数据库
rds备份与恢复
rds备份与恢复
55 3
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
24 0
|
2月前
|
SQL 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
188 7
|
29天前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
82 1
|
10天前
|
SQL 存储 关系型数据库
mysql数据库备份与恢复
mysql数据库备份与恢复
|
2月前
|
关系型数据库 MySQL Linux
Linux环境下定时备份mysql数据库
Linux环境下定时备份mysql数据库
|
2月前
|
存储 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
21 7
|
2月前
|
监控 容灾 安全
规划阿里云RDS跨区迁移并构建容灾与备份策略
规划阿里云RDS(Relational Database Service)跨区迁移并构建容灾与备份策略
109 2