使用xtrabackup备份mysql数据库

时间:2022-05-26 22:42:26

数据在一个企业里非常重要,因此经常需要备份数据库,确保出线故障时,可以立刻恢复数据到最新状态,目前常见的备份工具有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)