#!/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;
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
哈,这很简单:
创建一个目录
/sysbackup
:在编辑器中复制以下脚本:
将其保存在
/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:
太平洋时间:
首先到一个
cat /sysbackup/PartBackup-szHostName-dev-sda.txt
然后做一个:
你会得到 parted 的救援提示,在这里你填写上面的数据
cat