1. 程式人生 > >遷移數據至邏輯卷和LVM快照

遷移數據至邏輯卷和LVM快照

format eating back lin attr ash ren ntp type

遷移數據至邏輯卷和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

遷移數據至邏輯卷和LVM快照