How to backup and restore a partition table on Linux | 简单方法:磁盘分区表的备份与恢复(**译文笔记**)

How to backup and restore a partition table on Linux (****)

------------------------------------------------------------------

对 linux 用法的搜索,使用一手资料,即 发行版的有关 manual,这是优先的。第二手资料采用英文搜索引擎。最后才是中文搜索引擎。

或者,首先采用中文搜索引擎,如果发现搜到的垃圾太多时,则需要果断转为采用英文搜索引擎。否则,会浪费大量时间的?

在 linux OS 中,对硬盘分区表进行备份和恢复,只需要一条很短的命令,就 OK 了。但是,用中文搜索引擎搜了几天,都无法解决。

要点:

1. 确认磁盘的分区格式:fdisk -l ,或 gdisk -l /dev/sda。

磁盘标签类型:gpt   << gpt 分区格式。

“磁盘标签类型”属于公共通用数据,使用哪一个工具,都可以查询到。与工具无关

# fdisk -l   <<这个命令,查询到的信息太多。需要的信息淹没在大堆其他信息里。
# fdisk -l /dev/sda

注意:fdisk 与 sfdisk, gdisk 与 sgdisk 命令的区别。

2. 不同的分区格式,采用不同的备份与恢复命令行工具:简单,有效

2.1. 目前:sfdisk -->MBR;  sgdisk -->GPT。

不排除在未来,两种工具会合并在一起。或者以新的软件名称来合并在一起,或新软件来取代之。

2.2. MBR 格式

How do I back-up Linux partition with sfdisk command?

To backup /dev/sda partition table, enter:

    # sfdisk -d /dev/sda > sda.partition.table.12-30-2015.txt

Copy sda.partition.table.12-30-2015.txt to NFS mounted share or a USB pen drive.


How do I restore the Linux partition with sfdisk command to the disk?

    # sfdisk /dev/sda < sda.partition.table.12-30-2015.txt
    # sfdisk /dev/sda < /path/to/usb/pen/sda.partition.table.12-30-2015.txt

OR

    # sfdisk -f /dev/sda < /media/usb/sda.partition.table.12-30-2015.txt

2.3. GPT 格式

How do I backup /dev/sda GPT partition table to a file?

To save partition data to a backup file called /root/sda_partition_table_12_30_2015, run:

    # sgdisk --backup={/path/to/file} {/dev/device/here}
    # sgdisk --backup=/root/sda_partition_table_12_30_2015 /dev/sda

How do I restore GPT partition table from a file?

To restore the backup use:

    # sgdisk --load-backup={/path/to/file} {/dev/device/here}
    # sgdisk --load-backup=/media/usb/sda_partition_table_12_30_2015 /dev/sda

3. 说明

  • GPT 的备份文件好像是 二进制文件,在文本编辑器中打开为乱码。所以未必需要以 .txt 作为后缀名。MBR 的备份文件,是文本文件?
  • 恢复操作,如果需要挂载到其他 OK的系统中进行。完成后,记住执行“卸载”操作;否则,作为系统盘时,启动异常。

4. 问题
VMware 17, ubuntu 22.04,需要恢复分区的磁盘,为当前的操作系统盘。

执行恢复分区表后,首次重新,会出现好像是“内部错误”这样的提示;再次重启,OK,没有提示。

可能原因:

VM 不能完美支持这种操作?

还是其他的什么原因?

------------------------------------------------------------------

查询磁盘分区格式信息,示例:

  • 使用 fdisk、或 gdisk,都可以。
  • 但是,在备份与恢复操作时: fdisk 只用于 MBR,gdisk 只用于 GPT。

GPT 输出为二进制文件 ?

~$ sudo fdisk -l /dev/sda
Disk /dev/sda:32 GiB,34359738368 字节,67108864 个扇区
Disk model: VMware Virtual S
单元:扇区 / 1 * 512 = 512 字节
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt   << gpt 分区格式
磁盘标识符:F2A8B9C0-688B-4F6A-8BE1-77A70C05BC44

设备          起点     末尾     扇区  大小 类型
/dev/sda1     2048     4095     2048    1M BIOS 启动
/dev/sda2     4096  1054719  1050624  513M EFI 系统
/dev/sda3  1054720 67106815 66052096 31.5G Linux 文件系统

~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.8

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 67108864 sectors, 32.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): F2A8B9C0-688B-4F6A-8BE1-77A70C05BC44
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 67108830
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  
   2            4096         1054719   513.0 MiB   EF00  EFI System Partition
   3         1054720        67106815   31.5 GiB    8300 

--- ---

MBR 输出为文本文件 ?

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xba886cfb

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 63686655 63684608 30.4G 83 Linux
/dev/sda2       63686656 67094527  3407872  1.6G 83 Linux

sda.partition.table.12-30-2015.txt:MBR 输出为文本文件。

label: dos
label-id: 0xba886cfb
device: /dev/sda
unit: sectors
sector-size: 512

/dev/sda1 : start=        2048, size=    63684608, type=83, bootable
/dev/sda2 : start=    63686656, size=     3407872, type=83

------------------------------------------------------------------

参考:

Understanding Disk Partition in Linux – MBR vs GPT  https://www.howtouselinux.com/post/mbr-vs-gpt

How do you restore a GPT partition to a legacy bios device?  https://superuser.com/questions/1743640/how-do-you-restore-a-gpt-partition-to-a-legacy-bios-device

--- ---


linux分区备份,linux备份分区表,及dd命令 (**)  https://blog.csdn.net/ken2232/article/details/134196267

linux下磁盘分区(fdisk,gdisk,parted)三种分区工具比较  https://blog.csdn.net/ken2232/article/details/134196559


MBR 与 GPT,关于分区表你应该知道的一些知识 - 硬盘使用知识大全(8) https://blog.csdn.net/ken2232/article/details/134197391

Linux磁盘管理——分区+挂载(fdisk+gdisk+mount+lsblk+df)(***)  https://blog.csdn.net/ken2232/article/details/134196942


快速实现linux硬盘分区恢复的简易方法 (linux 硬盘分区恢复)  https://blog.csdn.net/ken2232/article/details/134195041

======================================

How to backup and restore a partition table on Linux (****)

  https://www.cyberciti.biz/faq/linux-backup-restore-a-partition-table-with-sfdisk-command/

Author: Vivek Gite Last updated: December 12, 2020

s it possible to just backup my Linux partition table and restore it when required? How do I restore partition table if it get deleted by mistake on Linux operating system? How do I backup and restore a GPT partition table on Linux?

Yes, you need to backup both data and disk’s partition table. This is useful for:
 

  1. Replicating disk’s partition for Linux software RAID in case of disk failure.
  2. Dealing with corrupted partition table
  3. Dealing with deleted partition table by mistake as data may still exists on the disk and can be accessed again with a correct partition table.

WARNING! These examples may crash your computer if NOT executed with proper care. BE EXTREMELY CAREFUL WITH THE FOLLOWING COMMANDS. ONE TYPING MISTAKE AND ALL YOUR DATA IS LOST.

How to see my current partition table

Type the following command to list partitions of each device:

# fdisk -l
# fdisk -l /dev/sda


OR

# sfdisk -l
# sfdisk -l /dev/sda

Sample outputs:

Fig.01: Linux List Disk Partitions Command

How do I back-up Linux partition with sfdisk command?

To backup /dev/sda partition table, enter:

# sfdisk -d /dev/sda > sda.partition.table.12-30-2015.txt

Copy sda.partition.table.12-30-2015.txt to NFS mounted share or a USB pen drive.

How do I restore the Linux partition with sfdisk command to the disk?

# sfdisk /dev/sda < sda.partition.table.12-30-2015.txt
# sfdisk /dev/sda < /path/to/usb/pen/sda.partition.table.12-30-2015.txt

OR

# sfdisk -f /dev/sda < /media/usb/sda.partition.table.12-30-2015.txt

How do I replicate a disk partition table from /dev/sda to /dev/sdd with the same geometry?

This is useful for Linux based software RAID device. Say you replaced /dev/sdd and you want to rebuild the Linux software RAID array. The first step is to replicate a partition table from a disk to another disk:

# sfdisk -d /dev/sda | sfdisk -f /dev/sdd

Now, run the following to verify that both hard drives have the same partitioning:

# fdisk -l /dev/sda
# fdisk -l /dev/sdd

Finally, use mdadm to manage and rebuild your RAID device:

# mdadm --manage /dev/mdX --add /dev/sdd1
# mdadm --manage /dev/mdX --add /dev/sdd2
....
..
# mdadm --manage /dev/mdX --add /dev/sddN

Use the following command to see raid array sync progress:

# watch cat /proc/mdstat

A note about disks with GPT (GUID Partition Table)

From the man page:

GPT fdisk (aka gdisk) is a text-mode menu-driven program for creation and manipulation of partition tables. It will automatically convert an old-style Master Boot Record (MBR) partition table or BSD disklabel stored without an MBR carrier partition to the newer Globally Unique Identifier (GUID) Partition Table (GPT) format, or will load a GUID partition table. When used with the -l command-line option, the program displays the current partition table and then exits.

Let us install gdisk on a Debian or Ubuntu Linux using apt-get command:

$ sudo apt-get install gdisk

Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libicu52
The following NEW packages will be installed:
  gdisk libicu52
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,937 kB of archives.
After this operation, 28.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/main gdisk amd64 0.8.8-1ubuntu0.1 [185 kB]
Get:2 http://security.ubuntu.com/ubuntu/ trusty-security/main libicu52 amd64 52.1-3ubuntu0.4 [6,752 kB]
Fetched 6,937 kB in 24s (284 kB/s)                                             
Selecting previously unselected package libicu52:amd64.
(Reading database ... 65753 files and directories currently installed.)
Preparing to unpack .../libicu52_52.1-3ubuntu0.4_amd64.deb ...
Unpacking libicu52:amd64 (52.1-3ubuntu0.4) ...
Selecting previously unselected package gdisk.
Preparing to unpack .../gdisk_0.8.8-1ubuntu0.1_amd64.deb ...
Unpacking gdisk (0.8.8-1ubuntu0.1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up libicu52:amd64 (52.1-3ubuntu0.4) ...
Setting up gdisk (0.8.8-1ubuntu0.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...

RHEL/CentOS user use the yum command to install the same:

# yum install gdisk

Fedora Linux user use the dnf command to install the same:

$ sudo dnf install gdisk

OpenSUSE Linux user use the yast command to install the same:

# yast install gdisk

Please note that sgdisk command works with both Linux and Unix-like system.

How do I backup /dev/sda GPT partition table to a file?

To save partition data to a backup file called /root/sda_partition_table_12_30_2015, run:

# sgdisk --backup={/path/to/file} {/dev/device/here}
# sgdisk --backup=/root/sda_partition_table_12_30_2015 /dev/sda

How do I restore GPT partition table from a file?

To restore the backup use:

# sgdisk --load-backup={/path/to/file} {/dev/device/here}
# sgdisk --load-backup=/media/usb/sda_partition_table_12_30_2015 /dev/sda

How do I replicate GPT partition scheme from /dev/sda to /dev/sdd (RAID array rebuild)?

The syntax is:

# sgdisk -R {SECOND-DEVICE-NAME-HERE} /dev/sda
# sgdisk -R /dev/sdd /dev/sda

To randomize the GUID on the /dev/sdd, enter:

# sgdisk -G /dev/sdd

Finally, verify that both hard drives have the same partitioning schema:

# sgdisk -p /dev/sda
# sgdisk -p /dev/sdd

Finally, use mdadm command as explained earlier to rebuild the RAID array on Linux.

About the author: Vivek Gite is the founder of nixCraft, the oldest running blog about Linux and open source. He wrote more than 7k+ posts and helped numerous readers to master IT topics. Join the nixCraft community via RSS Feed or Email Newsletter.

:https://www.cyberciti.biz/faq/linux-backup-restore-a-partition-table-with-sfdisk-command/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值