1. LVM的快照概述 LVM機制還提供了對LV做快照的功能,以此來獲得檔案系統的狀态一緻性備份。LVM采用寫時複制技術(Copy-On-Write,COW),不用停止服務或将邏輯卷設為隻讀就可以進行備份,使用LVM快照功能既可以獲得一緻備份,又不會影響伺服器的可用性。 LVM采用的寫時複制,是指當LVM快照建立的時候,僅複制原始卷中資料的中繼資料。換句話說,也就是在建立LVM邏輯卷的時候,并不會發生資料的實體複制。再換句話說,複制中繼資料,不複制實體資料,夠清楚了吧?是以快照的建立幾乎是實時的。當原始卷上有寫的操作執行時,快照會跟蹤原始卷中塊的改變,這個時候原始卷上将要改變的資料會在改變之前拷貝到快照預留的空間。
2.建立LVM快照 建立快照實際上也是建立了一個邏輯卷,隻不過該卷的屬性與普通邏輯卷的屬性有些不一樣。下面以/dev/vg6/ftpdata建立一個大小為500M的快照為例,示範快照建立于使用的方法。 首先我們先看一下/dev/vg6/ftpdata裡面的内容,裡面有很多檔案.
前期的一些步驟,我就不多說了
[[email protected] Desktop]# pvcreate /dev/sda5
Writing physical volume data to disk "/dev/sda5"
Physical volume "/dev/sda5" successfully created
[[email protected] Desktop]# vgcreate vg5 /dev/sda5
Volume group "vg5" successfully created
[[email protected] 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
[[email protected] Desktop]# lvcreate -L 6G -n ftp vg5
Logical volume "ftp" created
[[email protected] Desktop]# mkdir /ftp
[[email protected] 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.
[[email protected] /]# mount /dev/mapper/vg5-ftp /ftp/
[[email protected] /]# mount|grep ftp
/dev/mapper/vg5-ftp on /ftp type ext4 (rw)
[[email protected] /]# df -h |grep ftp
/dev/mapper/vg5-ftp 6.0G 140M 5.5G 3% /ftp
[[email protected] /]# cd /ftp/
[[email protected] ftp]# ll
total 16
drwx------. 2 root root 16384 Aug 2 21:12 lost+found
[[email protected] ftp]# cp /etc/*.conf .
[[email protected] ftp]# cp /usr/share/dict/linux.words .
[[email protected] 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
..........................
我們在這個時刻拍一個快照
[[email protected] ~]# lvcreate -L 500M -n snap-ftp -s /dev/vg5/ftp
Logical volume "snap-ftp" created
我們檢視一下邏輯卷和快照的資訊!
[[email protected] ~]# 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
我們另外開一個控制台,時刻檢視快照的使用大小
[[email protected] ftp]# watch -n 0.5 "lvdisplay /dev/vg5/snap-ftp |grep %"
我們在開一個控制台,用于增加ftp檔案的大小,此刻我們用的是ls的定向輸出到檔案ls.txt.因為此過程較長,我們我們有充分的時間看Allocated to snapshot大小的變化
[[email protected] ftp]# (ls -lR / ;ls -lR /)&>./ls.txt
t1時刻--ls還未完成

t2時刻--ls已經完成!!
ls已經完成,我們看一下 Allocated to snapshot 大小的變化
[[email protected] ~]# 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
[[email protected] 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
[[email protected] 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
我們挂載快照,看下裡面的檔案!
[[email protected] ftp]# mkdir /snap-ftp
[[email protected] ftp]# mount /dev/mapper/vg5-snap--ftp /snap-ftp/
對比一下!
/dev/vg5/ftp挂載目錄/ftp的檔案!
[[email protected] 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/
[[email protected] ftp]# cd /snap-ftp/
[[email protected] 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等檔案!這就是快照的作用,保持邏輯卷拍快照的時間點的原始資訊!
當我們删除原檔案系統的檔案時,觀察下快照大小的變化
[[email protected] ftp]# rm -rf ./*
[[email protected] ftp]# ll
total 0
[[email protected] ftp]# lvdisplay |grep %
Allocated to snapshot 11.92%
--沒有什麼變化,當我們删除資料的時候.真正存放資料的block區塊并沒有變化,真正變化的是inode節點表的資訊被删了
備份-還原資料
[[email protected] ~]# dump -0u /tmp/ftpdata.bak.dump /snap-ftp
[[email protected] yum.repos.d]# mkdir /ftpdata
[[email protected] yum.repos.d]# cd /ftpdata/
[[email protected] ftpdata]# restore -rf /tmp/ftpdata.bak.dump