Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了迁移数据至逻辑卷和LVM快照大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

迁移数据至逻辑卷和LVM快照 一、迁移数据至逻辑卷 1.首先创建一个逻辑卷 [[email protected] ~]# pvcreate /dev/sd{B,c} Physical volume "/dev/sdb" successfully created. Physical volume "/dev/sdc" successfully created. [[email protect
迁移数据至逻辑卷和LVM快照

1.首先创建一个逻辑卷

[[email protected] ~]# pvcreate /dev/sd{B,c}
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
[[email protected] ~]# vgcreate testvg /dev/sd{B,c}
  Volume group "testvg" successfully created
[[email protected] ~]# lvcreate -n testlv -L 5G testvg 
  Logical volume "testlv" created.

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

[[email protected] ~]# 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家目录为例进行数据迁移)

[[email protected] ~]# mkdir /testdir
[[email protected] ~]# mount /dev/testvg/testlv /testdir/
[[email protected] ~]# cp -a /home/. /testdir/
[[email protected] ~]# ls /testdir
lost+found  masuri

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

[[email protected] home]# mv /home/* /tmp/
[[email protected] testdir]# umount /testdir/

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

[[email protected] 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
~
~
#挂载设备
[[email protected] home]# mount -a
#查看设备是否挂载
[[email protected] 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中的数据是否存在
[[email protected] home]# cd /home
[[email protected] home]# ls
lost+found  masuri

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

1.创建快照卷

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

[[email protected] home]# lvcreate -n home_snap -s -L 100M /dev/testvg/testlv  Logical volume "home_snap" created.
#查看快照卷是否创建
[[email protected] 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.使用快照对逻辑卷恢复

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

[[email protected] ~]# cd /home
[[email protected] home]# ls
lost+found  masuri
[[email protected] home]# touch file{1..5}
[[email protected] home]# ls
file1  file2  file3  file4  file5  lost+found  masuri

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

[[email protected] ~]# umount /home/
[[email protected] ~]# lvconvert --merge /dev/testvg/home_snap 
  Merging of volume testvg/home_snap started.
  testvg/testlv: Merged: 100.00%

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

[[email protected] ~]# mount -a
[[email protected] ~]# cd /home
[[email protected] home]# ls
lost+found  masuri

大佬总结

以上是大佬教程为你收集整理的迁移数据至逻辑卷和LVM快照全部内容,希望文章能够帮你解决迁移数据至逻辑卷和LVM快照所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。