linux 逻辑卷跨卷组迁移,迁移数据至逻辑卷和LVM快照

一、迁移数据至逻辑卷

1.首先创建一个逻辑卷

[root@centos7 ~]# pvcreate /dev/sd{b,c}

Physical volume "/dev/sdb" successfully created.

Physical volume "/dev/sdc" successfully created.

[root@centos7 ~]# vgcreate testvg /dev/sd{b,c}

Volume group "testvg" successfully created

[root@centos7 ~]# lvcreate -n testlv -L 5G testvg

Logical volume "testlv" created.

2.将磁盘格式化为ext4分区格式

[root@centos7 ~]# mkfs.ext4 /dev/testvg/testlv

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

327680 inodes, 1310720 blocks

65536 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1342177280

40 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

Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

3.挂载逻辑卷设备至临时目录,并复制需要迁移的数据。(以下以home家目录为例进行数据迁移)

[root@centos7 ~]# mkdir /testdir

[root@centos7 ~]# mount /dev/testvg/testlv /testdir/

[root@centos7 ~]# cp -a /home/. /testdir/

[root@centos7 ~]# ls /testdir

lost+found masuri

4.卸载设备,移除原家目录下的内容。

[root@centos7 home]# mv /home/* /tmp/

[root@centos7 testdir]# umount /testdir/

5.将lvm设备挂载至/home完成数据迁移,由于用户家目录再启动时需要开机时挂载所以此处需要将其写入配置文件,并挂载

[root@centos7 home]# vim /etc/fstab

#

# /etc/fstab

# Created by anaconda on Tue Mar 5 21:07:19 2019

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=45490aa4-cf29-420d-a606-af32688b6707 / xfs defaults 0 0

UUID=15dcd896-b7cf-48d0-b8bd-4c0b0f2c62b2 /boot xfs defaults 0 0

UUID=4b6e1813-2c46-402a-869a-02cbbcb76ade /data xfs defaults 0 0

UUID=0995b444-48c1-4423-92bc-2deda0d3c082 swap swap defaults 0 0

UUID=a3fa2d53-91c4-4af5-9ee4-c63500dbaaf2 /home ext4 defaults 0 0

~

~

#挂载设备

[root@centos7 home]# mount -a

#查看设备是否挂载

[root@centos7 home]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 200G 0 disk

├─sda1 8:1 0 1G 0 part /boot

├─sda2 8:2 0 100G 0 part /

├─sda3 8:3 0 50G 0 part /data

├─sda4 8:4 0 1K 0 part

└─sda5 8:5 0 2G 0 part [SWAP]

sdb 8:16 0 20G 0 disk

└─testvg-testlv 253:0 0 5G 0 lvm /home

sdc 8:32 0 20G 0 disk

sr0 11:0 1 10G 0 rom

#查看lvm中的数据是否存在

[root@centos7 home]# cd /home

[root@centos7 home]# ls

lost+found masuri

二、lvm快照

lvm快照的原理是在和需要拍摄快照的逻辑卷的同一卷组上创建一个空白逻辑卷并标识当前的时间,当原逻辑卷内的某数据发生改变时,首先会在此数据未发生改变前复制一份至快照卷。当需要用到快照时,系统会将快照卷内的数据全部复制回逻辑卷,并将逻辑卷内拍摄快后建立的数据全部删除。快照卷的大小一般为逻辑卷内数据的大小,过大无意义,过小可能会造成数据的丢失。

1.创建快照卷

此处以刚才创建的逻辑卷/dev/testvg/testlv为例,创建快照

[root@centos7 home]# lvcreate -n home_snap -s -L 100M /dev/testvg/testlv

Logical volume "home_snap" created.

#查看快照卷是否创建

[root@centos7 home]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

home_snap testvg swi-a-s--- 100.00m testlv 0.01

testlv testvg owi-aos--- 5.00g

2.使用快照对逻辑卷恢复

在对逻辑卷恢复之前先将逻辑卷内的数据进行一些修改

[root@centos7 ~]# cd /home

[root@centos7 home]# ls

lost+found masuri

[root@centos7 home]# touch file{1..5}

[root@centos7 home]# ls

file1 file2 file3 file4 file5 lost+found masuri

恢复快照,恢复快照前需要将逻辑卷卸载

[root@centos7 ~]# umount /home/

[root@centos7 ~]# lvconvert --merge /dev/testvg/home_snap

Merging of volume testvg/home_snap started.

testvg/testlv: Merged: 100.00%

挂载逻辑卷查看数据是否恢复

[root@centos7 ~]# mount -a

[root@centos7 ~]# cd /home

[root@centos7 home]# ls

lost+found masuri

声明:本文采用 BY-NC-SA 协议进行授权,如无注明均为原创,转载请注明转自菩提树下叶撕阳。

本文地址:迁移数据至逻辑卷和LVM快照

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值