标签:
一、XtraBackup安装
下载地址:http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.8/source/
安装步骤:
================================ How to build XtraBackup on Linux ================================ Prerequisites -------------
$ yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses5-devel Compiling with CMake -------------------- $ cmake -DBUILD_CONFIG=xtrabackup_release && make -j4
Installation ------------ $ make install
will install all XtraBackup binaries, the innobackupex script and tests to /usr/local/xtrabackup. You can override this either with “make DESTDIR=... install” or by changing the installation layout with “cmake -DINSTALL_LAYOUT=...”.
如果出现下面报错,需要 yum install crypt* ; yum install libgcrypt*
CMake Error at cmake/gcrypt.cmake:25 (MESSAGE): Cannot find gcrypt.h in /usr/include;/usr/local/include;/opt/local/include. You can use libgcrypt-config --cflags to get the necessary path and pass it to CMake with -DGCRYPT_INCLUDE_PATH=<path> Call Stack (most recent call first): storage/innobase/xtrabackup/src/CMakeLists.txt:20 (FIND_GCRYPT) CMake Error at cmake/gcrypt.cmake:36 (MESSAGE): Cannot find libgcrypt shared libraries in /usr/lib;/usr/local/lib;/opt/local/lib. You can use libgcrypt-config --libs to get the necessary path and pass it to CMake with -DGCRYPT_LIB_PATH=<path> Call Stack (most recent call first): storage/innobase/xtrabackup/src/CMakeLists.txt:20 (FIND_GCRYPT)
二、xtrabackup全备测试
普通备份(全量备份)
[root@server mysql]# mkdir /data/mysql/databak
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --target-dir=/data/mysql/databak/
[root@server mysql]# cp -rp data/test/students.frm databak/test/
注意:xtrabackup只备份数据文件,并不备份数据表结构(.frm),所以这里要手动备份一下,以便xtrabackup恢复的时候使用,如果是做了分表分文件保存的话,就需要挑出.frm表结构,表数据文件.ibd不需要复制
全量备份恢复
实施对备份文件进行恢复前的准备
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/
从备份目录复制对应数据库表结构到默认的数据目录[如果是做了分表分文件,则需要全部复制到默认的数据目录]
cp -r /data/mysql/databak/test /data/mysql/data/
删除默认数据目录中对应的数据文件并复制备份的数据文件到默认数据目录
rm /data/mysql/data/ib*
cp /data/mysql/databak/ib* /data/mysql/data/
修改数据目录权限[注意细节:mysql库的所有者是mysql,组是root]
chown -R mysql:mysql /data/mysql/data
重启MySQL
[root@server data]# /etc/init.d/mysqld restart
三、xtrabackup增量备份测试
1. 全备
[root@server databak]# mkdir /data/mysql/databak/
[root@server mysql]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --target-dir=/data/mysql/databak/
[root@server mysql]# cp -rp data/test/students.frm databak/test/
2. 增量备份
[root@server databak]# mkdir /data/mysql/databak/delta/
[root@server databak]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --backup --incremental-basedir=/data/mysql/databak/ --target-dir=/data/mysql/databak/delta/
3. 备份恢复准备
[root@server data]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/
[root@server data]# xtrabackup --defaults-file=/usr/local/mysql/my.cnf --prepare --target-dir=/data/mysql/databak/ --incremental-dir/=/data/mysql/databak/delta/
4. 恢复数据库
从备份目录复制对应数据库表结构到默认的数据目录[如果是做了分表分文件,则需要全部复制到默认的数据目录]
cp -r /data/mysql/databak/test /data/mysql/data/
删除默认数据目录中对应的数据文件并复制备份的数据文件到默认数据目录
rm /data/mysql/data/ib*
cp /data/mysql/databak/ib* /data/mysql/data/
修改数据目录权限[注意细节:mysql库的所有者是mysql,组是root]
chown -R mysql:mysql /data/mysql/data
四、用innobackupex 执行全备与增量备份
innobackupex全备和增量备份
1. 全备
[root@server mysql]# innobackupex --user=root --password=redhat /data/mysql/databak/
innobackupex: got a fatal error with the following stacktrace: at /usr/bin/innobackupex line 3637
main::init() called at /usr/bin/innobackupex line 1557
innobackupex: Error: option ‘datadir‘ has different values:
‘/var/lib/mysql‘ in defaults file
‘/data/mysql/data/‘ in SHOW VARIABLES
innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=redhat /data/mysql/databak/
2. 第一次增量备份
#--incremental:增量备份的文件夹
#--incremental-dir:针对哪个做增量备份
[root@server mysql]# innobackupex --defaults-file=/usr/local/mysql/my.cnf --user=root --password=redhat --incremental /data/mysql/databak/ --incremental-dir /data/mysql/databak/2015-02-04_19-00-03/
3. 模拟数据丢失
mysql> drop database test;
Query OK, 1 row affected (0.14 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
4. 将全备数据执行prepare
innobackupex --apply-log --redo-only /data/mysql/databak/2015-02-04_19-00-03/
5. 将增量备份数据合并到全备中
[root@serv01 databackup]# innobackupex --apply-log --redo-only --incremental /data/mysql/databak/2015-02-04_19-00-03/ --incremental-dir=/data/mysql/databak/2015-02-04_19-03-58/
6. 停止mysqld服务,恢复数据
/etc/init.d/mysql stop
[root@serv01 databackup]# innobackupex --defaults-file=/usr/local/mysql/my.cnf --copy-back /data/mysql/databak/2015-02-04_19-00-03/
7. 对恢复的文件修改权限
chown -R mysql:mysql /data/mysql/data/*
chown -R mysql:root /data/mysql/data/mysql
8. 启动mysql服务
/etc/init.d/mysqld start
标签:
原文地址:http://www.cnblogs.com/forilen/p/4273133.html