当前位置:网站首页>lvm-snapshot:基于LVM快照的备份
lvm-snapshot:基于LVM快照的备份
2022-06-30 23:19:00 【星哥玩云】
续 lvm-snapshot:基于LVM快照的备份之准备工作 http://www.linuxidc.com/Linux/2014-05/101308.htm
Attention,Please! 重头戏开始啦
3.基于LVM快照的备份
(1)事务日志跟数据文件必须在同一个卷上;
(2)创建快照卷之前,要请求MySQL的全局锁;在快照创建完成之后释放锁;
(3)请求全局锁完成之后,做一次日志滚动;做二进制日志文件及位置标记(手动进行);
--------------------------------------分割线 --------------------------------------
MySQL管理之基于LVM实现几乎热备 http://www.linuxidc.com/Linux/2014-04/99672.htm
Ubuntu 12.04 KVM之VM动态迁移-基于LVM http://www.linuxidc.com/Linux/2014-04/99894.htm
RHEL5.9 LVM的使用 http://www.linuxidc.com/Linux/2014-02/97268.htm
Linux系统中对逻辑卷(LVM)的实现 http://www.linuxidc.com/Linux/2014-01/95004.htm
LVM磁盘管理之扩展与缩小LV http://www.linuxidc.com/Linux/2013-03/81262.htm
--------------------------------------分割线 --------------------------------------
备份步骤:
3.1 请求全局锁,并滚动日志
MariaDB [hellodb]> FLUSH TABLES WITH READ LOCK;
MariaDB [hellodb]> FLUSH LOGS;
3.2 查看并记录二进制日志文件及位置标记(手动进行);
MariaDB [hellodb]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 365 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
此时不要退出Mysql,一旦退出,就会我们手动施加的锁,就会自动释放
记录二进制日志文件及位置标记至指定文件
# mkdir /backups
# mysql -e 'show master status' >/backups/binlog.pos
3.3 创建快照卷(-L,指定快照卷大小;-n,指定快照卷名称;-p,指定快照卷的属性)
# lvcreate -L 100M -s -n mydata-snap -p r /dev/myvg/mydata
查看lvm卷使用使用情况
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mydata myvg owi-aos-- 10.00g
mydata-snap myvg sri-a-s-- 100.00m mydata 0.01
root vg0 -wi-ao--- 20.00g
swap vg0 -wi-ao--- 2.00g
usr vg0 -wi-ao--- 10.00g
var vg0 -wi-ao--- 20.00g
3.4 释放全局锁
MariaDB [hellodb]> UNLOCK TABLES;
注:快照创建完毕,可在之前尚未退出MariaDB的终端执行解锁操作
3.5 挂载快照卷并备份
(1)将快照卷以只读方式挂载至/mnt目录
[[email protected] ~]# mount -o ro /dev/myvg/mydata-snap /mnt/
(2)查看快照卷挂载是否成功(即查看挂载点下是否有我们之前备份的数据)
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# ls
binlogs data
[[email protected] mnt]# cd data/
[[email protected] data]# ls
aria_log.00000001 ibdata1 multi-master.info test
aria_log_control ib_logfile0 mysql www.linuxidc.com.err
hellodb ib_logfile1 performance_schema www.linuxidc.com.pid
细心的你应该注意到了,前面我们已经释放了全局锁,所以其他的用户是可以执行写入的操作了,因此
此处我们模拟的是,在我们完整备份以后,又有用户执行了写入操作实验环境,然而不幸的是,我们在此处导入数据没多久,就反了些很"2"的错误:一不小心关掉了数据库服务,并且误删除了数据/mydata/data/下的所有数据
3.6 导入新数据,模拟用户的写操作
MariaDB [mydb]> source /root/mydb.sql;
3.7 备份快照中的数据至指定位置
# cp -a /mnt/data/ /backups/data-$(date +%F)
3.8 备份完成之后,删除快照卷
# umount /mnt/ 卸载快照卷
# lvremove /dev/myvg/mydata-snap 移除快照卷
恢复:二进制日志一定要保存好,否则很难做到即时点还原
模拟数据库损坏
4.不小心停掉了数据库,并且删除了数据目录下的所有文件
[[email protected] ~]# service mysqld stop
Shutting down MySQL... [ OK ]
[[email protected] ~]# rm -rf /mydata/data/*
还好二进制日志尚在,不然没法做即时点还原(二进制日志文件很重要,要常备份)
[[email protected] ~]# ls /mydata/binlogs/
mysql-bin.000001 mysql-bin.000003 mysql-bin.000005 mysql-bin.state
mysql-bin.000002 mysql-bin.000004 mysql-bin.index
数据目录中的数据被误删除后,数据目录空空如也
[[email protected] ~]# ls /mydata/data/
还原数据目录下的所有数据,-a保持用户属主属组等属性信息不变
[[email protected] ~]# cp -a /backups/data-2014-04-12/* /mydata/data/
[[email protected] ~]# cd /mydata/data/
查看数据目录下的数据的属主属组是否都是mysql,若不是,就自行修改
[[email protected] data]# ll
total 110636
-rw-rw---- 1 mysql mysql 16384 Apr 12 22:33 aria_log.00000001
-rw-rw---- 1 mysql mysql 52 Apr 12 22:33 aria_log_control
drwx------ 2 mysql mysql 4096 Apr 12 22:38 hellodb
-rw-rw---- 1 mysql mysql 12582912 Apr 12 22:34 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Apr 12 22:34 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Apr 12 22:32 ib_logfile1
-rw-rw---- 1 mysql mysql 0 Apr 12 22:34 multi-master.info
drwx------ 2 mysql mysql 4096 Apr 12 22:33 mysql
drwx------ 2 mysql mysql 4096 Apr 12 22:33 performance_schema
drwx------ 2 mysql mysql 4096 Apr 12 22:25 test
-rw-r----- 1 mysql root 3660 Apr 12 22:34 www.linuxidc.com.err
-rw-rw---- 1 mysql mysql 5 Apr 12 22:34 www.linuxidc.com.pid
确认数据目录权限正确的情况下,就可以启动mysql了
[[email protected] data]# service mysqld start
在数据目录下查看数据文件是否都已恢复
[[email protected] data]# ls
aria_log.00000001 ibdata1 multi-master.info test
aria_log_control ib_logfile0 mysql www.linuxidc.com.err
hellodb ib_logfile1 performance_schema www.linuxidc.com.pid
在数据中查看数据文件是否都已恢复
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
此时,完整备份已成功恢复,但是我们模拟用户写操作时倒入的mydb.sql文件,生成的mydb数据库,及其中数据表还没恢复
由于该写入操作出现在我们完整备份之后,所以我们的完整备份中没有这个数据库和其中的数据,所以此时我们只能借助于二进制日志文件了。我们上次完整备份时的二进制日志文件是mysql-bin.000005,在我们做了完整备份恢复之后,重启了sql服务,
于是生成了mysql-bin.000006二进制日志文件。
查看二进制日志文件
[[email protected] data]# ls /mydata/binlogs/
mysql-bin.000001 mysql-bin.000003 mysql-bin.000005 mysql-bin.index
mysql-bin.000002 mysql-bin.000004 mysql-bin.000006
查看我们完整备份之前保存的二进制日志mysql-bin.000005的信息
[[email protected] data]# cat /backups/binlog.pos
FilePositionBinlog_Do_DBBinlog_Ignore_DB
mysql-bin.000005365
查看二进制日志文件mysql-bin.000005的具体内容
# mysqlbinlog --start-position=365 /mydata/binlogs/mysql-bin.000005
# at 365
#140412 22:55:48 server id 1 end_log_pos 403GTID 0-1-2176
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=2176*//*!*/;
......省略一些不甚重要的信息
# at 4857
#140412 22:55:51 server id 1 end_log_pos 4895GTID 0-1-2200
/*!100001 SET @@session.gtid_seq_no=2200*//*!*/;
# at 4895
#140412 22:55:51 server id 1 end_log_pos 5009Querythread_id=4exec_time=0error_code=0
SET TIMESTAMP=1397314551/*!*/;
DROP TABLE IF EXISTS `t5` /* generated by server */
/*!*/;
# at 5009
#140412 22:55:51 server id 1 end_log_pos 5047GTID 0-1-2201
/*!100001 SET @@session.gtid_seq_no=2201*//*!*/;
# at 5047
#140412 22:55:51 server id 1 end_log_pos 5245Querythread_id=4exec_time=0error_code=0
SET TIMESTAMP=1397314551/*!*/;
CREATE TABLE `t5` (
`ID` int(10) unsigned NOT NULL DEFAULT '0',
`Name` char(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!*/;
# at 5245
#140412 22:55:51 server id 1 end_log_pos 5283GTID 0-1-2202
借助二进制日志文件实现增量备份的恢复
现在我们需要将这些二进制日志信息导入到mysql服务器
法1:
# mysqlbinlog --start-position=365 /mydata/binlogs/mysql-bin.000005 >/tmp/incr.sql
# mysql </tmp/incr.sql 或 mysql> source /tmp/incr.sql
法2:
# mysqlbinlog --start-position=365 /mydata/binlogs/mysql-bin.000005 | mysql
在数据库查看mydb数据库是否已经恢复了
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.01 sec)
我们可以看到mydb数据库已成功恢复。
注:当数据库灾难恢复之后,我们要做的第一件事就是对当前数据库做一次完整备份,以备不时之需,有备无患
mylvbackup: perl脚本,快速基于lvm备份mysql
边栏推荐
- Two dots on the top of the latex letter
- 什么是SRM系统,如何规范公司内部采购流程
- 后疫情时代,云计算如何为在线教育保驾护航
- Maxpool2d explanation -- Application in arrays and images
- 76 page comprehensive solution 2022 for smart Logistics Park (download attached)
- Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
- 图纸加密如何保障我们的核心图纸安全
- Jmeter跨线程参数关联无需脚本
- shell 同时执行多任务下载视频
- Fund managers' corporate governance and risk management
猜你喜欢
![CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构](/img/ce/519778cd731f814ad111d1e37abd10.png)
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构

206页上海BIM技术应用与发展报告2021

76页智慧物流园区综合解决方案2022(附下载)

Braces on the left of latex braces in latex multiline formula

"Paddle + camera" has become a "prefabricated dish" in the AI world, and it is easier to implement industrial AI quality inspection

Architecture of IM integrated messaging system sharing 100000 TPS

New trends of China's national tide development in 2022

Fastjson V2 simple user manual

MaxPool2d详解--在数组和图像中的应用

项目管理到底管的是什么?
随机推荐
The superficial understanding of the industrial Internet finally brought the development of the industrial Internet into the strange circle of the consumer Internet
[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
Ctfshow framework reproduction
HP notebook disable touchpad after mouse is inserted
Dataloader source code_ DataLoader
Meet the streamnational | yangzike: what made me give up Dachang offer
KubeVela 1.4:让应用交付更安全、上手更简单、过程更透明
[golang] golang implements the string interception function substr
PS2 handle-1 "recommended collection"
零样本和少样本学习
76 page comprehensive solution 2022 for smart Logistics Park (download attached)
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
基金管理人公司治理和风险管理
"Paddle + camera" has become a "prefabricated dish" in the AI world, and it is easier to implement industrial AI quality inspection
How to use robots Txt and its detailed explanation
在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
JMeter cross thread parameter association requires no script
How cloud computing can protect online education in the post epidemic Era
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
Error when starting PHP: [pool www] cannot get uid for user '@php_ fpm_ [email protected]’