使用LVM备份和还原MariaDB数据库


通过和mysqldump进行对比介绍LVM快照卷
Mysqldump:无法进行热备份,数据库较大时备份时间较长,优点就是备份恢复非常的简单。
LVM优缺点如下:
优点:
1、几乎热备,只是在备份的时候为了方式产生数据不一致问题,需要锁定数据库。
2、支持所有存储引擎,因为LVM备份的原理只是拷贝文件
3、备份和恢复速度块,因为备份和恢复的原理只是拷贝文件。
缺点:
1、数据库必须放在LVM逻辑卷上
2、如果是生产环境有用户启动了事务,那么锁定数据库的过程可能需要很长的时间,而且也无法准确获得数据库停止时间
 
使用快照卷备份Mariadb
备份的前提条件:mysql的数据必须放在LVS逻辑卷之上,否则无法实现LVS备份。
 
如果使用LVS对单个数据库进行备份的话,需要使用innodb存储引擎,因为innodb存储引擎是每表一个表文件。
MariaDB[(none)]> SHOW GLOBAL VARIABLES LIKE 'innodb_file_%';
+--------------------------+----------+
|Variable_name            | Value    |
+--------------------------+----------+
|innodb_file_format      | Antelope |
|innodb_file_format_check | ON      |
|innodb_file_format_max  | Antelope |
|innodb_file_per_table    | ON      |
+--------------------------+----------+

备份六步走:
1、请求锁定所有表:
主要是为了防止在备份过程中有用户修改数据,造成备份和原数据的不一致性,需要在备份之前锁定表,防止用户修改,备份完成之后在解锁。
MariaDB[(none)]> FLUSH TABLES WITH READ LOCK;

说明:如果是生产环境有用户启动了事务,那么锁定数据库的过程可能需要很长的时间。
2、记录二进制日志文件及事件位置:
备份完成之后滚动日志,然后记录日志文件的位置,
MariaDB [(none)]> flush logs;
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      245 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

3、创建快照:
创建快照卷,这里使用的是额外启动的终端,因为数据库一旦退出,锁就释放了。
[root@MariaDB ~]# lvcreate -L 200M -n mydata-snap-s -p r /dev/myvg/mydata

4、释放锁:
MariaDB[(none)]> UNLOCK TABLES;

5、挂载快照卷,复制数据进行备份;
挂载快照卷
[root@MariaDB ~]# mkdir /snap
[root@MariaDB ~]# mount /dev/myvg/mydata-snap/snap/
[root@Mariadb ~]# ls /snap/data/
aria_log.00000001 ibdata1      Mariadb.err  mysql-bin.000001  performance_schema
aria_log_control  ib_logfile0 Mariadb.pid  mysql-bin.000002  test
hellodb          ib_logfile1  mysql        mysql-bin.index

复制数据进行备份
只是备份单个库使用
[root@MariaDB ~]# rsync -a /snap/data/hellodb//backup/hellodb-`date +%F-%H-%M-%S`
[root@Mariadb ~]# ls /backup/hellodb-2015-05-28-02-57-12/hellodb/
classes.frm coc.MYD      courses.MYI  scores.MYI  tb1.frm      toc.frm
classes.MYD coc.MYI      db.opt      students.frm  teachers.frm toc.MYD
classes.MYI courses.frm  scores.frm  students.MYD teachers.MYD  toc.MYI
coc.frm    courses.MYD  scores.MYD  students.MYI teachers.MYI

备份整个库
12345 [root@MariaDB ~]# rsync -a /snap/data/*/backup/mariadb-all-`date +%F-%H-%M-%S`
[root@Mariadb ~]# ls/backup/mariadb-all-2015-05-28-02-57-51/
aria_log.00000001 ibdata1      Mariadb.err  mysql-bin.000001  performance_schema
aria_log_control  ib_logfile0  Mariadb.pid  mysql-bin.000002  test
hellodb          ib_logfile1  mysql        mysql-bin.index

6、备份完成之后,删除快照卷;
[root@Mariadb ~]# umount /snap/
[root@Mariadb ~]# lvremove /dev/myvg/mydata-snap
Do you really want to remove active logical volumemydata-snap? [y/n]: y

模拟误删除
备份完成之后修改了一些数据
MariaDB [hellodb]> drop table tb1;
MariaDB [hellodb]> create table tb2 (id int);
MariaDB [hellodb]> insert into tb2 values(1),(2),(3);
MariaDB [hellodb]> select * from tb2;
+------+
| id  |
+------+
|    1 |
|    2 |
|    3 |
+------+

3 rows in set (0.00 sec)
修改完成之后把hellodb数据库误删除了
MariaDB [hellodb]> drop database hellodb;

然后还把整个数据库误删除了(说明:如果是生产环境二进制日志文件和数据文件一定是分开存放的,但是实验安装时如果是二进制日志文件和数据目录在一起,应该先复制一份二进制日志文件到备份目录)
[root@MariaDB ~]# rm -rf/mydata/data/*

数据库故障恢复
Mysql出现大故障时,最好停掉数据库,如果无法停止就杀死进程
[root@MariaDB ~]# service mysqld stop
MySQL server PID file could not be found!                  [FAILED]
[root@MariaDB ~]# killall mysqld
mysqld: no process killed

从二进制日志文件中导出所有和hellodb数据库相关的操作。
导出之前需要去掉误删除语句,查看语句在二进制日志文件中的记录位置
MariaDB [(none)]> show binlog events in'mysql-bin.000002'\G;
*************************** 7. row***************************
  Log_name:mysql-bin.000003
        Pos:642
 Event_type:Query
  Server_id:1
End_log_pos: 729
      Info:drop database hellodb
7 rows in set (0.00 sec)

得到语句记录位置之后,不导出记录删除位置
[root@Mariadb ~]# mysqlbinlog --stop-position=729/mydata/data/mysql-bin.000002 > /backup/lvm.sql

复制所有备份的文件和目录到数据目录下
[root@MariaDB ~]# cp -a /backup/mariadb-all-2015-05-28-02-57-51/*/mydata/data/
[root@MariaDB ~]# ll /mydata/data/
total 28764
-rw-rw---- 1 mysql mysql    16384 May 27 22:30 aria_log.00000001
-rw-rw---- 1 mysql mysql      52 May 27 22:30 aria_log_control
drwx------ 2 mysql mysql    4096 May 28 00:49 hellodb
-rw-rw---- 1 mysql mysql 18874368 May 28 00:49ibdata1
-rw-rw---- 1 mysql mysql  5242880 May 28 00:49 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 May 27 22:34 ib_logfile1
-rw-r----- 1 mysql root      5674 May 28 00:43 MariaDB.err
-rw-rw---- 1 mysql mysql        5May 28 00:43 MariaDB.pid
drwx------ 2 mysql root      4096 May 27 22:30 mysql
-rw-rw---- 1 mysql mysql      632 May 27 23:12 mysql-bin.000001
-rw-rw---- 1 mysql mysql    8667 May 27 23:22 mysql-bin.000002
-rw-rw---- 1 mysql mysql      114 May 28 00:50 mysql-bin.index
drwx------ 2 mysql mysql    4096 May 27 22:30 performance_schema
drwx------ 2 mysql root      4096 May 27 22:30 test

这个时候启动mysqld服务就可以看到备份之前的数据了
[root@MariaDB ~]# mysql -u root -p
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes          |
| coc              |
| courses          |
| scores          |
| students        |
| tb1              |
| teachers        |
| toc              |
+-------------------+
MariaDB [hellodb]> select * from tb1;
+------+
| id  |
+------+
|    1 |
|    2 |
|    3 |
|  21 |
|  22 |
|  23 |
+------+
6 rows in set (0.01 sec)

恢复的数据只是备份之前的数据,备份之后更改的数据还没有回了,这个时候就需要导入二进制日志转换成的sql文件了
[root@MariaDB ~]# mysql -u root -p </backup/lvm.sql

查看数据就可以看到tb2了
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes          |
| coc              |
| courses          |
| scores          |
| students        |
| tb2              |
| teachers        |
| toc              |
+-------------------+
MariaDB [hellodb]> select * from tb2;
+------+
| id  |
+------+
|    1 |
|    2 |
|    3 |
+------+

在 CentOS/RHEL/Scientific Linux 6 下安装 LAMP (Apache with MariaDB and PHP)

MariaDB Proxy读写分离的实现

Linux下编译安装配置MariaDB数据库的方法

CentOS系统使用yum安装MariaDB数据库

安装MariaDB与MySQL并存

Ubuntu 上如何将 MySQL 5.5 数据库迁移到 MariaDB 10 

[翻译]Ubuntu 14.04 (Trusty) Server 安装 MariaDB 

相关内容