mbrback shell脚本用于创建一个硬盘MBR和分区表的备份。

然后,可以使用mbrback的MBR引导代码,MBR,分区表备份文件进行相关恢复工作。

代码:

复制代码 代码示例:

#!/bin/bash

# Script Name: mbrback

# Requires: util-linux

# 不要修改这些变量

argsneeded=1

restoretype=""

back=""

devname=""

help ()

{

echo 'mbrback version 1.0.0'

echo 'Creates MBR and partition table backups of DEVICE named:'

echo '    HOST-DEVICE-MBR-back'

echo '    HOST-DEVICE-partition-back.sf'

echo 'Restores MBR and partition table from specified backup file'

echo 'Usage: sudo mbrback DEVICE [BACKUPFOLDER]'

echo '       (creates backup files of DEVICE)'

echo 'Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]'

echo '       (restores MBR boot code only)'

echo 'Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]'

echo '       (restores entire MBR)'

echo 'Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]'

echo '       (restores partition table)'

echo 'Example: sudo mbrback sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback /dev/sda'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in current folder)'

echo 'Example: sudo mbrback sda /mybackups'

echo '         (creates MBR and partition table backups of'

echo '          /dev/sda in /mybackups)'

echo 'Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back'

echo '         (restores MBR boot code of /dev/sda using'

echo '          /mybackups/sys-sda-MBR-back)'

echo 'Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf'

echo '         (restores partition table of /dev/sda using sfdisk file '

echo '          /mybackups/sys-sda-partition-back.sf)'

echo

echo "When restoring, mbrback will always tell you what it's going to do"

echo "and allow you to abort before it writes to disk."

echo "www.jquerycn.cn 2013-9-11"

}

index=0

while [ "$1" != "" ];

do

if [ "${1:0:1}" = "-" ]; then

case "$1" in

--help | -help )

help

exit

;;

--restoreboot )

if [ "$restoretype" = "" ]; then

restoretype="boot"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorefullmbr )

if [ "$restoretype" = "" ]; then

restoretype="fullmbr"

else

echo 'mbrback: can only use one restore option'

exit 1

fi

;;

--restorepart )

if [ "$restoretype" = "" ]; then

restoretype="part"

else

echo 'mbrback: can only use one restore option'

echo

help

exit 1

fi

;;

* )

echo "mbrback: Unknown option $1"

echo

help

exit 1

;;

esac

else

let "index+=1"

case $index in

1 )

devname=`basename "$1"`

if [ ! -b "/dev/$devname" ]; then

echo "mbrback: /dev/$devname is not a valid device"

exit 1

fi

;;

2 )

back="$1"

;;

* )

echo "mbrback: Too many arguments"

exit 1

;;

esac

fi

shift

done

if (( index < $argsneeded )) || [ "$devname" = "" ]; then

echo "mbrback: missing arguments"

echo

help

exit 1

fi

if [ `whoami` != "root" ]; then

echo 'mbrback: must be run with sudo'

exit 1

fi

sysname=$HOSTNAME

if [ "$restoretype" = "" ]; then

# create MBR and table backups

if [ "$back" = "" ]; then

back=`pwd`

else

if [ ! -d "$back" ]; then

echo "mbrback: $back is not a valid backup folder"

exit 1

fi

fi

dd if=/dev/$devname of="$back/$sysname-$devname-MBR-back" bs=512 count=1

sfdisk -d /dev/$devname > "$back/$sysname-$devname-partition-back.sf"

else

# restore

if [ "$back" = "" ]; then

echo "mbrback: you must specify a backup file"

exit 1

elif [ ! -f "$back" ]; then

echo "mbrback: file not found - $back"

exit 1

fi

if [ "$restoretype" = "boot" ] || [ "$restoretype" = "fullmbr" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" = "# partition table of " ]; then

echo "mbrback: $back is not an MBR backup file"

exit 1

fi

if [ "$(stat -c%s "$back")" != "512" ]; then

echo "mbrback: $back is wrong size for an MBR backup file"

exit 1

fi

fi

if [ "$restoretype" = "part" ]; then

sfhead=`head --bytes=21 "$back"`

if [ "$sfhead" != "# partition table of " ]; then

echo "mbrback: $back not a valid sfdisk backup file"

exit 1

fi

echo

echo "You are about to overwrite your /dev/$devname partition table with"

echo "the contents of $back"

echo

echo "WARNING!!! Unless the partition table has been damaged or you"

echo "           have accidentally deleted a partition, you should abort."

echo

echo "WARNING!!! Restoring the partition table from an out-of-date backup"

echo "           may render ALL the data on your drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

echo

elif [ "$restoretype" = "boot" ]; then

echo

echo "You are about to overwrite your /dev/$devname MBR boot code with"

echo "the contents of $back"

echo

echo "WARNING: Restoring your MBR boot code from an out-of-date MBR backup"

echo "         file may render your computer unbootable."

elif [ "$restoretype" = "fullmbr" ]; then

echo

echo "You are about to overwrite your ENTIRE /dev/$devname MBR with"

echo "the contents of $back"

echo

echo "WARNING!!! The full MBR contains both boot code and the drive's"

echo "           partition table.  Unless the partition table has been"

echo "           damaged or you have accidentally deleted a partition"

echo "           you should abort and restore boot code only with"

echo "           --restoreboot instead."

echo

echo "WARNING!!! Restoring your full MBR from an out-of-date MBR backup may"

echo "           render your computer unbootable and ALL the data on your"

echo "           drive unreadable."

echo

echo "WARNING!!! Do not proceed if /dev/$devname is mounted."

fi

echo

echo "Do you want to proceed? (you must type yes to proceed)"

read s1

if [ "$s1" != "yes" ]; then

echo "mbrback: no changes made - aborted at user request"

exit 2

fi

if [ "$restoretype" = "part" ]; then

sfdisk /dev/$devname < "$back"

elif [ "$restoretype" = "boot" ]; then

dd if="$back" of=/dev/$devname bs=448 count=1

elif [ "$restoretype" = "fullmbr" ]; then

dd if="$back" of=/dev/$devname bs=512 count=1

fi

echo "/dev/$devname was updated"

fi

exit 0

linux备份根目录与还原脚本,Shell脚本备份和还原MBR(主引导记录)相关推荐

  1. 《真象还原》读书笔记——第二章 编写 MBR 主引导记录

    2.1 计算机的启动过程 开机后运行的第一个程序是 BIOS . BIOS 搬运 MBR 并 跳转运行 MBR- 2.2 软件接力第一棒 BIOS 全名 基本输入输出系统. 2.2.1 实模式下的 1 ...

  2. Linux配置脚本导出运行,linux服务器部署jar包以及shell脚本的书写

    背景:记录在linux环境下部署jar程序的过程 1 部署过程记录 1.1 程序结构 这里的main函数就在DemRest2.java 文件中. 为了部署方便,要做到以下两点: 1 在导出的jar包中 ...

  3. 一个备份MySQL数据库的简单Shell脚本(转)

    Shell脚本是我们写不同类型命令的一种脚本,这些命令在这一个文件中就可以执行.我们也可以逐一敲入命令手动执行.如果我们要使用shell脚本就必须在一开始把这些命令写到一个文本文件中,以后就可以随意反 ...

  4. linux怎么测试一个脚本,一个Linux中用于监控的简易shell脚本

    系统管理员的任务真的很艰难,因为他/她必须监控服务器.用户.日志,还得创建备份,等等等等.对于大多数重复性的任务,大多数管理员都会写一个自动化脚本来日复一日地重复这些任务.这里,我们已经写了一个she ...

  5. 一个Linux中用于监控的简易shell脚本

    系统管理员的任务真的很艰难,因为他/她必须监控服务器.用户.日志,还得创建备份,等等等等.对于大多数重复性的任务,大多数管理员都会写一个自动化脚本来日复一日地重复这些任务.这里,我们已经写了一个she ...

  6. linux启动java jar文件_推荐:Linux启动Java程序jar包Shell脚本

    #!/bin/sh# 该脚本为Linux下启动java程序的脚本## author: luandy# date: 2021/1/15## 特别注意:# 该脚本使用系统kill命令来强制终止指定的jav ...

  7. java -jar 停止_推荐:Linux启动Java程序jar包Shell脚本

    每次启动Java程序jar包的时候,难道你还在手敲java -jar xxserver.jar--?边敲边想着都需要追加哪些参数? 今天就推荐给大家一个几乎通用的Shell脚本,它支持Java程序Ja ...

  8. linux 递归删除 空目录命令,使用shell脚本实现递归删除空目录

    平时一般使用php脚本实现一些文件管理功能,总归没有shell脚本强大,虽然本人不会写shell脚本,但是读懂shell脚本还是没问题的.对于"递归删除空目录"这样简单的功能使用p ...

  9. Linux(12)-命令行的使用,shell脚本

    命令行的使用,shell脚本 1.终端shell,man 2.shell 编程 2.1 shell脚本 2.2 注释 2.3 指明所用的shell 2.4 支持函数 2.5 使用变量 2.6 解析命令 ...

  10. linux中循环删除脚本,shell脚本:遍历删除

    遍历删除文本内路径上文件 windows上测试可以安装Git linux中,准备删除文件的脚本deleteFile.sh,picture.txt保存待删除文件的文件路径,picture文件夹下面有三张 ...

最新文章

  1. oracle安装就是home3,rhel3上安装Oracle(来自Oracle网站)
  2. gcc undefined reference to 问题解决方法(使用库)
  3. ValueError: XPath error: Invalid expression in //*[@id=‘info‘]/div/p[1]/test()_Python系列学习笔记
  4. 一分钟教你用Excel从统计局抓数据!
  5. rowmapper_Spring Integration Jdbc RowMapper示例
  6. 【渝粤教育】电大中专电商运营实操 (1)作业 题库
  7. VS code常用的快捷键
  8. 乔布斯诞辰64周年 库克发文纪念:我们每一天都怀念他
  9. 系统学习深度学习(四十一)--AlphaGo Zero强化学习原理
  10. css3+js生成任意正多边形棱柱
  11. UE4 C++编程入门整理
  12. 镰仓物语 | 亲近的人即便离开人间,它也在你的身边
  13. Spark2.1.0 + CarbonData1.0.0集群模式部署及使用入门
  14. android debug SIGABRT (signal SIGABRT)
  15. 天平游码读数例题_天平游码读数
  16. 逆袭增长1300万播放,UP主仅靠20万粉登顶B站!
  17. linux下打印机监控,Linux下控制打印机笔记
  18. python决策树的应用_决策树应用(一)
  19. JVM性能调优1_享学课堂
  20. 2021春招最新分享:Java一线大厂高岗面试题解析合集(六大专题

热门文章

  1. django的render的特殊用法
  2. [争什么! 掺在一起做撒尿牛丸啊! 笨蛋]ASP.NET Core 2.0 + EF6 + Linux +MySql混搭
  3. (三)JAVA使用POI操作excel
  4. java连接DB2数据库
  5. Gentoo Linux 内核指南
  6. 拓端tecdat|R语言分段线性回归分析预测车辆的制动距离
  7. (6)matplot去掉坐标轴
  8. js基础知识汇总13
  9. Servlet教程第0~3讲笔记
  10. jupyter notbook远程连接配置(Ubuntu16.04)