Linux中,使用lvm快照(snapshot)来快速备份数据

Linux中,使用lvm快照(snapshot)来快速备份数据

lv快照的工作原理是建立一个快照区,如果原来的数据发生变化,那么就把变化前的数据放到快照区,借用鸟哥的图:
Linux中,使用lvm快照(snapshot)来快速备份数据_第1张图片
也就是说,原来数据的修改量,不能超过快照区的大小,否则这个快照,就不能用了:

示例:原本有一个1G的LV,mysqllv1,上面的文件系统挂载在目录/root/mysqldata下:
root@db2a:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 961M 4.0K 961M 1% /dev
tmpfs 195M 1.1M 194M 1% /run
/dev/sda1 39G 14G 23G 38% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 972M 0 972M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/mapper/mqsvg1-mysqllv1 976M 957M 0 100% /root/mysqldata

root@db2a:~# lvdisplay
--- Logical volume ---
LV Path /dev/mqsvg1/mysqllv1
LV Name mysqllv1
VG Name mqsvg1
LV UUID K3coIn-e7j3-IWib-wtMx-8JL4-F9oz-jWA1r2
LV Write Access read/write
LV Creation host, time db2a, 2017-08-18 04:44:31 -0700
LV Status available
# open 1
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

root@db2a:~# ls -lh /root/mysqldata
total 955M
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.001
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.002
-rw-r--r-- 1 root root 339M Aug 18 17:07 db2diag.log.003
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.004
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.005
drwxr-xr-x 3 root root 4.0K Aug 18 04:53 logdir
drwx------ 2 root root 4.0K Aug 18 04:45 lost+found

现在在mysqllv1上新建一个lv快照,名子为mysqllvsnap,大小为300M,下面命令中的-s表示是快照,-n表示名子,
root@db2a:~# lvcreate -L 300M -s -n mysqllvsnap /dev/mqsvg1/mysqllv1
Logical volume "mysqllvsnap" created

可以看到,两个lv的大小都是1G,但mysqllvsnap的 COW-table LE为300MB:
root@db2a:~# lvdisplay
--- Logical volume ---
LV Path /dev/mqsvg1/mysqllv1
LV Name mysqllv1
VG Name mqsvg1
LV UUID K3coIn-e7j3-IWib-wtMx-8JL4-F9oz-jWA1r2
LV Write Access read/write
LV Creation host, time db2a, 2017-08-18 04:44:31 -0700
LV snapshot status source of
mysqllvsnap [active]
LV Status available
# open 1
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/mqsvg1/mysqllvsnap
LV Name mysqllvsnap
VG Name mqsvg1
LV UUID vsZQ0A-qo8g-pgZ9-LwT6-O2CA-0qvG-24teLn
LV Write Access read/write
LV Creation host, time db2a, 2017-08-18 17:07:40 -0700
LV snapshot status active destination for mysqllv1
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
COW-table size 300.00 MiB <--快照区实际容量
COW-table LE 75
Allocated to snapshot 0.00% <--快照区已经使用百分比
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

如果把mysqllvsnap挂载到某个目录上,并查看文件内容,会发现和原来的mysqllv1里内容完全一样,
root@db2a:~# mkdir /root/mysqlsnap
root@db2a:~# mount /dev/mqsvg1/mysqllvsnap /root/mysqlsnap/
root@db2a:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 961M 4.0K 961M 1% /dev
tmpfs 195M 1.1M 194M 1% /run
/dev/sda1 39G 14G 23G 38% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 972M 0 972M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/mapper/mqsvg1-mysqllv1 976M 957M 0 100% /root/mysqldata
/dev/mapper/mqsvg1-mysqllvsnap 976M 957M 0 100% /root/mysqlsnap

root@db2a:~# ls -lh /root/mysqldata
total 955M
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.001
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.002
-rw-r--r-- 1 root root 339M Aug 18 17:07 db2diag.log.003
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.004
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.005
drwxr-xr-x 3 root root 4.0K Aug 18 04:53 logdir
drwx------ 2 root root 4.0K Aug 18 04:45 lost+found
root@db2a:~# ls -lh /root/mysqlsnap
total 955M
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.001
-rw-r--r-- 1 root root 170M Aug 18 17:06 db2diag.log.002
-rw-r--r-- 1 root root 339M Aug 18 17:07 db2diag.log.003
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.004
-rw-r--r-- 1 root root 54M Aug 18 17:07 db2diag.log.005
drwxr-xr-x 3 root root 4.0K Aug 18 04:53 logdir
drwx------ 2 root root 4.0K Aug 18 04:45 lost+found

修改mysqllv1里的数据后,mysqllvsnap对应的数据不会发生变化,从而起到备份的作用。但要注意一点,修改的内容不能超过300MB,否则快照区不够用,导致快照失效,下面的例子中,第一次修改了大约170MB的数据,快照区被使用了56.77% (170/300),第二次再次修改了170MB的数据之后,快照失效:
root@db2a:~# cat /root/mysqldata/db2diag.log > /root/mysqldata/db2diag.log.001
root@db2a:~# lvdisplay /dev/mapper/mqsvg1-mysqllvsnap
--- Logical volume ---
LV Path /dev/mqsvg1/mysqllvsnap
LV Name mysqllvsnap
VG Name mqsvg1
LV UUID vsZQ0A-qo8g-pgZ9-LwT6-O2CA-0qvG-24teLn
LV Write Access read/write
LV Creation host, time db2a, 2017-08-18 17:07:40 -0700
LV snapshot status active destination for mysqllv1
LV Status available
# open 1
LV Size 1.00 GiB
Current LE 256
COW-table size 300.00 MiB
COW-table LE 75
Allocated to snapshot 56.77%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

root@db2a:~# cat /root/mysqldata/db2diag.log > /root/mysqldata/db2diag.log.002
root@db2a:~# lvdisplay /dev/mapper/mqsvg1-mysqllvsnap
/dev/mqsvg1/mysqllvsnap: read failed after 0 of 4096 at 1073676288: Input/output error
/dev/mqsvg1/mysqllvsnap: read failed after 0 of 4096 at 1073733632: Input/output error
/dev/mqsvg1/mysqllvsnap: read failed after 0 of 4096 at 0: Input/output error
/dev/mqsvg1/mysqllvsnap: read failed after 0 of 4096 at 4096: Input/output error
--- Logical volume ---
LV Path /dev/mqsvg1/mysqllvsnap
LV Name mysqllvsnap
VG Name mqsvg1
LV UUID vsZQ0A-qo8g-pgZ9-LwT6-O2CA-0qvG-24teLn
LV Write Access read/write
LV Creation host, time db2a, 2017-08-18 17:07:40 -0700
LV snapshot status INACTIVE destination for mysqllv1
LV Status available
# open 1
LV Size 1.00 GiB
Current LE 256
COW-table size 300.00 MiB
COW-table LE 75
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

所以,为了防止快照失效,有两种办法,第一是创建完快照之后,挂载到某个目录,马上将里面的内容备份出来。另一个是,建立一个和原来的lv一样大小或者更大的快照,这样的快照也不会失效。

你可能感兴趣的:(LINUX)