IT审计与灾备 4月24日 星期三 20:00 云祺视频号准时直播
vinchin linked
delta 扫码预约直播,观看精彩内容!
closed
logo
关于我们

技术分享

技术分享 lvm 逻辑卷的快照及备份 还原

lvm 逻辑卷的快照及备份 还原

2018-08-15

1. LVM的快照概述
    LVM机制还提供了对LV做快照的功能,以此来获得文件系统的状态一致性备份。LVM采用写时复制技术(Copy-On-Write,COW),不用停止服务或将逻辑卷设为只读就可以进行备份,使用LVM快照功能既可以获得一致备份,又不会影响服务器的可用性。
    LVM采用的写时复制,是指当LVM快照创建的时候,仅复制原始卷中数据的元数据。换句话说,也就是在创建LVM逻辑卷的时候,并不会发生数据的物理复制。再换句话说,复制元数据,不复制物理数据,够清楚了吧?因此快照的创建几乎是实时的。当原始卷上有写的操作执行时,快照会跟踪原始卷中块的改变,这个时候原始卷上将要改变的数据会在改变之前拷贝到快照预留的空间。

2.创建LVM快照
    创建快照实际上也是创建了一个逻辑卷,只不过该卷的属性与普通逻辑卷的属性有些不一样。下面以/dev/vg6/ftpdata创建一个大小为500M的快照为例,演示快照创建于使用的方法。
首先我们先看一下/dev/vg6/ftpdata里面的内容,里面有很多文件.


前期的一些步骤,我就不多说了
[root@yangcan Desktop]# pvcreate /dev/sda5
  Writing physical volume data to disk "/dev/sda5"
  Physical volume "/dev/sda5" successfully created
[root@yangcan Desktop]# vgcreate vg5 /dev/sda5
  Volume group "vg5" successfully created
[root@yangcan Desktop]# vgs
  VG         #PV #LV #SN Attr   VSize  VFree 
  vg5          1   0   0 wz--n- 10.00g 10.00g
  vg_yangcan   1   2   0 wz--n- 19.51g     0
[root@yangcan Desktop]# lvcreate -L 6G -n ftp vg5
  Logical volume "ftp" created
[root@yangcan Desktop]# mkdir /ftp
[root@yangcan Desktop]# mke2fs -T ext4 /dev/mapper/vg5-ftp 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@yangcan /]# mount /dev/mapper/vg5-ftp /ftp/
[root@yangcan /]# mount|grep ftp
/dev/mapper/vg5-ftp on /ftp type ext4 (rw)
[root@yangcan /]# df -h |grep ftp
/dev/mapper/vg5-ftp   6.0G  140M  5.5G   3% /ftp
[root@yangcan /]# cd /ftp/
[root@yangcan ftp]# ll
total 16
drwx------. 2 root root 16384 Aug  2 21:12 lost+found
[root@yangcan ftp]# cp /etc/*.conf .
[root@yangcan ftp]# cp /usr/share/dict/linux.words .
[root@yangcan ftp]# ll
total 5112
-rw-r--r--. 1 root root     148 Aug  2 21:18 asound.conf
-rw-------. 1 root root     232 Aug  2 21:18 autofs_ldap_auth.conf
-rw-r--r--. 1 root root    1780 Aug  2 21:18 cas.conf
-rw-r--r--. 1 root root   21214 Aug  2 21:18 dnsmasq.conf
-rw-r--r--. 1 root root     559 Aug  2 21:18 dracut.conf
-rw-r--r--. 1 root root      20 Aug  2 21:18 fprintd.conf
-rw-r--r--. 1 root root       0 Aug  2 21:18 gai.conf
-rw-------. 1 root root     821 Aug  2 21:18 grub.conf
..........................
我们在这个时刻拍一个快照
[root@yangcan ~]# lvcreate -L 500M -n snap-ftp -s  /dev/vg5/ftp 
  Logical volume "snap-ftp" created


我们查看一下逻辑卷和快照的信息!
[root@yangcan ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg5/ftp
  VG Name                vg5
  LV UUID                tRDd7b-jYiA-bxA3-KtUx-mYZW-xwHL-21SSVk
  LV Write Access        read/write
  LV snapshot status     source of
                         /dev/vg5/snap-ftp [active]
  LV Status              available
  # open                 1
  LV Size                6.00 GiB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
  --- Logical volume ---
  LV Name                /dev/vg5/snap-ftp
  VG Name                vg5
  LV UUID                0y8tuc-GdFu-duvd-fSDt-wsLQ-fYWa-q6hOd3
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/vg5/ftp
  LV Status              available
  # open                 0
  LV Size                6.00 GiB
  Current LE             1536
  COW-table size         500.00 MiB
  COW-table LE           125
  Allocated to snapshot  0.00% --此时的使用率为0
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3



我们另外开一个控制台,时刻查看快照的使用大小
[root@yangcan ftp]# watch -n 0.5 "lvdisplay /dev/vg5/snap-ftp |grep %"


我们在开一个控制台,用于增加ftp文件的大小,此刻我们用的是ls的定向输出到文件ls.txt.因为此过程较长,我们我们有充分的时间看Allocated to snapshot大小的变化


[root@yangcan ftp]# (ls -lR / ;ls -lR /)&>./ls.txt



t1时刻--ls还未完成



t2时刻--ls已经完成!!




ls已经完成,我们看一下 Allocated to snapshot 大小的变化


[root@yangcan ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg5/snap-ftp
  VG Name                vg5
  LV UUID                0y8tuc-GdFu-duvd-fSDt-wsLQ-fYWa-q6hOd3
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/vg5/ftp
  LV Status              available
  # open                 0
  LV Size                6.00 GiB
  Current LE             1536
  COW-table size         500.00 MiB
  COW-table LE           125
  Allocated to snapshot  5.86% 
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3




我们在t3时刻DD新建一下文件10M-t1



[root@yangcan ftp]# dd if=/dev/zero of=./10M-t1 bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0276569 s, 379 MB/s




我们在t4时刻DD新建一下文件 10M-t1
[root@yangcan ftp]# dd if=/dev/zero of=./20M-t2 bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0647524 s, 324 MB/s






我们挂载快照,看下里面的文件!
[root@yangcan ftp]# mkdir /snap-ftp
[root@yangcan ftp]# mount /dev/mapper/vg5-snap--ftp /snap-ftp/



对比一下! 
/dev/vg5/ftp挂载目录/ftp的文件!
[root@yangcan ftp]# ll -nt 
total 65512
-rw-r--r--. 1 0 0 20971520 Aug  2 21:35 20M-t2
-rw-r--r--. 1 0 0 10485760 Aug  2 21:31 10M-t1
-rw-r--r--. 1 0 0 30389219 Aug  2 21:26 ls.txt
-rw-r--r--. 1 0 0  4953699 Aug  2 21:18 linux.words
-rw-r--r--. 1 0 0      969 Aug  2 21:18 yum.conf
-rw-r--r--. 1 0 0      585 Aug  2 21:18 yp.conf
-rw-r--r--. 1 0 0     3001 Aug  2 21:18 warnquota.conf
-rw-r--r--. 1 0 0      480 Aug  2 21:18 updatedb.conf
-rw-r--r--. 1 0 0       45 Aug  2 21:18 Trolltech.conf
-rw-r--r--. 1 0 0     1314 Aug  2 21:18 tpvmlp.conf
-rw-r--r--. 1 0 0     1148 Aug  2 21:18 sysctl.conf
-rw-r--r--. 1 0 0      260 Aug  2 21:18 sos.conf
-rw-r--r--. 1 0 0     1272 Aug  2 21:18 smi.conf
-rw-r--r--. 1 0 0     6463 Aug  2 21:18 smartd.conf
-rw-r--r--. 1 0 0      216 Aug  2 21:18 sestatus.conf
-rw-r--r--. 1 0 0     2741 Aug  2 21:18 rsyslog.conf
-rw-r--r--. 1 0 0      103 Aug  2 21:18 resolv.conf
..........................


快照 /dev/mapper/vg5-snap--ftp 挂载的/snap-ftp/
[root@yangcan ftp]# cd /snap-ftp/
[root@yangcan snap-ftp]# ll -nt 
total 5112  --和最初的大小一样
-rw-r--r--. 1 0 0 4953699 Aug  2 21:18 linux.words
-rw-r--r--. 1 0 0     969 Aug  2 21:18 yum.conf
-rw-r--r--. 1 0 0     585 Aug  2 21:18 yp.conf
-rw-r--r--. 1 0 0    3001 Aug  2 21:18 warnquota.conf
-rw-r--r--. 1 0 0     480 Aug  2 21:18 updatedb.conf
-rw-r--r--. 1 0 0      45 Aug  2 21:18 Trolltech.conf
-rw-r--r--. 1 0 0    1314 Aug  2 21:18 tpvmlp.conf
-rw-r--r--. 1 0 0    1148 Aug  2 21:18 sysctl.conf
-rw-r--r--. 1 0 0     260 Aug  2 21:18 sos.conf
-rw-r--r--. 1 0 0    1272 Aug  2 21:18 smi.conf
-rw-r--r--. 1 0 0    6463 Aug  2 21:18 smartd.conf
-rw-r--r--. 1 0 0     216 Aug  2 21:18 sestatus.conf
-rw-r--r--. 1 0 0    2741 Aug  2 21:18 rsyslog.conf
-rw-r--r--. 1 0 0     103 Aug  2 21:18 resolv.conf
-rw-r--r--. 1 0 0    1484 Aug  2 21:18 request-key.conf
...................................


对比一下/ftp  和 /snap-ftp.在 /snap-ftp里面.并没有新增加的20M-t2,10M-t1等文件!这就是快照的作用,保持逻辑卷拍快照的时间点的原始信息!

当我们删除原文件系统的文件时,观察下快照大小的变化
[root@yangcan ftp]# rm -rf ./*
[root@yangcan ftp]# ll
total 0

[root@yangcan ftp]# lvdisplay |grep %
  Allocated to snapshot  11.92%
--没有什么变化,当我们删除数据的时候.真正存放数据的block区块并没有变化,真正变化的是inode节点表的信息被删了




备份-还原数据

[root@yangcan ~]# dump -0u /tmp/ftpdata.bak.dump /snap-ftp

[root@yangcan yum.repos.d]# mkdir /ftpdata
[root@yangcan yum.repos.d]# cd /ftpdata/
[root@yangcan ftpdata]# restore -rf /tmp/ftpdata.bak.dump





云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 容灾备份

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载