迁移数据至逻辑卷和LVM快照

迁移数据至逻辑卷和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

猜你喜欢

转载自blog.51cto.com/11886307/2370287