缘来路过 发表于 2018-10-1 07:58:07

mysql备份工具之innobackupex

  Xtrabackup是一个对InnoDB存储引擎做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品.Xtrabackup有两个主要工具:xtrabackup,innobackupex,且只能备份InnoDB和XtraDB两种存储引擎的表,而不能备份MyISAM数据表.使用innobackupex工具必须在mysql配置文件中指定数据目录,xtrabackup工具只能备份数据,不能备份表结构.
  mysql dba技术群 378190849
  武汉-linux运维群 236415619
  1.下载安装xtrabackup工具
  # 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
  # tar xvf Percona-XtraBackup-2.2.7-r5050-el6-x86_64-bundle.tar
  # ll percona-xtrabackup-*
  -rw-rw-r--. 1 root root 4863276 Dec4 22:18 percona-xtrabackup-2.2.7-5050.el6.x86_64.rpm
  -rw-rw-r--. 1 root root648012 Dec4 22:18 percona-xtrabackup-debuginfo-2.2.7-5050.el6.x86_64.rpm
  -rw-rw-r--. 1 root root961432 Dec4 22:18 percona-xtrabackup-test-2.2.7-5050.el6.x86_64.rpm
  # yum localinstall percona-xtrabackup-*-y
  # 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
  #
  2.创建数据库和innodb表
  # 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: 4Duplicates: 0Warnings: 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
  #
  3.用innobackupex工具完整备份innodb存储引擎的表
  # innobackupex --help |more   --查看帮助信息
  # innobackupex --user=root --password= --host=localhost/opt/all/    --备份所有数据库
  # 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 root2097152 Apr 24 13:13 xtrabackup_logfile      --日志文件
  4.删除数据和还原所有数据库
  # cd /usr/local/mysql-5.6.22/
  # mysqladmin -u root -p shutdown
  Enter password:
  # mv data data3
  # innobackupex --user=root --password= --host=localhost --apply-log   /opt/all1/2015-04-24_13-12-46/
  # innobackupex --user=root --password= --host=localhost --copy-back   /opt/all1/2015-04-24_13-12-46/
  # chown-R mysql:mysql data
  # /etc/init.d/mysqld restart
  ERROR! MySQL server PID file could not be found!
  Starting MySQL. SUCCESS!
  # 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
  #
  5.备份单个数据库
  # mysql-u root -p-e "create table tong.t1(q int)"
  Enter password:
  # mysql-u root -p-e "flush tables"
  Enter password:
  # mysql-u root -p-e "insert into tong.t1 values(9),(8)"
  Enter password:
  # mysql-u root -p-e "select * from tong.t1"
  Enter password:
  +------+
  | q    |
  +------+
  |    9 |
  |    8 |
  +------+
  # innobackupex --user=root --password= --host=localhost --database=tong/opt/tong/ --socket=/tmp/mysql.sock
  # 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
  #
  6.恢复单个数据库
  # mysqladmin -u root -p shutdown -S /tmp/mysql.sock   --关闭数据库
  Enter password:
  ## mv data desdata       --备份目前的数据目录
  # innobackupex --user=root --password= --host=localhost --database=tong --apply-log /opt/tong/2015-04-24_13-31-46/
  # innobackupex --user=root --password= --host=localhost --database=tong --copy-back /opt/tong/2015-04-24_13-31-46/
  # mv data tong      --将恢复后的数据库重命名
  # mv desdata data   --将以前的数据目录恢复
  # cd data
  # cp -a ../tong/tong.   --将恢复后的tong数据库移动到目前的数据目录中
  # chown -R mysql:mysql tong
  # /etc/init.d/mysqldrestart
  ERROR! MySQL server PID file could not be found!
  Starting MySQL. SUCCESS!
  # mysql -u root -p -e "select * from tong.t1"
  Enter password:
  +------+
  | q    |
  +------+
  |    9 |
  |    8 |
  +------+
  #
  7.在完整备份中添加增量备份
  # mysql -u root -p -e "select * from tong.t"
  Enter password:
  +------+
  | a    |
  +------+
  |    1 |
  |    2 |
  |    3 |
  |    4 |
  |    5 |
  +------+
  #
  # mysql-u root -p-e "insert into tong.t values(6),(7)"
  Enter password:
  # mysql-u root -p-e "select * from tong.t"
  Enter password:
  +------+
  | a    |
  +------+
  |    1 |
  |    2 |
  |    3 |
  |    4 |
  |    5 |
  |    6 |
  |    7 |
  +------+
  # 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                --增量备份文件存放处
  # 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
  #
  8.将增量备份的数据恢复
  # mysqladmin -u root -p shutdown -S /tmp/mysql.sock
  Enter password:
  # mv datasrcdata   --移走目录的数据文件
  # innobackupex --user=root --password= --host=localhost --apply-log/opt/all1/2015-04-24_13-12-46/       --首先执行完全恢复
  # 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/             --再执行增量恢复
  # innobackupex --user=root --password= --host=localhost --copy-back/opt/all1/2015-04-24_13-12-46/   --最后考贝数据文件
  # chown-R mysql:mysql data    --修改权限
  # /etc/init.d/mysqld restart    --启动服务
  ERROR! MySQL server PID file could not be found!
  Starting MySQL.. SUCCESS!
  # mysql-u root -p-e "select * from tong.t"
  Enter password:
  +------+
  | a    |
  +------+
  |    1 |
  |    2 |
  |    3 |
  |    4 |
  |    5 |
  |    6 |
  |    7 |                  --数据回来了
  +------+
  #
  9.全备份时压缩以节省资源
  # 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数据库
  # ll /home/tong* -d
  drwx------. 3 mysql mysql4096 Apr 24 13:41 /home/tong
  -rw-r--r--. 1 rootroot11464 Apr 24 14:45 /home/tong.tar.bzip2
  #
  10.解压数据文件
  # cd /home/
  # mkdir 123
  # mv tong.tar.bzip2 123/
  # cd 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
  #

页: [1]
查看完整版本: mysql备份工具之innobackupex