在生产系统中使用LVM务必要注意lvm metadata的备份,曾经在京东的时候发生过虚机用了lvm的磁盘系统,然后虚机文件qcow2突然坏掉了,想尽了办法也无法恢复,这给我们拉响了警钟啊,使用lvm的时候务必要备份分区信息,否则坏的时候就欲哭无泪了。
缺省在 /etc/lvm/backup/ 目录下是最新的备份,同样历史版本都在 /etc/lvm/archive/ 目录下。
一、先备份
在开始恢复之前,一定先做个备份
1cp -pr /etc/lvm /etc/lvm_bkp
然后去看看archive下的备份信息
1ls -l /etc/lvm/archive/vg_storage_00*
2-rw-------. 1 root root 13722 Oct 28 23:45 /etc/lvm/archive/vg_storage_00419-1760023262.vg
3-rw-------. 1 root root 14571 Oct 28 23:52 /etc/lvm/archive/vg_storage_00420-94024216.vg
4...
5-rw-------. 1 root root 14749 Nov 23 15:11 /etc/lvm/archive/vg_storage_00676-394223172.vg
6-rw-------. 1 root root 14733 Nov 23 15:29 /etc/lvm/archive/vg_storage_00677-187019982.vg
7#
二、最坏的情形恢复pv
警告:这一步只能在VG无法正常运行的时候再运行!!!
先说最坏的情形,LVM的建立路径是 pv —> vg —> lv,假设连 pv(物理卷都没了)
我们选择最近的备份,/etc/lvm/archive/vg_storage_00677-187019982.vg
1less /etc/lvm/archive/vg_storage_00677-187019982.vg
2...
3 physical_volumes {
4 pv0 {
5 id = "BgR0KJ-JClh-T2gS-k6yK-9RGn-B8Ls-LYPQP0"
6...
从里面拿到 pv 的 id ,重建 pv
1pvcreate --uuid "BgR0KJ-JClh-T2gS-k6yK-9RGn-B8Ls-LYPQP0" \
2 --restorefile /etc/lvm/archive/vg_storage_00677-187019982.vg
三、恢复VG
如果 vg 是正常的,那么就不用做第二步了,直接从备份中恢复即可,先查看一下
1vgcfgrestore --list vg1
2
3File: /etc/lvm/archive/vg1_00000-1238318622.vg
4VG name: vg1
5Description: Created *before* executing 'vgcreate vg1 /dev/sda6'
6Backup Time: Mon Feb 29 10:58:51 2016
7
8File: /etc/lvm/archive/vg1_00001-285796155.vg
9VG name: vg1
10Description: Created *before* executing 'lvcreate -L 1G -n lv2 vg1'
11Backup Time: Mon Feb 29 10:59:23 2016
12
13File: /etc/lvm/archive/vg1_00002-1661997476.vg ---> just before removal of volume (this is the archive we need)
14VG name: vg1
15Description: Created *before* executing 'lvremove /dev/vg1/lv2'
16Backup Time: Mon Feb 29 13:55:08 2016
17
18File: /etc/lvm/backup/vg1
19VG name: vg1
20Description: Created *after* executing 'lvremove /dev/vg1/lv2'
21Backup Time: Mon Feb 29 13:55:08 2016
有信息,我们先加 –test 测试一下
1vgcfgrestore vg01 --test -f /etc/lvm/archive/vg_data_00003-586203914.vg
2
3 TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
4 Volume group vg01 has active volume: lv001.
5 WARNING: Found 1 active volume(s) in volume group "vg01".
6 Restoring VG with active LVs, may cause mismatch with its metadata.
7 Do you really want to proceed with restore of volume group "vg01", while 1 volume(s) are active? [y/n]: y
8 Restored volume group vg01.
没问题,那就继续,真的操作了
1vgcfgrestore vg01 -f /etc/lvm/archive/vg_data_00003-586203914.vg
2
3 Volume group vg01 has active volume: lv001.
4 WARNING: Found 1 active volume(s) in volume group "vg01".
5 Restoring VG with active LVs, may cause mismatch with its metadata.
6 Do you really want to proceed with restore of volume group "vg01", while 1 volume(s) are active? [y/n]: y
7 Restored volume group vg01.
四、恢复后进行校验
1# 显示信息
2vgdisplay VG1
3
4# 激活
5vgchange -ay VG1
6
7# 显示逻辑卷
8lvs -a -o +devices
9
10# 重新扫描
11lvscan
12inactive '/dev/vg1/lv2' [1.00 GiB] inherit ### its in inactive state and make it active to use.
13ACTIVE '/dev/vg0/lv1' [1.00 GiB] inherit
14
15# 上面如果有inactive的,重新激活
16lvchange -a y /dev/vg1/lv2
17
18# 再显示一次
19lvs -a -o +devices
20
21# 再扫描
22lvscan
23ACTIVE '/dev/vg1/lv2' [1.00 GiB] inherit
24ACTIVE '/dev/vg0/lv1' [1.00 GiB] inherit
25
26# mount上测试
27mount /dev/vg1/lv2 /lv2
28
29# 看看东西都在不在
30ls -lh /lv2
五、备份和恢复的脚本
备份一个 VG 下所有卷,每个卷一个 snapshot 快照备份文件,backup_snapshot_lvm.pl
1#!/usr/bin/perl -w
2#
3# Run through a particular LVM volume group and perform a snapshot
4# and compressed file backup of each volume.
5#
6# This script is intended for use with backing up complete system
7# images of VMs, in addition to data level backups.
8#
9
10my $source_lvm_volgroup = 'vg_storage';
11my $source_lvm_snapsize = '5G';
12my @source_lvm_excludes = ('lv_unwanted', 'lv_tmpfiles');
13
14my $dest_dir='/mnt/backup/snapshots';
15
16foreach $volume (glob("/dev/$source_lvm_volgroup/*"))
17{
18 $volume =~ /\/dev\/$source_lvm_volgroup\/(\S*)$/;
19 my $volume_short = $1;
20
21 if ("$volume_short" ~~ @source_lvm_excludes)
22 {
23 # Excluded volume, we skip it
24 print "[info] Skipping excluded volume $volume_short ($volume)\n";
25 next;
26 }
27
28 print "[info] Processing volume $volume_short ($volume)\n";
29
30 # Snapshot volume
31 print "[info] Creating snapshot...\n";
32 system("lvcreate -n ${volume_short}_snapshot --snapshot $volume -L $source_lvm_snapsize");
33
34 # Write compressed backup file from snapshot, but only replace existing one once finished
35 print "[info] Creating compressed snapshot file...\n";
36 system("dd if=${volume}_snapshot | gzip --fast > $dest_dir/$volume_short.temp.gz");
37 system("mv $dest_dir/$volume_short.temp.gz $dest_dir/$volume_short.gz");
38
39 # Delete snapshot
40 print "[info] Removing snapshot...\n";
41 system("lvremove --force ${volume}_snapshot");
42
43 print "[info] Volume $volume_short backup completed.\n";
44}
45
46print "[info] FINISHED! VolumeGroup backup completed\n";
47
48exit 0;
恢复的脚本,restore_snapshot_lvm.pl:
1#!/usr/bin/perl -w
2#
3# Runs through LVM snapshots taken by backup_snapshot_lvm.pl and
4# restores them to the LVM volume in question.
5#
6
7my $dest_lvm_volgroup = 'vg_storage';
8my $source_dir = '/mnt/backup/snapshots';
9
10print "[WARNING] Beginning restore process in 5 SECONDS!!\n";
11sleep(5);
12
13foreach $volume (glob("$source_dir/*"))
14{
15 $volume =~ /$source_dir\/(\S*).gz$/;
16 my $volume_short = $1;
17
18 print "[info] Processing volume $volume_short ($volume)\n";
19
20 # Just need to decompress & write into LVM volume
21 system("zcat $source_dir/$volume_short.gz > /dev/$dest_lvm_volgroup/$volume_short");
22
23 print "[info] Volume $volume_short restore completed.\n";
24}
25
26print "[info] FINISHED! VolumeGroup restore completed\n";
27
28exit 0;
当然要谨记,这个是块设备级别的备份,最好还是要有另外的数据的备份。