Linux磁盘管理之LVM逻辑卷快照

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Linux磁盘管理之LVM逻辑卷快照

qiuhom   2020-01-12 我要评论

  一、快照的工作原理

  所谓快照就是将当时的系统数据记录下来,在未来若有数据变动,则会将变更前的数据放入快照区进行保存。我们可理解为快照就是给系统拍了一张照片,记录当时系统在拍快照的状态。只不过现实生活中的照片是没有办法将现在的生活还原到照片的时候,而Linux系统里LVM快照是可以的。从上面的阐述可以了解到,被做快照的逻辑卷是分成了两个部分,一部分是数据没有改动前的和数据变更后的。它是怎么工作的呢?在我们给系统做快照的前提是,系统上基于LVM管理的,且快照和逻辑卷必须是在同一卷组上;在我们给LVM管理的系统上做快照卷的时候,快照不可以给多个逻辑卷一起做,它是一个逻辑卷对应一个快照卷,不可以多个逻辑卷对应一个快照卷。快照是一次性使用,还原了快照,随之快照就失效,消失。接下来说说原理吧。快照是特殊的逻辑卷,它之所以要和被做快照的逻辑卷一个卷组是因为它在生成快照时,它是和LV共享很多PE的区块,因此快照必须同LV同一个卷组中。之所以叫快照,就是因为它快嘛,它为什么快,这就要和它的工作原理有密切的关系了,快照生成时,实际上它不是去拷贝原LV里的数据,它只是分配我们指定大小的空间,在原LV上的数据发生变化时,这个时候快照会把发生改变的数据先拷贝到快照区,这样一来快照就只是存放着被修改之前的数据。这样就起到了备份修改数据的作用,修改后的数据就存放在原LV上。如果有一天我们需要还原快照,这个时候快照就会和先有的LV做合并,快照里面存放修改之前的数据,快照和现有LV做合并时,快照上的数据会覆盖现有LV上数据,这样一来就实现了把数据还原到拍快照当时的状态。这里需要注意一点的是快照只是覆盖改变后的数据,没有做改变的数据它是不会去覆盖的。所以快照使用过后它就没有任何意义,它不可以再次记录恢复后数据的变化前的数据。在做快照的时候我们还需要注意其快照的大小,快照指定的空间大小不要太小,太小可能装不小变化前的数据,导致快照失效,当然空间也不宜太大,太大浪费空间没有意义,空间超过原LV大小,其实也是没有意义的,它最大空间就是和原LV大小相等,所以在做快照时,我们需要评估大概有多少的数据在发生变化,避免空间过小导致快照失效。

  二、快照的实现

  1)查看现有逻辑卷所在卷组空间是否足够创建快照

[root@test-centos7-node1 ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  xxx  test -wi-a----- 12.00g                                                    
[root@test-centos7-node1 ~]# vgdisplay test
  --- Volume group ---
  VG Name               test
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  51
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <29.98 GiB
  PE Size               8.00 MiB
  Total PE              3837
  Alloc PE / Size       1536 / 12.00 GiB
  Free  PE / Size       2301 / <17.98 GiB
  VG UUID               31vxAP-L5jb-8Yxm-25m2-LdD0-ee8R-sdTjum
   
[root@test-centos7-node1 ~]# 

  说明:这是已经创建好逻辑卷的系统上查看的,有关逻辑卷的管理请参考https://www.cnblogs.com/qiuhom-1874/p/12156146.html,可以看到还有17个G处于空闲状态

  2)针对现有逻辑卷创建快照

[root@test-centos7-node1 ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
https://img.qb5200.com/download-x/dev/sda3             48209924 1618304  46591620   4% /
devtmpfs               1922828       0   1922828   0% https://img.qb5200.com/download-x/dev
tmpfs                  1932652       0   1932652   0% https://img.qb5200.com/download-x/dev/shm
tmpfs                  1932652    8792   1923860   1% /run
tmpfs                  1932652       0   1932652   0% /sys/fs/cgroup
https://img.qb5200.com/download-x/dev/sda1              2086912  137672   1949240   7% /boot
tmpfs                   386532       0    386532   0% /run/user/0
https://img.qb5200.com/download-x/dev/mapper/test-xxx  10190100   36896   9612536   1% /mnt
[root@test-centos7-node1 ~]# ls
hellodb_innodb.sql  test
[root@test-centos7-node1 ~]# 
[root@test-centos7-node1 ~]# df 
Filesystem           1K-blocks    Used Available Use% Mounted on
https://img.qb5200.com/download-x/dev/sda3             48209924 1618304  46591620   4% /
devtmpfs               1922828       0   1922828   0% https://img.qb5200.com/download-x/dev
tmpfs                  1932652       0   1932652   0% https://img.qb5200.com/download-x/dev/shm
tmpfs                  1932652    8792   1923860   1% /run
tmpfs                  1932652       0   1932652   0% /sys/fs/cgroup
https://img.qb5200.com/download-x/dev/sda1              2086912  137672   1949240   7% /boot
tmpfs                   386532       0    386532   0% /run/user/0
https://img.qb5200.com/download-x/dev/mapper/test-xxx  10190100   36896   9612536   1% /mnt
[root@test-centos7-node1 ~]# ls /mnt
fstab  inittab  lost+found
[root@test-centos7-node1 ~]# vgdisplay 
  --- Volume group ---
  VG Name               test
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  51
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <29.98 GiB
  PE Size               8.00 MiB
  Total PE              3837
  Alloc PE / Size       1536 / 12.00 GiB
  Free  PE / Size       2301 / <17.98 GiB
  VG UUID               31vxAP-L5jb-8Yxm-25m2-LdD0-ee8R-sdTjum
   
[root@test-centos7-node1 ~]# lvcreate -L 1G -n lv_xxx_snap -p r -s https://img.qb5200.com/download-x/dev/test/xxx 
  Logical volume "lv_xxx_snap" created.
[root@test-centos7-node1 ~]# lvs
  LV          VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_xxx_snap test sri-a-s---  1.00g      xxx    0.01                                   
  xxx         test owi-aos--- 12.00g                                                    
[root@test-centos7-node1 ~]# 

  说明:创建快照和创建其他逻辑卷的命令是一样的,都是用lvcreate  其中-s就是指定创建的逻辑卷为快照 -p 指定其快照属性 r 表示只读属性 最后要跟上对那个逻辑卷做快照,当然我们指定被做快照的逻辑对应设备的路径,系统就能够分辨出快照该创建到那个VG上,所以我们这里是可不指定VG的名称的

[root@test-centos7-node1 ~]# lvdisplay https://img.qb5200.com/download-x/dev/test/lv_xxx_snap 
  --- Logical volume ---
  LV Path                https://img.qb5200.com/download-x/dev/test/lv_xxx_snap
  LV Name                lv_xxx_snap
  VG Name                test
  LV UUID                ZjBpYF-2fg6-n6Wz-ehJd-Zagv-lg3y-HFmHjz
  LV Write Access        read only
  LV Creation host, time test-centos7-node1, 2020-01-11 03:18:29 -0500
  LV snapshot status     active destination for xxx
  LV Status              available
  # open                 0
  LV Size                12.00 GiB
  Current LE             1536
  COW-table size         1.00 GiB
  COW-table LE           128
  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:3
   
[root@test-centos7-node1 ~]# 

  说明:我们创建好快照,可用lvdisplay 指定快照对应设备路径来查看快照的详情,这个同查看逻辑卷的详情用法一致。可以看到该快照是用于xxx 逻辑卷的快照

  3)创建挂载目录挂载快照

[root@test-centos7-node1 ~]# mkdir /snap
[root@test-centos7-node1 ~]# mount https://img.qb5200.com/download-x/dev/test/lv_xxx_snap /snap
mount: https://img.qb5200.com/download-x/dev/mapper/test-lv_xxx_snap is write-protected, mounting read-only
[root@test-centos7-node1 ~]# ls /snap/
fstab  inittab  lost+found
[root@test-centos7-node1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
https://img.qb5200.com/download-x/dev/sda3                      46G  1.6G   45G   4% /
devtmpfs                      1.9G     0  1.9G   0% https://img.qb5200.com/download-x/dev
tmpfs                         1.9G     0  1.9G   0% https://img.qb5200.com/download-x/dev/shm
tmpfs                         1.9G  8.7M  1.9G   1% /run
tmpfs                         1.9G     0  1.9G   0% /sys/fs/cgroup
https://img.qb5200.com/download-x/dev/sda1                     2.0G  135M  1.9G   7% /boot
tmpfs                         378M     0  378M   0% /run/user/0
https://img.qb5200.com/download-x/dev/mapper/test-xxx          9.8G   37M  9.2G   1% /mnt
https://img.qb5200.com/download-x/dev/mapper/test-lv_xxx_snap  9.8G   37M  9.2G   1% /snap
[root@test-centos7-node1 ~]# 

  说明:挂载上快照,我们发现快照的大小和被做快照的大小是一致的,使用量什么的都是一致的,上面不是说快照里面存放的是被修改前的数据吗,怎么有数据了呢,这是因为我们看到快照里面的数据不是快照本身的数据,是共享其原LV里的数据,所以我们是可以看到的,之所以能看到是系统把原有的LV数据镜像到快照里,让我们感觉里面是有东西的,就是告诉我们快照做好了,别担心。这里需要注意一点,如果原LV的文件系统上xfs文件系统,挂载的时候可能出现挂载不上的情况,原因是快照的UUID和原LV的UUID相同,xfs的文件系统默认挂载选项会检查UUID,如果冲突它会不让你挂载的,这个时候我们挂载需要指定其选项不检查UUID ,用 mount -o 来指定其挂载选项 nouuid 表示挂载不检查uuid;本人是把LV的文件系统格式化成ext4,所以挂载的时候没有加选项是可以挂载上去的;如果创建快照时没有指定为只读属性,那么挂载的时候建议大家加上挂载选项为只读,这样挂载上的快照才能不被修改的可能。

  4)修改原LV里的文件,看看快照卷里的文件是否有更改

[root@test-centos7-node1 ~]# cat /mnt/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Jan  1 07:19:49 2020
#
# Accessible filesystems, by reference, are maintained under 'https://img.qb5200.com/download-x/devhttps://img.qb5200.com/download-x/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=fea42698-b2ff-41fb-b517-a1caa0b962d7 /                       xfs     defaults        0 0
UUID=512dff54-789c-41e8-8765-9542215f2a01 /boot                   xfs     defaults        0 0
UUID=e2582696-1cab-4c28-9793-108602f39f24 swap                    swap    defaults        0 0
[root@test-centos7-node1 ~]# cat /snap/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Jan  1 07:19:49 2020
#
# Accessible filesystems, by reference, are maintained under 'https://img.qb5200.com/download-x/devhttps://img.qb5200.com/download-x/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=fea42698-b2ff-41fb-b517-a1caa0b962d7 /                       xfs     defaults        0 0
UUID=512dff54-789c-41e8-8765-9542215f2a01 /boot                   xfs     defaults        0 0
UUID=e2582696-1cab-4c28-9793-108602f39f24 swap                    swap    defaults        0 0
[root@test-centos7-node1 ~]# echo "this is test file " > /mnt/fstab 
[root@test-centos7-node1 ~]# cat /mnt/fstab
this is test file 
[root@test-centos7-node1 ~]# cat /snap/fstab

#
# /etc/fstab
# Created by anaconda on Wed Jan  1 07:19:49 2020
#
# Accessible filesystems, by reference, are maintained under 'https://img.qb5200.com/download-x/devhttps://img.qb5200.com/download-x/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=fea42698-b2ff-41fb-b517-a1caa0b962d7 /                       xfs     defaults        0 0
UUID=512dff54-789c-41e8-8765-9542215f2a01 /boot                   xfs     defaults        0 0
UUID=e2582696-1cab-4c28-9793-108602f39f24 swap                    swap    defaults        0 0
[root@test-centos7-node1 ~]# 

  说明:发现没有我们修改原LV里的数据后,快照里边的对应的文件内容没有发生变化,所以快照里边永远保存的是做快照那一刻的数据状态情况,中间怎么修改它都只保存最老的版本,LV里面保存最新版本的状态,这里需要提醒的是快照虽然有备份的效果,但是它不能代替备份,如果vg怀了,那么逻辑卷和快照都将失效。

  三、快照还原

  1) 取消逻辑卷和快照的挂载

[root@test-centos7-node1 ~]# df
Filesystem                   1K-blocks    Used Available Use% Mounted on
https://img.qb5200.com/download-x/dev/sda3                     48209924 1618368  46591556   4% /
devtmpfs                       1922828       0   1922828   0% https://img.qb5200.com/download-x/dev
tmpfs                          1932652       0   1932652   0% https://img.qb5200.com/download-x/dev/shm
tmpfs                          1932652    8776   1923876   1% /run
tmpfs                          1932652       0   1932652   0% /sys/fs/cgroup
https://img.qb5200.com/download-x/dev/sda1                      2086912  137672   1949240   7% /boot
tmpfs                           386532       0    386532   0% /run/user/0
https://img.qb5200.com/download-x/dev/mapper/test-xxx          10190100   36896   9612536   1% /mnt
https://img.qb5200.com/download-x/dev/mapper/test-lv_xxx_snap  10190100   36896   9612536   1% /snap
[root@test-centos7-node1 ~]# umount /mnt
[root@test-centos7-node1 ~]# umount /snap/
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
https://img.qb5200.com/download-x/dev/sda3       48209924 1618368  46591556   4% /
devtmpfs         1922828       0   1922828   0% https://img.qb5200.com/download-x/dev
tmpfs            1932652       0   1932652   0% https://img.qb5200.com/download-x/dev/shm
tmpfs            1932652    8776   1923876   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
https://img.qb5200.com/download-x/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# 

  说明:还原就是把快照和原LV合并,所以两者都必须是处于非挂载的状态

  2)合并逻辑卷和快照,进行数据还原

[root@test-centos7-node1 ~]# lvs
  LV          VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_xxx_snap test sri-a-s---  1.00g      xxx    0.01                                   
  xxx         test owi-a-s--- 12.00g                                                    
[root@test-centos7-node1 ~]# lvconvert --merge https://img.qb5200.com/download-x/dev/test/lv_xxx_snap 
  Merging of volume test/lv_xxx_snap started.
  test/xxx: Merged: 100.00%
[root@test-centos7-node1 ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  xxx  test -wi-a----- 12.00g                                                    
[root@test-centos7-node1 ~]# 

  说明:合并快照和逻辑卷后,快照的的使命就完成了,当然快照也就没有存在的意义,系统会把快照给删除掉,在centos6上做合并是可看到删除快照成功的提示。我们指定合并的快照就可以了,因为快照里记录了原LV是哪一个,所以合并时不用在指定其原LV

  3)挂载LV 查看数据

[root@test-centos7-node1 ~]# mount https://img.qb5200.com/download-x/dev/test/xxx /mnt
[root@test-centos7-node1 ~]# ls /mnt
fstab  inittab  lost+found
[root@test-centos7-node1 ~]# cat /mnt/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Jan  1 07:19:49 2020
#
# Accessible filesystems, by reference, are maintained under 'https://img.qb5200.com/download-x/devhttps://img.qb5200.com/download-x/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=fea42698-b2ff-41fb-b517-a1caa0b962d7 /                       xfs     defaults        0 0
UUID=512dff54-789c-41e8-8765-9542215f2a01 /boot                   xfs     defaults        0 0
UUID=e2582696-1cab-4c28-9793-108602f39f24 swap                    swap    defaults        0 0
[root@test-centos7-node1 ~]# 

  说明:可以看到fstab文件已经还原到被修改前的状态。这种方式是利用快照,还原到原有LV里,如果需要还原到其他LV里 这个时候就需先要把快照里的东西给备份出来,然后再复制给到其他LV里即可,当然这种方式就不是利用快照来还原,它的本质就是文件复制到另外的地方,和快照没有关系。快照只是记录快照生成时的数据状态,生产中建议利用快照来记录快照生成当时的数据状态,然后在把快照挂载到系统上,然后慢慢拷贝数据进行备份,拷贝完后删除快照。一般情况快照不易在系统上生存太长时间,时间越长,影响磁盘的I/O写入,可能影响服务器的性能。建议把快照里的数据拷完就删除快照。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们