如何备份我的主引导记录(MBR)和分区表(PT)?

如何备份我的主引导记录(MBR)和分区表(PT)?

我想知道如何:

  1. 备份
  2. 进行恢复

将所有磁盘的所有 MBR 和所有 PT 保存到一个文件中,以便我可以使用rsync或类似程序轻松地备份它们。

答案1

哈哈,这很简单:

  1. 创建目录/sysbackup

    mkdir /sysbackup
    
  2. 在编辑器中复制以下脚本:

    #!/bin/bash
    #
    # This script saves the well-known (last good) partition table entries to a text file 
    # and the MBR to a binary file for all live disks on the system.
    #
    # Copyright (c) Fabby 2017
    #
    # This program is free software: you can redistribute it and/or modify it under 
    # the terms of the GNU General Public License as published by the Free Software 
    # Foundation, either version 3 of the License, or (at your option) any later 
    # version.
    #
    # This program is distributed in the hope that it will be useful, 
    # but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
    # or FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
    # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
    # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
    # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
    # THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    # See the GNU General Public License for more details.
    #
    # You DID NOT receive a copy of the GNU General Public License along with 
    # this program as the license is bigger then this program.
    # Therefore, see http://www.gnu.org/licenses/ for more details.
    #
    # This script should be run at the /etc/rc.local or systemd equivalent point in time
    #
    for szDisk in /dev/?d?; 
    do
      sfdisk --dump "$szDisk" > /sysbackup/PartBackup-"$(hostname)-${szDisk//\//-}""$(date +"%F-%H%M%S%N")"".txt"
      dd if="$szDisk" of=/sysbackup/MBRBackup-"$(hostname)-${szDisk//\//-}""$(date +"%F-%H%M%S%N")"".bck" bs=512 count=1
    done;
    
  3. 保存为/usr/local/bin(例如mbr-pt-bck

  4. chmod +x /usr/local/bin/mbr-pt-bck
  5. 使用执行sudo mbr-pt-bck
  6. 确保/sysbackup/包含在您的rsync(或类似)命令中。

或者,

  • 将上述脚本放入/etc/rc.local或等效的 systemd 中,并在每次启动时自动执行。
  • 将其包含在您的预备份脚本中

答案2

恢复上述任何一个也相当容易:

主引导记录(MBR):

dd if=/sysbackup/MBRBackup-szHostName-dev-sda.bck of=/dev/sda  bs=512 count=1

太平洋标准时间:

首先cat /sysbackup/PartBackup-szHostName-dev-sda.txt

Disk label type: msdos
Minor    Start       End     Type      Filesystem  Flags
1          0.031   8056.032  primary   ext3

然后只需执行以下操作:

parted /dev/sda rescue 

您将获得 parted 救援提示,您可以在其中填写上面的数据cat

Start? 0
End? 8056
Information: A ext3 primary partition was found at 0.031MB ->
8056.030MB.  Do you want to add it to the partition table?
Yes/No/Cancel? y

相关内容