Start
例如我们要对卷组"test_vg"每晚进行数据库备份,就要采用snapshot类型的卷组。这种卷组是其它卷组的一个只读拷贝,它含有在创建snapshot卷组时原卷组的所有数据,这意味你可以备份这个卷组而不用担心在备份过程中数据会改变,也不需要暂时关闭数据库卷以备份。
6.1 建立snapshot卷
一个snapshot卷可大可小,但必须有足够的空间存放所有在本snapshot卷生存期间改变的数据,一般最大要求是原卷组的1.1倍。如空间不够,snapshot卷将不能使用。
# lvcreate -L592M -s -n dbbackup /dev/test_vg/databases
lvcreate -- WARNING: the snapshot must be disabled if it gets full
lvcreate -- INFO: using default snapshot chunk size of 64 KB for "/dev/test_vg/dbbackup"
lvcreate -- doing automatic backup of "test_vg"
lvcreate -- logical volume "/dev/test_vg/dbbackup" successfully created
6.2 安装snapshot卷
现在可以安装该卷:
# mkdir /mnt/test_vg/dbbackup
# mount /dev/test_vg/dbbackup /mnt/test_vg/dbbackup
mount: block device /dev/test_vg/dbbackup is write-protected, mounting read-only
从上面可以看出,snapshot卷是只读的。
当使用XFS文件系统时,mount命令要使用nouuid与norecovery选项:
# mount /dev/test_vg/dbbackup /mnt/test_vg/dbbackup -o nouuid,norecovery,ro
6.3 备份数据
如采用tar向磁带备份:
# tar -cf /dev/rmt0 /mnt/test_vg/dbbackup
6.4 删除snapshot 卷
在完成备份后,就可卸载并删除snapshot卷。
# umount /mnt/test_vg/dbbackup
# lvremove /dev/test_vg/dbbackup
lvremove -- do you really want to remove "/dev/test_vg/dbbackup"? [y/n]: y
lvremove -- doing automatic backup of volume group "test_vg"
lvremove -- logical volume "/dev/test_vg/dbbackup" successfully removed
7 更换卷组硬盘
由于某种原因,需要用新的硬盘替代卷组中的旧硬盘,如用一SCSI硬盘替换IDE硬盘,其步骤为:
7.1 准备/初始化新硬盘
首先用pvcreate命令初始化新的硬盘,如使用整个硬盘:
# pvcreate /dev/sdf
pvcreate -- physical volume "/dev/sdf" successfully created
7.2 加入卷组
把新硬盘加入卷组:
# vgextend test_vg /dev/sdf
vgextend -- INFO: maximum logical volume size is 255.99 Gigabyte
vgextend -- doing automatic backup of volume group "test_vg"
vgextend -- volume group "test_vg" successfully extended
7.3 数据搬家
在移除旧硬盘前,要把其上的数据转移到新硬盘上。在转移数据时,不要求卸载文件系统,但建议在数据转移前进行备份,以防转移进程中意外导致数据丢失。
pvmove用来实现数据转移,根据数据量的多少,它可能要使用大量的时间,并可降低逻辑卷的性能,因此要在系统不太忙时操作。
# pvmove /dev/hdb /dev/sdf
pvmove -- moving physical extents in active volume group "test_vg"
pvmove -- WARNING: moving of active logical volumes may cause data loss!
pvmove -- do you want to continue? [y/n] y
pvmove -- 249 extents of physical volume "/dev/hdb" successfully moved
7.4 移除未用硬盘
当数据被转移到其它硬盘后,就可以从卷组中删除这块不再使用的硬盘:
# vgreduce dev /dev/hdb
vgreduce -- doing automatic backup of volume group "test_vg"
vgreduce -- volume group "test_vg" successfully reduced by physical volume:
vgreduce -- /dev/hdb
从此,卷组test_vg不再使用IDE硬盘/dev/hdb,这块硬盘可以从机器中拆下或用作它途。
8 迁移卷组到其它系统
把一个卷组转移到其它系统是很容易的(如更换服务器),这要用命令vgexport与vgimport。
8.1 卸载文件系统
为整体搬迁卷组,应首先把它从文件系统中卸载,如:
# unmount /mnt/design/users
8.2 设置卷组为非活动状态
把卷组从内核中卸载,以避免任何对它可能的操作:
# vgchange -an test_vg
vgchange -- volume group "test_vg" successfully deactivated
8.3 Export 卷组
这个操作不是必须的,便它可以防止系统对卷组的访问:
# vgexport test_vg
vgexport -- volume group "test_vg" sucessfully exported
当机器关机后,构成卷组的硬盘就可被转移到新的服务器上。
8.4 Import 卷组
在新的服务器上,可用pvscan查看卷组情况,如在这台计算机上,硬盘新的设备为/dev/sdb,使用pvscan可有:
# pvscan
pvscan -- reading all physical volumes (this may take a while...)
pvscan -- inactive PV "/dev/sdb1" is in EXPORTED VG "test_vg" [996 MB / 996 MB free]
pvscan -- inactive PV "/dev/sdb2" is in EXPORTED VG "test_vg" [996 MB / 244 MB free]
pvscan -- total: 2 [1.95 GB] / in use: 2 [1.95 GB] / in no VG: 0 [0]
现可以import卷组test_vg (同时也激活它)以安装其上的文件系统
# vgimport test_vg /dev/sdb1 /dev/sdb2
vgimport -- doing automatic backup of volume group "test_vg"
vgimport -- volume group "test_vg" successfully imported and activated
8.5 安装文件系统
# mkdir -p /mnt/design/users
# mount /dev/test_vg/users /mnt/design/users
在完成以上操作后,原卷组在新的服务器上就可使用了。
9 分割卷组
这种情况是:需要在系统中加入新的卷组,但没有其它可用新硬盘,而已有的卷组中还有大量空间可用。如向系统加入一个"design"卷组。
9.1 检查可用空间
# pvscan
pvscan -- reading all physical volumes (this may take a while...)
pvscan -- ACTIVE PV "/dev/sda" of VG "dev" [1.95 GB / 0 free]
pvscan -- ACTIVE PV "/dev/sdb" of VG "sales" [1.95 GB / 1.27 GB free]
pvscan -- ACTIVE PV "/dev/sdc" of VG "ops" [1.95 GB / 564 MB free]
pvscan -- ACTIVE PV "/dev/sdd" of VG "dev" [1.95 GB / 0 free]
pvscan -- ACTIVE PV "/dev/sde" of VG "ops" [1.95 GB / 1.9 GB free]
pvscan -- ACTIVE PV "/dev/sdf" of VG "dev" [1.95 GB / 1.33 GB free]
pvscan -- ACTIVE PV "/dev/sdg1" of VG "ops" [996 MB / 432 MB free]
pvscan -- ACTIVE PV "/dev/sdg2" of VG "dev" [996 MB / 632 MB free]
pvscan -- total: 8 [13.67 GB] / in use: 8 [13.67 GB] / in no VG: 0 [0]
我们决定把/dev/sdg1与/dev/sdg2分配组design,但首先要把其上的物理块移到其它卷的空闲空间中(如把卷组dev移到/dev/sdf,卷组ops移到/dev/sde)。
9.2 从选定硬盘移出数据
由于硬盘上的逻辑卷仍在使用,故首先要转移它们的数据。
把所有在使用的物理块从/dev/sdg1上转移到/dev/sde,及从/dev/sdg2转移到/dev/sdf。
# pvmove /dev/sdg1 /dev/sde
pvmove -- moving physical extents in active volume group "ops"
pvmove -- WARNING: moving of active logical volumes may cause data loss!
pvmove -- do you want to continue? [y/n] y
pvmove -- doing automatic backup of volume group "ops"
pvmove -- 141 extents of physical volume "/dev/sdg1" successfully moved
# pvmove /dev/sdg2 /dev/sdf
pvmove -- moving physical extents in active volume group "dev"
pvmove -- WARNING: moving of active logical volumes may cause data loss!
pvmove -- do you want to continue? [y/n] y
pvmove -- doing automatic backup of volume group "dev"
pvmove -- 91 extents of physical volume "/dev/sdg2" successfully moved
9.3 创建新卷组
现在把/dev/sdg2从卷组dev从分割出并加入到新卷组design中。我们可用vgreduce与vgcreate完成工作,但vgsplit此时更方便:
# vgsplit dev design /dev/sdg2
vgsplit -- doing automatic backup of volume group "dev"