lvm的备份还原及修改UUID

Posted stau

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lvm的备份还原及修改UUID相关的知识,希望对你有一定的参考价值。

今天在VMware? Workstation 12 Pro虚拟机上克隆了一台Linux虚拟机,无意中发现两台虚拟机blkid一模一样。

[[email protected] ~]# blkid

[[email protected] ~]# blkid

 

有没有什么办法改变blkid里的UUID呢?网上查找了很多关于如何实现改变UUID的方法,都不怎么好使。最后我实际操作,终于找到一条可以真正改变blkid里UUID的方法,实验步骤如下:

1.准备。为了不与其它系统卷组名称相同从而导致无法被挂载启动。这里我先修改逻辑卷和卷组名称

[[email protected] ~]# lvrename /dev/centos/root lvroot

[[email protected] ~]# vgrename centos VolGroup65

 

 

2.把系统关机,查看虚拟磁盘文件名称

[[email protected] ~]# shutdown -h now

 

2.另起一台虚拟机,添加刚刚查看的虚拟磁盘文件

 

3.开机,如果无法开机,使用光盘进入rescue修复模式进行修复

[[email protected] ~]# df -hT

 

正常启动后如图可以看到我们添加虚拟磁盘,此处显示的/dev/sdb就是需要修改blkid里UUID的磁盘。

[[email protected] ~]# ll /dev/sd*

 

 

4.查看当前所有卷组vgdisplay

[[email protected] ~]# vgdisplay

  --- Volume group ---

  VG Name               centos

  System ID            

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  3

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               2

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               12.00 GiB

  PE Size               4.00 MiB

  Total PE              3072

  Alloc PE / Size       3072 / 12.00 GiB

  Free  PE / Size       0 / 0  

  VG UUID               d5xxJl-gMyV-Y1Gj-v57D-fDqY-GvWL-ih6DaL

  

  --- Volume group ---

  VG Name               VolGroup65

  System ID            

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  7

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                2

  Open LV               0

  Max PV                0

  Cur PV                2

  Act PV                2

  VG Size               21.99 GiB

  PE Size               4.00 MiB

  Total PE              5630

  Alloc PE / Size       5630 / 21.99 GiB

  Free  PE / Size       0 / 0  

  VG UUID               b4If34-7E1r-rvMN-WpKE-2Kmg-22Ts-auTm1i

查看uuid

[[email protected] ~]# blkid

 

5.如果卷组没有激活,需要先激活一下

[[email protected] ~]# vgchange -ay VolGroup65

 

-a: activate

-y: yes 激活

-n: no 不激活

6.生成新UUID

  6.1XFS系统:

  6.1.1改变根分区UUID。

    要改变根分区UUID,因为系统采用的是lvm逻辑卷管理,我暂未发现针对lvm类型修改UUID的命令与方法。所以这里我采用备份根分区然后还原的方式。步骤如下:

a.查看卷组剩余空间,发现没有剩余PE

[[email protected] ~]# vgdisplay VolGroup65

 

b.查看磁盘空间

[[email protected] ~]# fdisk -l /dev/sdb

 

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0xc0412435

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     1026047      512000   83  Linux

/dev/sdb2         1026048     5220351     2097152   83  Linux

/dev/sdb3         5220352    47163391    20971520   83  Linux

说明:/dev/sdb1 为引导分区

         /dev/sdb2 为swap分区

         /dev/sdb3 才是我要备份的根分区

c.因为我怕空间不足,所以这里我把swap分区移除掉。(数据无价,删除任何文件前请确认已经备份好重要数据。删除前需要先取消挂载。)

c.1确认操作的分区没有被挂载

[[email protected] ~]# df -hT

Filesystem              Type      Size  Used Avail Use% Mounted on

/dev/mapper/centos-root xfs        10G  7.0G  3.1G  70% /

devtmpfs                devtmpfs  224M     0  224M   0% /dev

tmpfs                   tmpfs     236M     0  236M   0% /dev/shm

tmpfs                   tmpfs     236M  5.7M  230M   3% /run

tmpfs                   tmpfs     236M     0  236M   0% /sys/fs/cgroup

/dev/sr0                iso9660   8.8G  8.8G     0 100% /mnt/cdrom

/dev/sda1               xfs       197M  108M   90M  55% /boot

tmpfs                   tmpfs      48M     0   48M   0% /run/user/0

c.2移除swap分区

[[email protected] ~]# lvremove /dev/VolGroup65/swap

Do you really want to remove active logical volume VolGroup65/swap? [y/n]: y

  Logical volume "swap" successfully removed

[[email protected] ~]# vgreduce VolGroup65 /dev/sdb2

  Removed "/dev/sdb2" from volume group "VolGroup65"

[[email protected] ~]# pvremove /dev/sdb2

  Labels on physical volume "/dev/sdb2" successfully wiped.

d.新建backup-lv,备份根分区

d.1新建磁盘分区。把剩余空间新建一个磁盘分区

[[email protected] ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

 

 

Command (m for help): p

 

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0xc0412435

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048     1026047      512000   83  Linux

/dev/sdb2         1026048     5220351     2097152   83  Linux

/dev/sdb3         5220352    47163391    20971520   83  Linux

 

Command (m for help): n

Partition type:

   p   primary (3 primary, 0 extended, 1 free)

   e   extended

Select (default e): e

Selected partition 4

First sector (47163392-83886079, default 47163392):

Using default value 47163392

Last sector, +sectors or +size{K,M,G} (47163392-83886079, default 83886079):

Using default value 83886079

Partition 4 of type Extended and of size 17.5 GiB is set

 

Command (m for help): n

All primary partitions are in use

Adding logical partition 5

First sector (47165440-83886079, default 47165440):

Using default value 47165440

Last sector, +sectors or +size{K,M,G} (47165440-83886079, default 83886079):

Using default value 83886079

Partition 5 of type Linux and of size 17.5 GiB is set

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

d.2更新磁盘信息

[[email protected] ~]# partprobe /dev/sdb

[[email protected] ~]# ll /dev/sdb*

brw-rw---- 1 root disk 8, 16 Feb  4 17:28 /dev/sdb

brw-rw---- 1 root disk 8, 17 Feb  4 17:28 /dev/sdb1

brw-rw---- 1 root disk 8, 18 Feb  4 17:28 /dev/sdb2

brw-rw---- 1 root disk 8, 19 Feb  4 17:28 /dev/sdb3

brw-rw---- 1 root disk 8, 20 Feb  4 17:28 /dev/sdb4

brw-rw---- 1 root disk 8, 21 Feb  4 17:28 /dev/sdb5

d.3新建pv,扩展vg,并查看vg信息

[[email protected] ~]# pvcreate /dev/sdb5

  Physical volume "/dev/sdb5" successfully created.

[[email protected] ~]# vgextend VolGroup65 /dev/sdb5

  Volume group "VolGroup65" successfully extended

[[email protected] ~]# vgdisplay VolGroup65

  --- Volume group ---

  VG Name               VolGroup65

  System ID            

  Format                lvm2

  Metadata Areas        3

  Metadata Sequence No  7

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               0

  Max PV                0

  Cur PV                3

  Act PV                3

  VG Size               39.50 GiB

  PE Size               4.00 MiB

  Total PE              10112

  Alloc PE / Size       5118 / 19.99 GiB

  Free  PE / Size       4994 / <19.51 GiB

  VG UUID               b4If34-7E1r-rvMN-WpKE-2Kmg-22Ts-auTm1i

d.4新建备份lv

[[email protected] ~]# lvcreate -n backup-lv -l +100%Free VolGroup65

  Logical volume "backup-lv" created.

d.5格式化lv

[[email protected] ~]# mkfs.xfs /dev/VolGroup65/backup-lv

meta-data=/dev/VolGroup65/backup-lv isize=512    agcount=4, agsize=1278464 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=5113856, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=2560, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

d.6挂载

[[email protected] ~]# mkdir -p /mnt/VolGroup65/lvroot /mnt/VolGroup65/backup-lv

[[email protected] ~]# mount /dev/mapper/VolGroup65-lvroot /mnt/VolGroup65/lvroot

[[email protected] ~]# mount /dev/VolGroup65/backup-lv /mnt/VolGroup65/backup-lv

 

e.备份源lv逻辑卷。此处我使用rsync工具,/boot引导分区因为我需要重建,所以我备份排除此目录下文件。(备份工具有很多,大家可以择优选择。因为我需要改变UUID的值,所以我使用最方便的复制工具rsync.)

[[email protected] ~]# rsync -avz /mnt/VolGroup65/lvroot /mnt/VolGroup65/backup-lv --exclude=media/* --exclude=sys/* --exclude=proc/* --exclude=mnt/* --exclude=tmp/* --exclude=boot/*

 

f.移除根分区

[[email protected] ~]# umount /dev/mapper/VolGroup65-lvroot

[[email protected] ~]# lvremove /dev/mapper/VolGroup65-lvroot

Do you really want to remove active logical volume VolGroup65/lvroot? [y/n]: y

  Logical volume "lvroot" successfully removed

[[email protected] ~]# vgreduce VolGroup65 /dev/sdb3

  Removed "/dev/sdb3" from volume group "VolGroup65"

[[email protected] ~]# pvremove /dev/sdb3

  Labels on physical volume "/dev/sdb3" successfully wiped.

注:如果vgreduce如下失败,需要先pvmove走

[[email protected] ~]# vgreduce VolGroup65 /dev/sdb3

  Physical volume "/dev/sdb3" still in use

可以移动pvmove到另一磁盘,后者Free PE需要大于前者

[[email protected] ~]# pvmove /dev/sdb3 /dev/sda3

  /dev/sdb3: Moved: 0.00%

  /dev/sdb3: Moved: 14.65%

  /dev/sdb3: Moved: 30.27%

  /dev/sdb3: Moved: 75.20%

  /dev/sdb3: Moved: 100.00%

到此除了包含备份文件的/dev/sda5,根分区已经备份和移走了,其它分区可以重新分区或者格式化了。

 

g.重建根分区及还原数据

  我选择了重新分区,分区后别忘了partprobe /dev/sdb刷新下分区表

[[email protected] ~]# partprobe /dev/sda

[[email protected] ~]# partprobe /dev/sdb

[[email protected] ~]# ll /dev/sdb*

brw-rw---- 1 root disk 8, 16 Feb  6 16:36 /dev/sdb

brw-rw---- 1 root disk 8, 17 Feb  6 16:36 /dev/sdb1

brw-rw---- 1 root disk 8, 18 Feb  6 16:36 /dev/sdb2

brw-rw---- 1 root disk 8, 19 Feb  6 16:36 /dev/sdb3

brw-rw---- 1 root disk 8, 20 Feb  6 16:36 /dev/sdb4

brw-rw---- 1 root disk 8, 21 Feb  6 16:36 /dev/sdb5

 

  g.1新建PV和新建vg

[[email protected] ~]# pvcreate /dev/sdb2 /dev/sdb3

  Physical volume "/dev/sdb2" successfully created.

  Physical volume "/dev/sdb3" successfully created.

[[email protected] ~]# vgcreate VG65 /dev/sdb{2,3}

  Volume group "VG65" successfully created

  g.2新建lv

[[email protected] ~]# lvcreate -L 2G -n swap VG65

  Logical volume "swap" created.

[[email protected] ~]# lvcreate -l 100%Free -n lvroot VG65

  Logical volume "lvroot" created.

  g.4格式化

  引导分区因为必须为标准分区格式,所以直接格式化就好

[[email protected] ~]# mkfs.xfs /dev/sdb1

meta-data=/dev/sdb1              isize=512    agcount=4, agsize=19200 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=76800, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=855, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

格式化lv

[[email protected] ~]# mkfs.xfs /dev/mapper/VG65-lvroot

meta-data=/dev/mapper/VG65-lvroot isize=512    agcount=4, agsize=1323008 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

data     =                       bsize=4096   blocks=5292032, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal log           bsize=4096   blocks=2584, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

格式化swap分区

[[email protected] ~]# mkswap /dev/mapper/VG65-swap

Setting up swapspace version 1, size = 2097148 KiB

no label, UUID=2ab4424a-e95d-489a-96f1-c967bc6a7c0c

 

  g.5挂载并还原数据

[[email protected] ~]# mkdir /mnt/vg65

[[email protected] ~]# mount /dev/VG65/lvroot /mnt/vg65

[[email protected] ~]# rsync -avz /mnt/VolGroup65/backup-lv/lvroot/* /mnt/vg65/ --exclude=media/* --exclude=sys/* --exclude=proc/* --exclude=mnt/* --exclude=tmp/* --exclude=boot/*

  e.修改/mnt/vg65/etc/fstab

  e.1查看新UUID

[[email protected] vg65]# blkid

/dev/sda1: UUID="a8b7ce70-f1c1-4f73-b13f-d5daf168096f" TYPE="xfs"

/dev/sda2: UUID="kjXpdZ-5Rpy-7tg0-KqiV-9FRg-0OXX-q37J3B" TYPE="LVM2_member"

/dev/sdb1: UUID="ccb49fe1-c127-4e1c-ae0d-996cc17a624e" TYPE="xfs"

/dev/sdb3: UUID="vgZOV0-I24e-xRvb-GIdH-Q5o9-v7Fq-beBFYw" TYPE="LVM2_member"

/dev/sr0: UUID="2018-05-07-12-53-47-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"

/dev/mapper/centos-root: UUID="7041be13-c88f-4221-b5c2-6d3e55a1c3f2" TYPE="xfs"

/dev/mapper/centos-swap: UUID="0562ef7b-0b3c-4da5-8414-1d13d44dc7f0" TYPE="swap"

/dev/sdb5: UUID="Yfucop-iXE0-vIQh-L0QJ-AUY4-8uAK-FASfAj" TYPE="LVM2_member"

/dev/mapper/VolGroup65-backup--lv: UUID="8a8fa0d6-7de0-44a5-a24d-5b97ef7a89b7" TYPE="xfs"

/dev/sda3: UUID="vRTH1r-wMfx-htsk-wQoe-fGLB-2169-nftTLt" TYPE="LVM2_member"

/dev/sdb2: UUID="1NKilr-tNwd-US1g-I1k1-1ATI-bFkg-0dtjWm" TYPE="LVM2_member"

/dev/mapper/VG65-lvroot: UUID="8d661a87-3eee-4025-9714-064774cada00" TYPE="xfs"

/dev/mapper/VG65-swap: UUID="2ab4424a-e95d-489a-96f1-c967bc6a7c0c" TYPE="swap"

e.2修改挂载文件

[[email protected] vg65]# vim etc/fstab

 

#

# /etc/fstab

# Created by anaconda on Sat Dec 15 20:32:13 2018

#

# 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

#

/dev/mapper/VG65-lvroot                   /                       xfs     defaults        0 0

UUID=ccb49fe1-c127-4e1c-ae0d-996cc17a624e /boot                   xfs     defaults        0 0

/dev/mapper/VG65-swap                     swap                    swap    defaults        0 0

UUID=2018-05-07-12-53-47-00               /mnt/cdrom              iso9660 defaults        0 0

f.修复grub2

  f.1挂载/dev/sdb1到/boot分区

[[email protected] ~]# mount /dev/sdb1 /boot

  f.2 重新安装内核

[[email protected] ~]# rpm -ivh /mnt/cdrom/Packages/kernel-3.10.0-862.el7.x86_64.rpm --force

Preparing...                          ################################# [100%]

Updating / installing...

   1:kernel-3.10.0-862.el7            ################################# [100%]

  f.3 grub2安装

[[email protected] ~]# grub2-install /dev/sdb

Installing for i386-pc platform.

Installation finished. No error reported.

  f.4 grub配置文件生成

[[email protected] ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

 g.完成。

  把63关机,65开机。可能有多个菜单可以vim /boot/grub2/grub.cfg删除多余的菜单

 

 h. 其它

一些可以生成新UUID的方法

  XFS文件系统

 

  EXT文件系统

       [[email protected] ~]# uuidgen |xargs -i tune2fs -U {} /dev/mapper/VolGroup65-lvroot

 

[[email protected] ~]# pvchange -u /dev/sda3 #为pv(/dev/sda3)随机产生一个新的uuid号

[[email protected] ~]# vgchange -u test       #为vg(test) 随机产生一个新的uuid号

修复XFS文件系统

 

以上是关于lvm的备份还原及修改UUID的主要内容,如果未能解决你的问题,请参考以下文章

[原创]MySQL数据库查询和LVM备份还原学习笔记记录

基于逻辑卷LVM的MySQLmariadb数据库备份还原详细实现

实现LVM逻辑卷数据库备份还原,二进制日志文件分离存放

mysql数据库-备份与还原实操

Linux系统备份还原工具1(DD)(应用实例)

MongoDB数据备份还原,及docker中MongoDB备份还原