一,
LVM快照备份的意义
数据库备份一直是数据库运维工作中的重点,一个完备的备份不仅仅是仅有后悔药的功能,还可能有迁移数据库的作用。
那么,数据库备份系统我们需要的,也就是看重的是四个点,甚至更多的点
第一个是备份的范围准确,例如,物理备份数据库文件,但数据库运行的几十个G日志也跟着备份出来了,这无疑是增加了备份的负担,毕竟无效的数据,没谁愿意保存管理,
第二个是备份的速度,例如某个比较大的数据库,数据量达到几个T,利用SQL转储方式备份一两天才能完成,这无疑是错误的,毕竟,除了备份,我们还需要使用数据库,而转储可能会有锁表的情况发生,导致停库的时间不可计划,进而数据库部分或者全部不可使用。那么,调整备份策略,使用其它的工具提高备份的效率,无疑会减少停库锁表这样的风险了。
第三个是备份后的恢复还原操作的复杂性,例如,某个数据库我们顺利完成了备份,但恢复还原的操作非常复杂,有着各种的先后顺序,一步错就可能导致前功尽弃或者毁掉整个数据库,那么,无疑的,这种备份是失败的
第四个是备份后的恢复还原效率,例如,某种数据库备份后,由于数据库是非常庞大的,恢复还原还是需要十几个小时,甚至几天,这无疑是不可接受的备份还原,我们应该认为这是一种错误的备份恢复策略。
而lvm快照备份和还原系统是可以满足我们的备份效率的,还原效率可能是有待商榷的,毕竟lvm快照备份的根本底层是建立在操作系统的文件系统下,但,据我估计,还原只是取决于硬盘的读写速度而已了,因为,lvm快照基本是没有什么逻辑的,类似于dd命令,只是要求IO读写速度而已。
从备份范围来说,LVM快照基本是整分区备份,当然也就是全库备份,目前没有什么增量备份策略,需要注意到的是,lvm快照恢复是需要数据库停机,也就是冷恢复的,对于冷恢复方式比较介意的同学就不需要看下面的内容了(冷恢复,指的是恢复还原的时候,数据库完全停止,lvm快照的恢复方式决定了在恢复的时候必须要卸载源分区,数据库自然就停掉了)
下面将在VMware里模拟一个在虚拟机内运行的postgresql数据库,利用lvm快照备份和还原的例子
二,
lvm快照使用前的一些基本概念
1,什么是LVM
LVM是 Logical Volume Manager(逻辑卷管理)的简写,该技术使得动态管理磁盘成为一个可能
那么,何谓动态磁盘管理?
我们知道操作系统有运行和关闭两种状态,动态的意思就是操作系统一直是运行状态,在对磁盘做扩缩容等等调整的时候,不会影响操作系统的运行,也不影响硬盘内的每个分区的数据,反之,静态的磁盘管理就是跑到机箱跟前,关闭操作系统,然后把某个硬盘拿掉,或者直接在操作系统内把某个分区格式化掉,数据清理掉,这无疑的操作效率是低效的,不安全的。
2,
根分区和其它分区的概念
这个不需要我多说了,Linux的一切都要从 / 也就是根开始,一个硬盘可以有四个主分区以及N个逻辑分区,而根分区毫无疑问是不可以格式化的,而其它分区,比如主分区,逻辑分区是可以格式化的,如果这些其它分区内的数据你确定不需要的时候。因此,有的时候,硬件运维工程师会直接一个硬盘只划分一个根分区,这个时候安装的Linux是仍然可以运行的。简单的理解根分区就好像Windows的C盘,也就是当你打开了一个Windows的电脑,虽然你可以格式化C盘,但无疑的,操作系统会报废(Windows低版本好像是可以)。
3,
新磁盘的挂载方式
有四种挂载方式,第一种,loop整盘挂载,也就是直接按自己的需求格式化后,创建一个挂载点挂载,这种方式是可以随时取消挂载的。卸载的时候,新磁盘的内容不会丢失,但分区无法动态扩展
第二种,新添加的磁盘作为根节点的扩容,这种方式无法取消挂载,新磁盘必须是lvm格式的,根节点同样是lvm格式的,可以在添加新磁盘扩展,但若其中一个硬盘损坏,整个服务器将不可用。
第三种,新添加的磁盘单独lvm,不作为任何其它的lvm扩容,这种方式是可以随时取消挂载的,前提是卸载相关的pv,vg,lv,卸载的时候,新磁盘的内容必定会丢失,分区可以动态扩展,新磁盘损坏不影响服务器运行
第四种,新添加的磁盘通过fdisk或者parted,划分普通分区,然后根据需要格式化为特定文件系统,例如,xfs,ext4等等,然后按自己需求挂载到指定挂载点。新磁盘的内容不会丢失,但分区无法动态扩展
那么,本文将只讨论第二种和第三种lvm形式的磁盘扩展管理
三,
新添加的磁盘作为根节点的扩容示例
计划使用VMware虚拟机进行演示,该VMware虚拟机最开始是是一个磁盘,lvm分区格式,后续添加一个新磁盘,新磁盘给根分区提供扩容空间。
两个磁盘,一个是100G,新磁盘是20G,根分区扩容前:
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
可以看到,sdb没有任何使用,sda有20G没有使用,这个没有使用的20G我们暂且不管,主要使用sdb这个新增磁盘以lvm格式加入到sda2也就是根节点
根节点扩容,以sdb这个新磁盘为扩展源:
-
A,先使用fdisk工具,将sdb磁盘转化为lvm格式
[root@centos5 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd6585e20.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd6585e20
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
-
B,查看磁盘状态,发现sdb1还没有加入centos-root:
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 4.2G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
-
C,将sdb1加入centos-root :
####注:先查询现有的vg是哪些,确定库容对象,vgs命令查询出来,扩容对象是centos,该vg现在只有一个pv
####注:第二个命令可以理解为组合命令,创建PV的同时扩容VG
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 1 0 wz--n- 80.00g 0
[root@centos5 ~]# vgextend centos /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
Volume group "centos" successfully extended
-
D,观察磁盘状态,可以看到名为centos的已扩展了一个PV,新磁盘sdb1也可以看到了
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 1 0 wz--n- <100.00g 0
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 4.2G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="z2Rght-nYfZ-FxXe-Vc7H-WGoY-Raxk-JzTNa0" TYPE="LVM2_member"
-
E,开始扩容根分区:
####注:我这里偷懒了,懒得算什么pe的,直接让命令告诉我多少个pe,可以看到是5119个,一个pe是4M,第二个命令输出扩容结果,可以看到扩容成功
[root@centos5 ~]# lvextend -L +20G /dev/centos/root
Insufficient free space: 5120 extents needed, but only 5119 available
[root@centos5 ~]# lvextend -l +5119 /dev/centos/root
Size of logical volume centos/root changed from 80.00 GiB (20480 extents) to <100.00 GiB (25599 extents).
Logical volume centos/root successfully resized.
-
F,保存更改,同步磁盘更改到df命令:
####注,上面的blkid命令可以看到根分区是xfs格式的,因此,同步命令使用的是xfs_growfs
[root@centos5 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=5242880 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=20971520, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=10240, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 20971520 to 26213376
-
G,检查是否扩容成功:
#####注,可以看到,扩容完美完成,一切都没有问题
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 100G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─centos-root 253:0 0 100G 0 lvm /
sr0 11:0 1 4.2G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="z2Rght-nYfZ-FxXe-Vc7H-WGoY-Raxk-JzTNa0" TYPE="LVM2_member"
[root@centos5 ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
sysfs 0 0 0 - /sys
proc 0 0 0 - /proc
devtmpfs 2.0G 0 2.0G 0% /dev
securityfs 0 0 0 - /sys/kernel/security
tmpfs 2.0G 0 2.0G 0% /dev/shm
devpts 0 0 0 - /dev/pts
tmpfs 2.0G 9.0M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
cgroup 0 0 0 - /sys/fs/cgroup/systemd
pstore 0 0 0 - /sys/fs/pstore
cgroup 0 0 0 - /sys/fs/cgroup/memory
cgroup 0 0 0 - /sys/fs/cgroup/net_cls,net_prio
cgroup 0 0 0 - /sys/fs/cgroup/hugetlb
cgroup 0 0 0 - /sys/fs/cgroup/pids
cgroup 0 0 0 - /sys/fs/cgroup/cpu,cpuacct
cgroup 0 0 0 - /sys/fs/cgroup/blkio
cgroup 0 0 0 - /sys/fs/cgroup/perf_event
cgroup 0 0 0 - /sys/fs/cgroup/devices
cgroup 0 0 0 - /sys/fs/cgroup/freezer
cgroup 0 0 0 - /sys/fs/cgroup/cpuset
configfs 0 0 0 - /sys/kernel/config
/dev/mapper/centos-root 100G 1.4G 99G 2% /
systemd-1 - - - - /proc/sys/fs/binfmt_misc
mqueue 0 0 0 - /dev/mqueue
hugetlbfs 0 0 0 - /dev/hugepages
debugfs 0 0 0 - /sys/kernel/debug
/dev/sr0 4.3G 4.3G 0 100% /mnt
/dev/sda1 1014M 136M 879M 14% /boot
tmpfs 396M 0 396M 0% /run/user/0
binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_misc
扩容后的总结
优点:根分区可以持续扩展,当容量快耗尽的时候,可以增加新硬盘,按上面的方法继续的动态扩容
缺点:无法创建lvm快照,因为lvm快照是对根分区快照,无法使用,强行使用的话,必须到服务器跟前进入应急模式后再使用快照,非常的危险,麻烦,其次,如果sdb磁盘损坏,无法卸载此磁盘,因为是根分区扩展形式,强行卸载的话,整个服务器将无法开机,总之,我们需要考虑新加磁盘的质量,例如,某个服务器根节点扩容了3 次,每次使用一个新磁盘,如果期间某个磁盘损坏,整个服务器的数据都将损坏
下面模拟MBR分区信息被覆盖导致磁盘故障的情形
###注:抹去sdb1的MBR信息
[root@centos5 ~]# dd if=/dev/zero of=/dev/sdb1 bs=446 count=1
1+0 records in
1+0 records out
446 bytes (446 B) copied, 0.000108516 s, 4.1 MB/s
此时,服务器已经无法读写数据,下面模拟的是写数据,可以看到写了334B数据就停止了,当然,重启服务器自然是进入紧急救援模式了,如何修复MBR本文就不在此讨论了
[root@centos5 /]# dd if=/dev/random of=/testfile bs=100M count=200
dd: warning: partial read (93 bytes); suggest iflag=fullblock
^X^C0+41 records in
0+41 records out
334 bytes (334 B) copied, 1651.13 s, 0.0 kB/s
[root@centos5 /]# ls -al testfile
-rw-r--r-- 1 root root 334 Dec 23 06:55 testfile
四,
第三种挂载方式的演示
挂载前的磁盘信息:
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
新磁盘sdb利用fdisk转化成lvm格式:
[root@centos5 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2cb32d47.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
转化后新增的sdb1信息:
可以看到sdb1转化成了LVM2_member了
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
创建一个新的vg,名字就叫vg
[root@centos5 ~]# vgcreate vg /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
Volume group "vg" successfully created
创建一个全新的lv,名字叫testlv,注意,这里只使用sdb1的10G,最后一个vg 是上面vgs查询出来的名字
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 1 0 wz--n- 80.00g 0
vg 1 0 0 wz--n- <20.00g <20.00g
[root@centos5 ~]# lvcreate -n testlv -L 10G vg
Logical volume "testlv" created.
lv创建完成后,查看sdb1的磁盘信息
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vg-testlv 253:1 0 10G 0 lvm
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
格式化lv以准备使用,这里使用的是xfs文件系统
[root@centos5 ~]# mkfs.xfs /dev/vg
vg/ vga_arbiter
[root@centos5 ~]# mkfs.xfs /dev/vg/testlv
meta-data=/dev/vg/testlv isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
创建挂载点,手动挂载,挂载完毕后查看磁盘信息,blkid可以看到新增了 /dev/mapper/vg-testlv,文件系统格式为xfs
[root@centos5 ~]# mkdir /data
[root@centos5 ~]# mount -t xfs /dev/vg/testlv /data/
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vg-testlv 253:1 0 10G 0 lvm /data
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
/dev/mapper/vg-testlv: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
df命令可以看到挂载信息(最后一行),是符合我们的预期的,vg空余了10G,下面在从此vg里划分5G出来,扩展/dev/vg/testlv 这个lv,如果vgs查询,看不到剩余,将不可以库容
[root@centos5 ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
sysfs 0 0 0 - /sys
proc 0 0 0 - /proc
devtmpfs 2.0G 0 2.0G 0% /dev
securityfs 0 0 0 - /sys/kernel/security
tmpfs 2.0G 0 2.0G 0% /dev/shm
devpts 0 0 0 - /dev/pts
tmpfs 2.0G 9.0M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
cgroup 0 0 0 - /sys/fs/cgroup/systemd
pstore 0 0 0 - /sys/fs/pstore
cgroup 0 0 0 - /sys/fs/cgroup/memory
cgroup 0 0 0 - /sys/fs/cgroup/hugetlb
cgroup 0 0 0 - /sys/fs/cgroup/cpu,cpuacct
cgroup 0 0 0 - /sys/fs/cgroup/pids
cgroup 0 0 0 - /sys/fs/cgroup/cpuset
cgroup 0 0 0 - /sys/fs/cgroup/net_cls,net_prio
cgroup 0 0 0 - /sys/fs/cgroup/devices
cgroup 0 0 0 - /sys/fs/cgroup/freezer
cgroup 0 0 0 - /sys/fs/cgroup/blkio
cgroup 0 0 0 - /sys/fs/cgroup/perf_event
configfs 0 0 0 - /sys/kernel/config
/dev/mapper/centos-root 80G 1.4G 79G 2% /
systemd-1 0 0 0 - /proc/sys/fs/binfmt_misc
mqueue 0 0 0 - /dev/mqueue
debugfs 0 0 0 - /sys/kernel/debug
hugetlbfs 0 0 0 - /dev/hugepages
/dev/sr0 4.4G 4.4G 0 100% /mnt
/dev/sda1 1014M 136M 879M 14% /boot
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vg-testlv 10G 33M 10G 1% /data
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 1 0 wz--n- 80.00g 0
vg 1 1 0 wz--n- <20.00g <10.00g
扩容/dev/vg/testlv 并同步信息到df命令,最终查看sdb1的磁盘信息
[root@centos5 ~]# lvextend -L +5G /dev/vg/testlv
Size of logical volume vg/testlv changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume vg/testlv successfully resized.
[root@centos5 ~]# xfs_growfs /dev/vg/testlv
meta-data=/dev/mapper/vg-testlv isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2621440 to 3932160
[root@centos5 ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
sysfs 0 0 0 - /sys
proc 0 0 0 - /proc
devtmpfs 2.0G 0 2.0G 0% /dev
securityfs 0 0 0 - /sys/kernel/security
tmpfs 2.0G 0 2.0G 0% /dev/shm
devpts 0 0 0 - /dev/pts
tmpfs 2.0G 9.0M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
cgroup 0 0 0 - /sys/fs/cgroup/systemd
pstore 0 0 0 - /sys/fs/pstore
cgroup 0 0 0 - /sys/fs/cgroup/memory
cgroup 0 0 0 - /sys/fs/cgroup/hugetlb
cgroup 0 0 0 - /sys/fs/cgroup/cpu,cpuacct
cgroup 0 0 0 - /sys/fs/cgroup/pids
cgroup 0 0 0 - /sys/fs/cgroup/cpuset
cgroup 0 0 0 - /sys/fs/cgroup/net_cls,net_prio
cgroup 0 0 0 - /sys/fs/cgroup/devices
cgroup 0 0 0 - /sys/fs/cgroup/freezer
cgroup 0 0 0 - /sys/fs/cgroup/blkio
cgroup 0 0 0 - /sys/fs/cgroup/perf_event
configfs 0 0 0 - /sys/kernel/config
/dev/mapper/centos-root 80G 1.4G 79G 2% /
systemd-1 - - - - /proc/sys/fs/binfmt_misc
mqueue 0 0 0 - /dev/mqueue
debugfs 0 0 0 - /sys/kernel/debug
hugetlbfs 0 0 0 - /dev/hugepages
/dev/sr0 4.4G 4.4G 0 100% /mnt
/dev/sda1 1014M 136M 879M 14% /boot
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vg-testlv 15G 33M 15G 1% /data
binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_misc
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vg-testlv 253:1 0 15G 0 lvm /data
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
/dev/mapper/vg-testlv: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
五,
lvm快照的示例
前面的演示中,我的sda盘留了19G没有使用,这个19G可以划分到sdb:
首先使用fdisk重新划分sda,把剩余的19G分到sda3这个分区,期间lsblk查询不到新分区,因此使用partprobe命令同步信息
[root@centos5 ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008cd1a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 169879551 83890176 8e Linux LVM
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (169879552-209715199, default 169879552):
Using default value 169879552
Last sector, +sectors or +size{K,M,G} (169879552-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 19 GiB is set
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008cd1a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 169879551 83890176 8e Linux LVM
/dev/sda3 169879552 209715199 19917824 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 80G 0 part
└─centos-root 253:0 0 80G 0 lvm /
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vg-testlv 253:1 0 15G 0 lvm /data
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
/dev/mapper/vg-testlv: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
[root@centos5 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
partprobe命令后,可以看到新划分的sda3,名为vg的这个vg也留了5G,因此扩展vg得到24G
[root@centos5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 80G 0 part
│ └─centos-root 253:0 0 80G 0 lvm /
└─sda3 8:3 0 19G 0 part
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vg-testlv 253:1 0 15G 0 lvm /data
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 1 0 wz--n- 80.00g 0
vg 1 1 0 wz--n- <20.00g <5.00g
[root@centos5 ~]# vgextend vg /dev/sda3
Physical volume "/dev/sda3" successfully created.
Volume group "vg" successfully extended
[root@centos5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 1 0 wz--n- 80.00g 0
vg 2 1 0 wz--n- <38.99g <23.99g
注意,注意,lvm快照必须要有剩余的VFree,否则没有地方保存快照,下面开始创建lvm快照:
[root@centos5 ~]# lvcreate -s -n lvsnap1 -L +2G /dev/vg/testlv
Logical volume "lvsnap1" created.
这样创建的lvm快照有点不太友好,也就是脚本不能使用,修改成脚本可以使用的:
[root@centos5 ~]# date=$(date +'%Y-%m-%d-%H-%M-%S')
[root@centos5 ~]# lvcreate -s -n $date -L +2G /dev/vg/testlv
Logical volume "2023-12-26-22-51-38" created.
删除快照:
先查询有哪些快照
[root@centos5 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID DD3jlh-uziD-NkpQ-g2tY-rFk4-Bj0a-HPCDdV
LV Write Access read/write
LV Creation host, time localhost, 2023-06-18 08:45:58 +0800
LV Status available
# open 1
LV Size 80.00 GiB
Current LE 20480
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg/testlv
LV Name testlv
VG Name vg
LV UUID V0tsdx-zcpw-dZVu-80rc-J5q7-6pR5-5wojmc
LV Write Access read/write
LV Creation host, time centos5, 2023-12-23 21:44:14 +0800
LV snapshot status source of
2023-12-25 [active]
2023-12-26-22-51-38 [active]
2023-12-26-22-52-45 [active]
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/vg/2023-12-25
LV Name 2023-12-25
VG Name vg
LV UUID mM6liB-Az5L-EaiJ-U1Ra-TxUo-6AF0-d6jqPO
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 22:49:49 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
可以看到,快照有COW字样,和普通的lv是不一样的,并且Allocated to snapshot
删除快照:
[root@centos5 ~]# lvremove /dev/vg/2023-12-25 -y
Logical volume "2023-12-25" successfully removed
六,
lvm快照的秘密
先随便创建两个lvm快照,并查看快照使用率:
可以看到51-38和52-45两个快照的使用率都是0.01% ,Allocated to snapshot 就是快照的使用率,快照的大小是 COW-table size,快照的源是LV snapshot status active destination for testlv,也就是testlv,快照的状态是 LV Status available
[root@centos5 data]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID DD3jlh-uziD-NkpQ-g2tY-rFk4-Bj0a-HPCDdV
LV Write Access read/write
LV Creation host, time localhost, 2023-06-18 08:45:58 +0800
LV Status available
# open 1
LV Size 80.00 GiB
Current LE 20480
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg/testlv
LV Name testlv
VG Name vg
LV UUID V0tsdx-zcpw-dZVu-80rc-J5q7-6pR5-5wojmc
LV Write Access read/write
LV Creation host, time centos5, 2023-12-23 21:44:14 +0800
LV snapshot status source of
2023-12-26-22-51-38 [active]
2023-12-26-22-52-45 [active]
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-51-38
LV Name 2023-12-26-22-51-38
VG Name vg
LV UUID BSwfzP-aAof-2INU-q9xD-n8mf-F4dB-dWdK4o
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 22:51:40 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:6
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-52-45
LV Name 2023-12-26-22-52-45
VG Name vg
LV UUID HwCQGm-2hc8-vQSg-yrBR-3zRI-3Xas-IAH02U
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 23:06:34 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
源分区内写入一些文件,并查看快照的详情:
可以看到,两个快照同时提升了使用率,说明两个快照同时生效了
[root@centos5 data]# dd if=/dev/zero of=/data/testfile bs=150M count=6
6+0 records in
6+0 records out
943718400 bytes (944 MB) copied, 3.71762 s, 254 MB/s
[root@centos5 data]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID DD3jlh-uziD-NkpQ-g2tY-rFk4-Bj0a-HPCDdV
LV Write Access read/write
LV Creation host, time localhost, 2023-06-18 08:45:58 +0800
LV Status available
# open 1
LV Size 80.00 GiB
Current LE 20480
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg/testlv
LV Name testlv
VG Name vg
LV UUID V0tsdx-zcpw-dZVu-80rc-J5q7-6pR5-5wojmc
LV Write Access read/write
LV Creation host, time centos5, 2023-12-23 21:44:14 +0800
LV snapshot status source of
2023-12-26-22-51-38 [active]
2023-12-26-22-52-45 [active]
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-51-38
LV Name 2023-12-26-22-51-38
VG Name vg
LV UUID BSwfzP-aAof-2INU-q9xD-n8mf-F4dB-dWdK4o
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 22:51:40 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 31.18%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:6
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-52-45
LV Name 2023-12-26-22-52-45
VG Name vg
LV UUID HwCQGm-2hc8-vQSg-yrBR-3zRI-3Xas-IAH02U
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 23:06:34 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 31.18%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
查看磁盘详情:
[root@centos5 data]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 80G 0 part
│ └─centos-root 253:0 0 80G 0 lvm /
└─sda3 8:3 0 19G 0 part
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
├─vg-testlv-real 253:1 0 15G 0 lvm
│ ├─vg-testlv 253:2 0 15G 0 lvm /data
│ ├─vg-2023--12--26--22--52--45 253:4 0 15G 0 lvm
│ └─vg-2023--12--26--22--51--38 253:6 0 15G 0 lvm
├─vg-2023--12--26--22--52--45-cow 253:3 0 2G 0 lvm
│ └─vg-2023--12--26--22--52--45 253:4 0 15G 0 lvm
└─vg-2023--12--26--22--51--38-cow 253:5 0 2G 0 lvm
└─vg-2023--12--26--22--51--38 253:6 0 15G 0 lvm
sr0 11:0 1 4.4G 0 rom /mnt
[root@centos5 data]# blkid
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="326cd1a2-713a-4a37-85d9-8f1606083297" TYPE="xfs"
/dev/sda2: UUID="RoyB4E-tuJx-OSAn-uGvH-YOiH-rerF-51wkyI" TYPE="LVM2_member"
/dev/sda3: UUID="zF0iGr-wVpE-NNF3-6lo9-aXmd-Mh7z-L58ijQ" TYPE="LVM2_member"
/dev/sdb1: UUID="uj3eg7-RN9e-u7Ev-OQqz-1F5b-AC5H-5Q4Dax" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="81ad8aed-bf72-4bd9-b9f4-8743e1dc074d" TYPE="xfs"
/dev/mapper/vg-testlv: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
/dev/mapper/vg-2023--12--26--22--51--38: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
/dev/mapper/vg-2023--12--26--22--52--45: UUID="a9506987-a65d-40a7-821a-638f3b3e2a22" TYPE="xfs"
挂载第一个快照,查看第一个快照的内容:
可以看到,快照保存的是打快照之前的/data目录下的内容
[root@centos5 ~]# mount -o nouuid /dev/mapper/vg-2023--12--26--22--51--38 /media/
那么,Allocated to snapshot 指的是什么呢?其实,指的是快照所能容纳的源分区的改变,快照只记录文件的增加,文件的减少不记录,如何证明这个结论呢?
以上两个快照删除:
[root@centos5 ~]# lvremove /dev/vg/2023-12-26-22-51-38 -y
Logical volume "2023-12-26-22-51-38" successfully removed
[root@centos5 ~]# lvremove /dev/vg/2023-12-26-22-52-45 -y
Logical volume "2023-12-26-22-52-45" successfully removed
重新打一个lvm快照,源分区仍然是testlv,打快照之前,看看源分区有哪些文件,可以看到有两个900M的文件,还一个txt文件:
[root@centos5 ~]# ls -alh /data/
total 1.8G
drwxr-xr-x 2 root root 55 Dec 26 23:24 .
dr-xr-xr-x. 18 root root 272 Dec 25 23:38 ..
-rw-r--r-- 1 root root 900M Dec 26 23:09 testfile
-rw-r--r-- 1 root root 900M Dec 26 23:24 testfile1
-rw-r--r-- 1 root root 20 Dec 26 23:07 test.txt
[root@centos5 ~]# lvcreate -s -n $date -L +2G /dev/vg/testlv
Logical volume "2023-12-26-22-52-45" created.
查看快照:
快照仍然是2G大小,快照使用的还是0.01%
[root@centos5 ~]# lvdisplay /dev/vg/2023-12-26-22-52-45
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-52-45
LV Name 2023-12-26-22-52-45
VG Name vg
LV UUID K5dSeS-3Tfx-bcFr-HfZw-b6Xu-kckr-ZXawHW
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 23:33:54 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
删除源分区内的文件后,查看快照使用率:
文件减少不影响快照的使用率
[root@centos5 ~]# lvdisplay /dev/vg/2023-12-26-22-52-45
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-52-45
LV Name 2023-12-26-22-52-45
VG Name vg
LV UUID K5dSeS-3Tfx-bcFr-HfZw-b6Xu-kckr-ZXawHW
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 23:33:54 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
[root@centos5 ~]# mount -o nouuid /dev/mapper/vg-2023--12--26--22--52--45 /media/
[root@centos5 ~]# cd /media/
[root@centos5 media]# ls -al
total 1843204
drwxr-xr-x 2 root root 55 Dec 26 23:24 .
dr-xr-xr-x. 18 root root 272 Dec 25 23:38 ..
-rw-r--r-- 1 root root 943718400 Dec 26 23:09 testfile
-rw-r--r-- 1 root root 943718400 Dec 26 23:24 testfile1
-rw-r--r-- 1 root root 20 Dec 26 23:07 test.txt
取消挂载,源分区增加2个900M的文件,并查看快照的使用率:
可以看到,使用率到88.33,换算下来确实是新增的testfile2和testfile3这两个文件
[root@centos5 ~]# dd if=/dev/zero of=/data/testfile2 bs=150M count=6
6+0 records in
6+0 records out
943718400 bytes (944 MB) copied, 1.1629 s, 812 MB/s
[root@centos5 ~]# dd if=/dev/zero of=/data/testfile3 bs=150M count=6
6+0 records in
6+0 records out
943718400 bytes (944 MB) copied, 0.930175 s, 1.0 GB/s
[root@centos5 ~]# lvdisplay /dev/vg/2023-12-26-22-52-45
--- Logical volume ---
LV Path /dev/vg/2023-12-26-22-52-45
LV Name 2023-12-26-22-52-45
VG Name vg
LV UUID K5dSeS-3Tfx-bcFr-HfZw-b6Xu-kckr-ZXawHW
LV Write Access read/write
LV Creation host, time centos5, 2023-12-26 23:33:54 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 88.33%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
OK,现在删除源分区内的所有文件,并卸载源分区,然后使用这个快照恢复:
[root@centos5 ~]# rm -rf /data/*
[root@centos5 ~]# umount /data/
[root@centos5 ~]# lvconvert --merge /dev/vg/2023-12-27-08-01-39
Merging of volume vg/2023-12-27-08-01-39 started.
vg/testlv: Merged: 100.00%
[root@centos5 ~]# mount /dev/vg/testlv /data/
[root@centos5 ~]# ls /data/
testfile testfile1 test.txt
此时,快照自动删除了,只要使用一次,快照就没有了:
[root@centos5 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID DD3jlh-uziD-NkpQ-g2tY-rFk4-Bj0a-HPCDdV
LV Write Access read/write
LV Creation host, time localhost, 2023-06-18 08:45:58 +0800
LV Status available
# open 1
LV Size 80.00 GiB
Current LE 20480
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg/testlv
LV Name testlv
VG Name vg
LV UUID V0tsdx-zcpw-dZVu-80rc-J5q7-6pR5-5wojmc
LV Write Access read/write
LV Creation host, time centos5, 2023-12-23 21:44:14 +0800
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
小结:
数据盘单独挂载方式vgcreate和lvcreate,sdb磁盘如果损坏,并不会影响服务器整体的运行,卸载掉 /data 这个挂载点就可以了。
lvm快照是一次性的,只要使用了快照进行恢复,就自动删除
lvm快照是一种实时备份的方式,快照建立的时刻,源分区的所有数据就由与源分区同区的快照保存了,快照的速度是非常快的,通常在几秒内,而快照恢复也是十分迅速的,比dd命令快速N个量级,用于数据库的实时备份是完全可以的。(本例使用的/data目录是完全可以用于数据库的)