使用xtrabackup备份mysql数据库
数据在一个企业里非常重要,因此经常需要备份数据库,确保出线故障时,可以立刻恢复数据到最新状态,目前常见的备份工具有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数据库的更多相关文章
- 利用xtrabackup备份mysql数据库
利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...
- xtrabackup备份mysql数据库的使用方法
xtrabackup是由percona提供的mysql备份工具,它是一款物理备份工具,通过连接数据库把数据库的数据备份出来.对于innodb存储引擎其支持全量备份和增量备份.对于myisam存储引擎只 ...
- 使用Xtrabackup 备份mysql数据库
##创建逻辑卷 [root@node1 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully create ...
- xtrabackup备份mysql数据库方法
1.安装 xtrabackup 工具包 下载percona yum源 https://www.percona.com/redir/downloads/percona-release/redhat/pe ...
- 配置xtrabackup备份mysql数据库
下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/ 为了方便起见本次安装使用yum源安装方式 1 安装yum源:yum insta ...
- Percona Xtrabackup备份mysql大数据库(完整备份与增量备份)
Percona Xtrabackup备份mysql大数据库(完整备份与增量备份) 文章目录 [隐藏] Xtrabackup简介 Xtrabackup安装 Xtrabackup工具介绍 inno ...
- MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份]
MariaDB之基于Percona Xtrabackup备份大数据库[完整备份与增量备份] 1.Xtrabackup的安装 percona-xtrabackup-2.2.3-4982.el6.x86_ ...
- 学会用各种姿势备份MySQL数据库
学会用各种姿势备份MySQL数据库 前言 为什么需要备份数据? 数据的备份类型 MySQL备份数据的方式 备份需要考虑的问题 设计合适的备份策略 实战演练 使用cp进行备份 使用mysqldump+复 ...
- 批处理命令 BAT备份MySQL数据库
批处理命令 BAT备份MySQL数据库 作者: 字体:[增加 减小] 类型:转载 时间:2009-07-23我要评论 MySQL数据的备份工具也许有很多,在这我要给大家分享一下通过DOS批处理命令和M ...
随机推荐
- Windows 下针对python脚本做一个简单的进程保护
前提: 大家运行的脚本程序经常会碰到系统异常关闭.或被其他用户错杀的情况.这样就需要一个进程保护的工具. 本文结合windows 的计划任务,实现一个简单的进程保护的功能. 利用py2exe生产 ex ...
- myeclipse ctrl + 鼠标单击 出现 source not found
有时候我们下载来目录中有一个src文件夹,里面是源代码,而不是打包好的jar或zip文件.src目录下的源代码是按照包结构存放的,比如a.java的第一行是package test; 那么在src/t ...
- ios AFNetworking 3.0 报错 : *** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:],
AFNetWorking[:] *** Assertion failure -- :::] *** Terminating app due to uncaught exception 'NSInter ...
- Office 365系列六 ------ 创建sharepoint online网站
这节跟大家介绍简单的创建sharep online私有网站集,sharepoint online 可以给我们提供开箱即用的功能,比如文档库:可以给我们取代File Server,提供了版本管理,版本变 ...
- ViewPager+Fragment实现页面的切换
新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding=&q ...
- DirectX12 Samples 学习笔记 – PredicationQueries
一.效果 这是一个比较简单的sample,运行sample可以看到,当红橙色长方形完全覆盖白色正方形时,白色正方形不显示,其他情况,均显示白色正方形. 二.实现 Render主要由三个部分组成 1.F ...
- Appium移动自动化测试之Eclipse
下载eclipse,这个下载方式比较多,eclipse官网,CSDN都有的下,版本根据自己操作系统选择,切记eclipse版本一定要与JDK版本一至,不然eclipse无法启动.现在我们来搭建Andr ...
- Ajax 知识点
AJAX 即"Asynchronous Javascript And XML"(异步JavaScript和XML) Ajax 不是某种编程语言,只是一种在无需重新加载整个网页的情况 ...
- 关于Servlet手动配置web.xml部分代码
<servlet> <!-- 文件名 --> <servlet-name>deleteServlet</servlet-name> <!-- 文件 ...
- Java的二维数组的应用及杨辉三角的编写
(1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...