如何备份主引导记录(MBR)和分区表(PT)?
我想知道如何:
- 做一个备份
- 做一个恢复
将所有MBR和我所有磁盘的所有PT都放到一个文件中,以便我可以使用rsync
或类似的程序轻松备份它们。
哈,这很简单:
-
创建目录
/sysbackup
:mkdir /sysbackup
-
在编辑器中复制以下脚本:
#!/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;
-
将其保存在
/usr/local/bin
(例如mbr-pt-bck
) -
chmod +x /usr/local/bin/mbr-pt-bck
- 使用
sudo mbr-pt-bck
执行它 - 确保
/sysbackup/
包含在rsync
(或类似的)命令中。
或者,
- 将上面的脚本放在
/etc/rc.local
或等效的systemd中,并在每次启动时自动执行它。 - 将其包含在备份前脚本中
要恢复以上任何一项也很容易:
MBR:
dd if=/sysbackup/MBRBackup-szHostName-dev-sda.bck of=/dev/sda bs=512 count=1
PT:
首先是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
并且您将获得分开的救援提示,您可以在其中填写上述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