LVM快照
LVM的快照就是把逻辑卷中的数据全部备份了一份出来,修改原来的数据,并不会影响快照里面的数据。且备份和还原的速度非常快,非常实用于数据量很大的情况下进行备份操作
1、逻辑卷管理器快照
1、快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝
2、对于需要备份或者复制的现有数据临时拷贝以及其它操作来说,快照是最合适的选择
3、创建的前提,快照和逻辑卷在同一个卷组中, 在生成快照时会分配给它一定的空间,快照中的数据是逻辑卷的原始数据,当快照还原后,快照的生命周期结束;建立快照的大小应小于等于原始逻辑卷,也可以使用lvextend扩展快照
4、快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享
5、由于快照区与原本的LV共用很多PE的区块,因此快照与被快照的LV必须在同一个VG中.系统恢复的时候的文件数量不能高于快照区的实际容量
2、LVM xfs快照
1、完成逻辑卷
[root@centos_8 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@centos_8 ~]# vgcreate vg0 /dev/sdc
Volume group "vg0" successfully created
[root@centos_8 ~]# lvcreate -n mysql -L +19G vg0
Logical volume "mysql" created.
2、格式化
[root@centos_8 ~]# mkfs.xfs /dev/vg0/mysql
meta-data=/dev/vg0/mysql isize=512 agcount=4, agsize=1245184 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=4980736, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
3、挂载逻辑卷mysql
[root@centos_8 ~]# mount /dev/vg0/mysql /mnt/mysql
#放点数据进去:::::原始数据
[root@centos_8 mysql]# ll
total 8
-rw-r--r-- 1 root root 595 Dec 15 20:21 fstab
-rw-r--r-- 1 root root 2189 Dec 15 20:21 passwd
4、创建xfs快照
[root@centos_8 mysql]# lvcreate -n mysql-snapshot -s -L 1G /dev/vg0/mysql
#-s:表示是一个逻辑卷快照
#-n:表示快照的名字
LV Path /dev/vg0/mysql-snapshot
LV Name mysql-snapshot
VG Name vg0
LV UUID C59woK-e7ri-EgIW-PfKi-Eq01-w3CC-ihHJLB
LV Write Access read/write
LV Creation host, time centos_8, 2021-12-15 20:25:18 +0800
LV snapshot status active destination for mysql
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
COW-table size 1.00 GiB
COW-table LE 256
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
5、挂载快照
[root@centos_8 mysql]# mount -o nouuid /dev/vg0/mysql-snapshot /mnt/snap
#注意事项:在linux系统中是不允许相同的ID进行同时挂载的,所有加上个nouuid表示忽略UUID
#[root@centos_8 mysql]# blkid
#/dev/mapper/vg0-mysql: UUID="d3caf992-7ed0-4df6-a952-7b50af2d178f" TYPE="xfs"
#/dev/mapper/vg0-mysql--snapshot: UUID="d3caf992-7ed0-4df6-a952-7b50af2d178f" TYPE="xfs"
6、查看快照
[root@centos_8 mysql]# ll /mnt/snap/ 都是旧的数据
total 8
-rw-r--r-- 1 root root 595 Dec 15 20:21 fstab
-rw-r--r-- 1 root root 2189 Dec 15 20:21 passwd
7、开始删除、新增、修改/mnt/mysql中的数据
[root@centos_8 mysql]# ll
total 8
-rw-r--r-- 1 root root 0 Dec 15 20:40 f1
-rw-r--r-- 1 root root 0 Dec 15 20:41 f3
-rw-r--r-- 1 root root 94 Dec 15 20:41 fstab
-rw-r--r-- 1 root root 371 Dec 15 20:41 passwd
8、还原
#1、取消快照和逻辑卷的挂载
[root@centos_8 ~]# umount /mnt/mysql/
[root@centos_8 ~]# umount /mnt/snap/
#2、利用lvconvert进行合并也就是还原
[root@centos_8 ~]# lvconvert --merge /dev/vg0/mysql-snapshot {/dev/vg0/mysql 加不加都可以,因为它自己找的到}
Merging of volume vg0/mysql-snapshot started.
vg0/mysql: Merged: 100.00%
#3、挂载逻辑卷
[root@centos_8 ~]# mount /dev/vg0/mysql /mnt/mysql/
[root@centos_8 ~]# ll /mnt/mysql/
total 8
-rw-r--r-- 1 root root 595 Dec 15 20:21 fstab
-rw-r--r-- 1 root root 2189 Dec 15 20:21 passwd
#恢复到了最初状态
9、一旦恢复数据,快照的生命周期结束,
[root@centos_8 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mysql vg0 -wi-ao---- 15.00g
#已经没有快照在逻辑卷里面了
3、如果要创建ext系列的快照,只需要在格式化文件系统的时候,格式化为ext系列的就行,其他一样