lvm 邏輯卷的快照及備份 還原

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大小的變化node

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


t1時刻--ls還未完成linux



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-t1app

[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
相關文章
相關標籤/搜索