设为首页 收藏本站
查看: 361|回复: 0

[经验分享] mysql备份工具之innobackupex

[复制链接]

尚未签到

发表于 2018-10-1 07:58:07 | 显示全部楼层 |阅读模式
  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>  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>  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]#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-606917-1-1.html 上篇帖子: 《Nginx1.13+MySQL 5.7+PHP7.2 优化》 下篇帖子: mysql数据库理论与实战
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表