Linux: making image of a drive and of the MBR

Some of the small programs I wrote on my spare time. They are Free Software, and written mostly in OCaml.
Post Reply
User avatar
Vincent
Posts: 3077
Joined: Fri Apr 07, 2006 12:10 pm
Location: Schtroumpf
Contact:

Linux: making image of a drive and of the MBR

Post by Vincent » Fri Jun 19, 2009 12:14 am

Taking images of drives is easy under linux using dd. Here is a tiny wrapper written for my personal use, which makes it very easy to save and restore a specific partition* on my system. edit: Added procedures to save and restore the Master Boot Record.

Images are compressed using gzip.

(
*: namely the Windows system: on my gaming computer I have Windows and Linux in dual boot. The windows system is on a small partition, and its softwares are on another one. Since Windows turns into a piece of junk pretty quickly, the idea is to restore it to its original pristine state every now and then, without having to reinstall it manually, which is quite tedious. Not to mention reactivating it online (which can only be done a few times) and restoring the MBR which the installation destroys blithely.
)




Usage:
(change $disk in script to the drive you are interested in)

Code: Select all

source disk_saver.sh

disksave some_description
diskrestore some_save_file

-> <download script>
<very interesting post on DD and its many uses>


code (maybe not up-to-date; use download link)

Code: Select all

# Mini disk image
# Vincent Hugot

############################
# OPTIONS YOU MUST CHANGE: #
############################

# The disk/partition you want to take an image of
drive=sda1

# Drive on which the Master Boot Record is stored
mbrdrive=sda

# Name of the computer 
# (use this if you have several computers, in order to avoid confusion)
compname="[computer]"

#################
# OTHER OPTIONS #
#################

blocks=16M

mbrsz=446  # save boot loader only
#mbrsz=512  # save boot loader and partitions table

# save name creates the file 'name' containing the image of the selected drive
disksave () { 
  local sname="SAVE$compname-$drive--$1.img";
  local cmd="dd conv=notrunc if=/dev/$drive ibs=$blocks | gzip > $sname" ; 
  echo "Executing following SAVE command:";
  echo $cmd;
  eval $cmd;
  chmod -w $sname;
  ls -sh $sname;
}

# restore img restores the image file 'img' to the selected drive
diskrestore () {
  local cmd="dd conv=notrunc if=$1 ibs=$blocks | gunzip | dd conv=notrunc of=/dev/$drive obs=$blocks";
  echo "Executing following RESTORATION command:";
  echo $cmd;
  eval $cmd;
}

# save the Master Boot Record
mbrsave () {
  local sname="MBR$compname-$mbrdrive-$mbrsz--$1.img";
  local cmd="dd if=/dev/$mbrdrive of=$sname count=1 bs=$mbrsz";
  echo "Executing following MBR SAVE command:";
  echo $cmd;
  eval $cmd;
  chmod -w $sname;
  ls -sh $sname;
}

# restore the Master Boot Record
mbrrestore () {
  local cmd="dd if=$1 of=/dev/$mbrdrive count=1 bs=$mbrsz";
  echo "Executing following MBR RESTORATION command:";
  echo $cmd;
  eval $cmd;
}
{ Vincent Hugot }

Post Reply

Who is online

Users browsing this forum: No registered users and 167 guests