当前位置:网站首页>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
边栏推荐
- Solution to the conflict between unique index and logical deletion
- What does the &?
- Reason why wechat payment wxpaypubhelper V3 callback XML is empty
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
- [golang] golang implements the string interception function substr
- Deployment of microservices based on kubernetes platform
- 微信小程序通过点击事件传参(data-)
- Qlineedit of QT notes (74) specifies the input type
- Swift 5.0 - creation and use of swift framework
- 基金客户服务
猜你喜欢

Fastjson V2 simple user manual

Detailed explanation of conv2d -- use in arrays and images

Prospects of world digitalization and machine intelligence in the next decade

Deployment of microservices based on kubernetes platform

In 2022, the latest JCR officially released the list of the latest global impact factors (top 600)

零样本和少样本学习

Architecture of IM integrated messaging system sharing 100000 TPS

Fastjson V2 简单使用手册

5G智慧建筑解决方案2021

理想中的接口自动化项目
随机推荐
Ctfshow permission maintenance
D compile time count
shell 同时执行多任务下载视频
如何区分平台安全和网上炒作?网络投机有哪些止损技巧?
AtCoder Beginner Contest 257
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
Architecture of IM integrated messaging system sharing 100000 TPS
2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main() { ints := make
Fastjson V2 simple user manual
微信小程序中的数据双向绑定
Deployment of microservices based on kubernetes platform
Error when starting PHP: [pool www] cannot get uid for user '@php_ fpm_ [email protected]’
理想中的接口自动化项目
Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.
企业出海数字化转型解决方案介绍
Wechat applet transmits parameters (data-) by clicking events
5g smart building solution 2021
How to mention hot fix and cherry pick
Dell r720 server installation network card Broadcom 5720 driver
pytorch 的Conv2d的详细解释