数据在一个企业里非常重要,因此经常需要备份数据库,确保出线故障时,可以立刻恢复数据到最新状态,目前常见的备份工具有mysqldump和xtrabackup,数据量较少时可以使用mysqldump,但随着数据越来越大的时候,mysqldump就不适合了,因为mysqldump不支持增量或恢复的时候也会很慢,因此推荐使用percona公司的xtrabackup软件,下面介绍该软件的使用:

产品介绍:

xtrabackup是由percona提供的mysql数据库备份工具,根据官方介绍这是目前唯一一款开源的能够对innodb和xtradb数据库进行热备的工具,特点如下:

1)  备份过程快速,可靠

2)  备份过程不会打断正在执行的事务

3)  能够基于压缩等功能节约磁盘空间和流量

4)  自动实现备份检验

5)  还原速度快

备份实验:

CentOS 6 -----系统版本

xtrabackup---Percona-xtrabackup-24-2.4.4-l.el6.x86_64.rpm

mariadb-- 5.5.40-MariaDB

(编译安装mariadb5.5.40省略)

一、安装xtrabackup

1、安装前提

[root@server4 ~]# wget ftp://rpmfind.net/linux/atrpms/el6-x86_64/atrpms/stable/libev-4.04-2.el6.x86_64.rpm

[root@server4 ~]# rpm -ivh libev-4.04-2.el6.x86_64.rpm

[root@server4 ~]# yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL

2、根据系统版本下载对应的xtrabackup软件

[root@server4 ~]#wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm

[root@server4 ~]#rpm -ivh  percona-xtrabackup-24-2.4.4.1-el.x86_64.rpm

3、xtrabackup产品介绍

1)安装完xtrabackup会生成如下文件

[root@server4 data]# rpm -ql percona-xtrabackup-24
/usr/bin/innobackupex
/usr/bin/xbcloud
/usr/bin/xbcloud_osenv
/usr/bin/xbcrypt
/usr/bin/xbstream
/usr/bin/xtrabackup

2)常用参数详解

[root@server4 ~]# innobackupex --help

--apply-log

解释:一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据 文件仍处理不一致状态。--apply-log的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态。

--redo-only

解释:强制备份日志时只redo,跳过rollback,这在做增量备份时非常必要

--copy-back

解释:做数据恢复时将备份数据文件拷贝到MySQL服务器的datadir

--slave-info

解释:一般备份从库的时候会使用, 加上--slave-info备份目录下会多生成一个xtrabackup_slave_info 文件,而master_log_file和master_log_pos就是这个xtrabackup_slave_info里面的值

--incremental

解释:创建一个增量备份时需要使用该参数,必须使用--incremental-basedir指定上一次增量或全备的路径

--rsync

解释:采用rsync复制数据

--user=name   --password=password --socket=/tmp/mysql.sock --port=3306

解释:以哪个用户的身份备份

--databases=name

解释:指定要备份的数据库

--remote-host=HOSTNAME

解释:通过ssh将备份数据存储到进程服务器上

4、备份mariadb数据库(完全)

1)模拟数据生成

MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table student(SID tinyint auto_increment primary key,Sname varchar(20) not null,Sage tinyint not null,CID tinyint not null);
Query OK, 0 rows affected (0.01 sec)

MariaDB [testdb]> create table course(CID tinyint auto_increment primary key,course varchar(50) not null);
Query OK, 0 rows affected (0.00 sec)

MariaDB [testdb]> insert into course(course)values('yuwen'),('yingyu');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [testdb]> insert into student(Sname,Sage,CID)values('wenming',12,1),('liuying',27,2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [testdb]> select * from student;
+-----+---------+------+-----+
| SID | Sname | Sage | CID |
+-----+---------+------+-----+
| 1 | wenming | 12 | 1 |
| 2 | liuying | 27 | 2 |
+-----+---------+------+-----+
2 rows in set (0.00 sec)

MariaDB [testdb]> select * from course;
+-----+--------+
| CID | course |
+-----+--------+
| 1 | yuwen |
| 2 | yingyu |
+-----+--------+
2 rows in set (0.00 sec)

2)完全备份数据库

1)创建备份用户

MariaDB [(none)]> create user 'backupuser'@'localhost' identified by 'cisco123';

Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> grant reload,lock tables,replication client,process on *.* to 'backupuser'@'localhost';

Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> flush privileges

2)创建备份目录

完全备份:mkdir -p /server/backup

增量备份:mkdir -p /backup

3) 备份数据库

[root@server4 ~]# innobackupex --user=backupuser --password=cisco123 --rsync /server/backup/

IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".

161011 18:08:56 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/tmp/mysql.sock' as 'backupuser' (using password: YES).
161011 18:08:56 version_check Connected to MySQL server
161011 18:08:56 version_check Executing a version check against the server...
161011 18:08:56 version_check Done.
161011 18:08:56 Connecting to MySQL server host: localhost, user: backupuser, password: set, port: 3306, socket: /tmp/mysql.sock
Using server version 5.5.40-MariaDB-log
innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /mydata/data
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 5242880

161011 18:08:57 Executing UNLOCK TABLES
161011 18:08:57 All tables unlocked
161011 18:08:57 Backup created in directory '/server/backup/2016-10-11_18-08-55'
MySQL binlog position: filename 'mysql-bin.000003', position '1679'
161011 18:08:57 [00] Writing backup-my.cnf
161011 18:08:57 [00] ...done
161011 18:08:57 [00] Writing xtrabackup_info
161011 18:08:57 [00] ...done
xtrabackup: Transaction log of lsn (1607944) to (1607944) was copied.
161011 18:08:57 completed OK!

加入在备份的同时,数据发生了变化,使用xtrabackup第二天可以采用增量备份

MariaDB [testdb]> insert into course(course)values('shuxue'),('lishi');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [testdb]> insert into student(Sname,Sage,CID)values('binyou',32,3),('haiduo',28,4);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [testdb]> select * from student;
+-----+---------+------+-----+
| SID | Sname | Sage | CID |
+-----+---------+------+-----+
| 1 | wenming | 12 | 1 |
| 2 | liuying | 27 | 2 |
| 3 | binyou | 32 | 3 |
| 4 | haiduo | 28 | 4 |
+-----+---------+------+-----+
4 rows in set (0.00 sec)

配置增量备份

[root@server4 ~]# innobackupex --incremental /backup --user=backupuser --password=cisco123 --incremental-basedir=/server/backup/2016-10-11_18-08-55/
161011 18:13:30 innobackupex: Starting the backup operation

IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".

161011 18:13:30 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;port=3306;mysql_socket=/tmp/mysql.sock' as 'backupuser' (using password: YES).
161011 18:13:30 version_check Connected to MySQL server
161011 18:13:30 version_check Executing a version check against the server...
161011 18:13:30 version_check Done.
161011 18:13:30 Connecting to MySQL server host: localhost, user: backupuser, password: set, port: 3306, socket: /tmp/mysql.sock
Using server version 5.5.40-MariaDB-log
innobackupex version 2.4.4 based on MySQL server 5.7.13 Linux (x86_64) (revision id: df58cf2)
incremental backup from 1607944 is enabled.
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /mydata/data
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 5242880
InnoDB: Number of pools: 1

xtrabackup: The latest check point (for incremental): '1610107'
xtrabackup: Stopping log copying thread.
.161011 18:13:32 >> log scanned up to (1610107)

161011 18:13:32 Executing UNLOCK TABLES
161011 18:13:32 All tables unlocked
161011 18:13:32 Backup created in directory '/backup/2016-10-11_18-13-30'
MySQL binlog position: filename 'mysql-bin.000003', position '2183'
161011 18:13:32 [00] Writing backup-my.cnf
161011 18:13:32 [00] ...done
161011 18:13:32 [00] Writing xtrabackup_info
161011 18:13:32 [00] ...done
xtrabackup: Transaction log of lsn (1610107) to (1610107) was copied.
161011 18:13:32 completed OK!

5、模拟数据破坏,如何恢复数据

1)数据准备

[root@server4 ~]# innobackupex --apply-log --redo-only /server/backup/2016-10-11_18-08-55/ --user=backupuser --password=cisco123

[root@server4 ~]# innobackupex --apply-log --redo-only /server/backup/2016-10-11_18-08-55/ --increment-basedir=/backup/2016-10-11_18-13-30/ --user=backupuser --password=cisco123

2)数据恢复

[root@server4 ~]# rm -rf /mydata/data

[root@server4 ~]# innobackupex --copy-back /server/backup/2016-10-11_18-08-55/

[root@server4 ~]# chown -R mysql.mysql /mydata/data/*

[root@server4 data]# chown -R mysql.mysql /mydata/data/
[root@server4 data]# service mysqld start
Starting MySQL.. [ OK ]
[root@server4 data]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.40-MariaDB-log Source distribution

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)

使用xtrabackup备份mysql数据库的更多相关文章

  1. 利用xtrabackup备份mysql数据库

    利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...

  2. xtrabackup备份mysql数据库的使用方法

    xtrabackup是由percona提供的mysql备份工具,它是一款物理备份工具,通过连接数据库把数据库的数据备份出来.对于innodb存储引擎其支持全量备份和增量备份.对于myisam存储引擎只 ...

  3. 使用Xtrabackup 备份mysql数据库

    ##创建逻辑卷 [root@node1 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully create ...

  4. xtrabackup备份mysql数据库方法

    1.安装 xtrabackup 工具包 下载percona yum源 https://www.percona.com/redir/downloads/percona-release/redhat/pe ...

  5. 配置xtrabackup备份mysql数据库

    下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/ 为了方便起见本次安装使用yum源安装方式 1    安装yum源:yum insta ...

  6. Percona Xtrabackup备份mysql大数据库(完整备份与增量备份)

    Percona Xtrabackup备份mysql大数据库(完整备份与增量备份)     文章目录 [隐藏] Xtrabackup简介 Xtrabackup安装 Xtrabackup工具介绍 inno ...

  7. MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份]

    MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份] 1.Xtrabackup的安装 percona-xtrabackup-2.2.3-4982.el6.x86_ ...

  8. ​学会用各种姿势备份MySQL数据库

    学会用各种姿势备份MySQL数据库 前言 为什么需要备份数据? 数据的备份类型 MySQL备份数据的方式 备份需要考虑的问题 设计合适的备份策略 实战演练 使用cp进行备份 使用mysqldump+复 ...

  9. 批处理命令 BAT备份MySQL数据库

    批处理命令 BAT备份MySQL数据库 作者: 字体:[增加 减小] 类型:转载 时间:2009-07-23我要评论 MySQL数据的备份工具也许有很多,在这我要给大家分享一下通过DOS批处理命令和M ...

随机推荐

  1. VFP MSSOAPTOOKIT 使用SOAP Headers

    .NET 有如下使用了自定义扩展HEADER来做验证  server.asmx代码 using System;using System.Collections.Generic;using System ...

  2. GIT GUI的使用

    http://blog.csdn.net/fym0512/article/details/7713006

  3. copy(python中的引用,浅拷贝,深拷贝)

    #直接赋值 list = [1,2,['a','b'],'python'] #现将a等于list a = list print a [1,2,['a','b'],'python'] list.appe ...

  4. ueditor使用canvas在图片上传前进行压缩

    之前就看到H5使用canvas就可以在前端使用JS压缩图片,这次接到任务要把这个功能嵌入到ueditor里面去,以节省流量,减轻服务器压力. H5使用canvas进行压缩的代码有很多,核心原理就是创建 ...

  5. Java和.NET使用DES对称加密的区别

    Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...

  6. AOP (Aspect-OrientedProgramming)面向切面编程

    AOP OOP 面向对象编程 适合自上向下,却不适合自左向右 AOP把软件系统分为两个部分:核心关注点和横切关注点.业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点. 横切关注点的一个特 ...

  7. Java 零基础之作业小练习

    [练习1] 需求:输入学员的名称及总科目数并显示每项科目成绩的分数,算出总成绩. package demo2; import java.util.Scanner; //先import Scanner语 ...

  8. Android "adb devices no permissions"

    列出当前连接设备时出现以下情况 [user@dell platform-tools]# ./adb devices List of devices attached ???????????? no p ...

  9. The remote name could not be resolved问题的解决方法

    网站如果绑定了代理ip,内部跳转的时候,就会报The remote name could not be resolved错误,这个错误很难排查,网上也没有多少可参考的例子 现在记录下解决方法,以备参考 ...

  10. modelsim搭建uvm环境及实例

    Modelsim SE-64 10.2c 自带编译好的uvm-1.1d 脚本new_run.do set UVM_DPI_HOME C:/modeltech64_10.2c/uvm-1.1d/win6 ...