xtrabackup 介绍:
xtrabackup有两个主要的工具:innobackupex和xtrabackup,xtrabackup只能备份InnoDB和XtraDB数据表,innobackupex封装了xtrabackup,可以备份MyISAM数据表。
第一步安装:
1.1.下载xtackbackup
地址:http://www.percona.com/downloads/XtraBackup/
1.2.安装:
tar -zxf percona-xtrabackup-2.0.8-587.tar.gz cd /tmp/soft/percona-xtrabackup-2.0.8/bin cp innobackupex /usr/bin/ cp xtrabackup_55 /usr/bin/
xtrabackup_55 取决于安装数据库的版本,此处为5.5
1.3.参数选项解释
Innobackupex 参数解释: --defaults-file=[MY.CNF]该选项传递给xtrabackup子进程,从指定文件读取缺省选项 --apply-log 从备份恢复。 --redo-only 该选项强制跳过rollback阶段,只进行redo。这是有必要使用的,如果备份后,要使用增量改变的。 --copy-back 从备份目录拷贝数据和索引文件到datadir目录 --remote-host=HOSTNAME备份到远程主机上,使用ssh --stream=[tar|cpio(notimplemented)] 指定备份标准输出格式 --tmpdir=DIRECTORY默认与tmpdir相同。使用—remote-host或—stream参数后,传输日志文件将存放在临时目录下 --use-memory=MB选项传递给xtrabackup子进程。恢复使用内存大小 --parallel=NUMBER-OF-THREADS选项传递给xtrabackup子进程,指定数据传输线程总数。默认为1 --throttle=IOS选项传递给xtrabackup子进程,限制IO线程数量 --sleep=MS 选项传递给xtrabackup子进程。每拷贝1MB数据暂停多少MS时间 --compress[=LEVEL]选项传递给xtrabackup子进程。压缩级别在0-9.1快速压缩,9最佳压缩,0不压缩。默认为1. --include=REGEXP选项传递给xtrabackup子进程。使用正则进行匹配 --databases=LIST指定备份数据库 --tables-file=FILE --uncompress选项传递给xtrabackup子进程。对压缩过的InnoDB数据文件不进行压缩 --export 仅使用于prepare选项。选项传递给xtrabackup子进程。 --user=NAME --password=WORD --host=HOST --port=PORT --slave-info 备份复制从服务端,主从信息记录在ibbackup_slave_info文件中 --socket=SOCKET --no-timestamp不在备份根目录下创建以当前时间戳为名称的新的备份目录 --ibbackup=IBBACKUP-BINARYibbackup二进制路径 --no-lock 禁止表级锁。全部是InnoDB引擎表和不关系二进制日志位置下使用 --scpopt=SCP-OPTIONS指定scp参数
第二步骤,备份及恢复测试
1.使用innobackupex全备份测试
1.1.创建库,创建表,插入数据
创建库:
root@localhost:(none) 02:25:07 >create database ckl default character set utf8; Query OK, 1 row affected (0.01 sec)
创建表
root@localhost:(none) 02:25:31 >use ckl; Database changed root@localhost:ckl 02:25:36 >create table customer_info (name varchar(11),age int,hobby varchar(11)); Query OK, 0 rows affected (0.06 sec)
插入数据:
root@localhost:ckl 02:28:54 >insert into customer_info values(‘kangge‘,‘29‘,‘read‘); Query OK, 1 row affected (0.00 sec) root@localhost:ckl 02:29:05 >insert into customer_info values(‘obama‘,‘29‘,‘golf‘); Query OK, 1 row affected (0.00 sec)
查看数据:
root@localhost:ckl 02:30:11 >select * from customer_info; +--------+------+-------+ | name | age | hobby | +--------+------+-------+ | kangge | 29 | read | | obama | 29 | golf | +--------+------+-------+ 2 rows in set (0.00 sec)
1.2.全备份
--user 指定备份用户 --password 指定备份密码 --defaults-file 指定配置文件 --include 指定需要备份的数据库,多个用‘|‘隔开,比如‘mysql|ckl‘ /data/backup/ 全备份路径 /data/dela/ 增量备份路径
执行备份命令,只备份ckl库:
innobackupex --user=root --password=ckl893 --defaults-file=/etc/my.cnf --include=‘ckl‘ --slave-info /data/backup/
以下为备份部分内容:
InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. 141013 02:38:24 innobackupex: Starting mysql with options: --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered -- 141013 02:38:24 innobackupex: Connected to database with mysql child process (pid=61476) 141013 02:38:30 innobackupex: Connection to database server closed IMPORTANT: Please check that the backup run completes successfully. At the end of a successful backup run innobackupex prints "completed OK!". innobackupex: Using mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 innobackupex: Using mysql server version Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. xtrabackup_55 version 2.0.8 for Percona Server 5.5.16 Linux (x86_64) (revision id: 587) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /data/mysql/3306/data xtrabackup: Target instance is assumed as followings. xtrabackup: innodb_data_home_dir = /data/mysql/3306/data xtrabackup: innodb_data_file_path = ibdata1:1024M:autoextend xtrabackup: innodb_log_group_home_dir = /data/mysql/3306/data xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 268435456 xtrabackup: using O_DIRECT >> log scanned up to (954367302) 141013 2:38:32 InnoDB: Warning: allocated tablespace 1547, old maximum was 9 [01] Copying /data/mysql/3306/data/ibdata1 to /data/backup/2014-10-13_02-38-30/ibdata1 >> log scanned up to (954367302) >> log scanned up to (954367302) ...... 141013 02:39:16 innobackupex: completed OK!
备份完成
2.全备份恢复
2.1.删除数据库ckl
drop database ckl;
2.2. 停止数据库
/etc/init.d/mysqld stop
2.3. prepare全备份操作:
innobackupex --user=root --password=ckl893 --defaults-file=/etc/my.cnf --apply-log --export /data/backup/2014-10-13_02-38-30/
以下为恢复部分内容:
InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. 141013 02:41:43 innobackupex: Starting ibbackup with command: xtrabackup_55 --defaults-file="/etc/my.cnf" --defaults-group="mysqld" --prepare --target-dir=/data/backup/2014-10-13_02-38-30 --export --tmpdir=/tmp xtrabackup_55 version 2.0.8 for Percona Server 5.5.16 Linux (x86_64) (revision id: 587) xtrabackup: cd to /data/backup/2014-10-13_02-38-30 xtrabackup: This target seems to be not prepared yet. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(954367302) xtrabackup: Temporary instance for recovery is set as followings. xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:1024M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 xtrabackup: Temporary instance for recovery is set as followings. .... 141013 02:42:02 innobackupex: completed OK!
完成
2.4.拷贝备份文件
\cp -rf /data/backup/2014-10-13_02-38-30/* /data/mysql/3306/data/
2.5.赋予权限:
chown -R mysql.mysql /data/mysql/3306/data/
2.6.启动数据库,查看
/etc/init.d/mysqld start
查看数据:
root@localhost:ckl 02:47:29 >select * from customer_info; +--------+------+-------+ | name | age | hobby | +--------+------+-------+ | kangge | 29 | read | | obama | 29 | golf | +--------+------+-------+ 2 rows in set (0.00 sec)
3.增量备份及恢复:
3.1.增加数据:
root@localhost:ckl 02:50:00 >insert into customer_info values(‘hook‘,‘40‘,‘drive‘); Query OK, 1 row affected (0.01 sec) root@localhost:ckl 02:50:30 >select * from customer_info; +--------+------+-------+ | name | age | hobby | +--------+------+-------+ | kangge | 29 | read | | obama | 29 | golf | | hook | 40 | drive | +--------+------+-------+
3.2. 备份全备份以来的增量备份
单个库增量备份:
Innobackupex --user=root --password=ckl893 --include=‘ckl‘ --defaults-file=/etc/my.cnf --incremental-basedir=/data/backup/2014-10-13_02-38-30/ --incremental /data/dela/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy .... 141013 02:52:19 innobackupex: completed OK!
3.3.再次增加数据:
root@localhost:ckl 02:59:38 >insert into customer_info values(‘gardner‘,‘40‘,‘run‘); Query OK, 1 row affected (0.00 sec) root@localhost:ckl 03:00:55 >select * from customer_info; +---------+------+-------+ | name | age | hobby | +---------+------+-------+ | kangge | 29 | read | | obama | 29 | golf | | hook | 40 | drive | | gardner | 40 | run | +---------+------+-------+ 4 rows in set (0.00 sec)
3.4. 备份自增量备份以来的数据:
单个库增量备份
innobackupex --user=root --password=ckl893 --include=‘ckl‘ --defaults-file=/etc/my.cnf --incremental-basedir=/data/dela/2014-10-13_02-51-52/ --incremental /data/dela/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved. ..... 141013 03:03:12 innobackupex: completed OK!
4.删除数据库,测试恢复
4.1.删除数据库:
drop database ckl;
4.2.prepare 操作:
全备份prepare:
innobackupex --user=root --password=ckl893 --defaults-file=/etc/my.cnf --apply-log --export /data/backup/2014-10-13_02-38-30/
第一次增量prepare:
innobackupex --user=root --password=ckl893 --defaults-file=/etc/my.cnf --apply-log --redo-only /data/backup/2014-10-13_02-38-30/ --incremental-dir=/data/dela/2014-10-13_02-51-52/
第二次增量prepare:
innobackupex --user=root --password=ckl893 --defaults-file=/etc/my.cnf --apply-log /data/backup/2014-10-13_02-38-30/ --incremental-dir=/data/dela/2014-10-13_03-02-45/
备份目录分析
在/data/dela/下有三个目录:
2014-10-13_02-38-30/ 全备份目录 2014-10-13_02-51-52/ 第一次增量备份目录 2014-10-13_03-02-45/ 第二次增量备份目录
4.3.拷贝备份文件
多次增量备份特别注意:
恢复的时候,如果是多次增量备份,执行两步操作
第一步:拷贝最后一次的增量备份文件
\cp -rf /data/backup/2014-10-13_03-02-45/* /data/mysql/3306/data/
第二部:拷贝全备的备份文件
\cp -rf /data/backup/2014-10-13_02-38-30/* /data/mysql/3306/data/
因为多次增量备份期间可能会有新的表创建,而全备则没有相应的表文件,所有必需要拷贝最后一次的增量备份文件
4.4.赋予权限:
chown -R mysql.mysql /data/mysql/3306/data/
4.5.启动数据库,查看
/etc/init.d/mysqld start root@localhost:ckl 03:13:19 >select * from customer_info; +---------+------+-------+ | name | age | hobby | +---------+------+-------+ | kangge | 29 | read | | obama | 29 | golf | | hook | 40 | drive | | gardner | 40 | run | +---------+------+-------+ 4 rows in set (0.00 sec)
本文出自 “运维菜鸟” 博客,请务必保留此出处http://ckl893.blog.51cto.com/8827818/1763170
原文地址:http://ckl893.blog.51cto.com/8827818/1763170