PDA

View Full Version : Image save and restore CD/DVD with KNOPPIX



mclae
03-09-2006, 10:24 PM
Saving and restoring disk images is easy to do with Knoppix. What is hard is finding all the parts you need and then testing with different parameters. Here are three scripts to automate the process.
Note that I have used these scripts to save/restore Windows hard drives.

First, what you will need to do this:
1. an extra partition to hold the partition(s) you are saving. (May be Linux or FAT32 partition, but not NTFS)
2. a tool to edit CD/DVD images. (I use UltraIso and .ISO image files)
3. a CD/DVD burner.
4. an ISO KNOPPIX image

Note: if you use an external USB hard disk or CD/DVD drive, some versions of KNOPPIX may have problems. I recommend you use the KNOPPIX 4.0+ for best hardware compatibility.

How it works:
During bootup, KNOPIX calls the KNOPPIX.sh script. All this version does is look for and run auto.sh

Auto.sh looks for image files on the CD.DVD. If it finds an inage set, it restores the image to the local hard drive. If no images set is found, auto.sh calls saveimage.sh.

saveimage.sh prompts for several parameters, then saves the selected partitions to the destination drive.

To create a save CD, copy the file listings below into .SH files and add them to a disk image (.ISO) as specified.

To create a restore CD/DVD, start with the save CD image (ISO) and add the saved image set to the root of the CD image. This will be either \single, \double, or \triple.

Programmer's note: this is my first Linux project. I am positive that there are better ways to handle user input, partition detection, etc. Please tell me what they are! :) I consider this a working prototype.

File #1: knoppix.sh
Copy this as a .SH file into the \KNOPPIX directory of your KNOPPIX ISO image.
---start copy
#!/bin/sh
[ "`id -u`" = "0" ] || { echo "You need root privileges to modify the system!" >&2 ; exit 1; }
#set -v
echo "looking for auto.sh"
[ -d "$1" ] && CONFIGS="$1/auto.sh"
[ -f "$CONFIGS" ] || CONFIGS="/cdrom/auto.sh"
[ -f "$CONFIGS" ] || CONFIGS="/cdrom/KNOPPIX/auto.sh"
if [ -f "$CONFIGS" ]; then
echo "found $CONFIGS, running it"
/bin/bash $CONFIGS
fi
#sleep 30
set +v
---end copy

File #2: auto.sh
COpy this file to the root directory of the KNOPPIX ISO image.
---start copy
#! /bin/bash
#set -v
#sleep 10
clear
echo "Running auto.sh"

#set devfault values
imgfile1=xx
imgfile2=xx
imgfile3=xx
mbrfile=master.mbr
fstabfile=fstab.sav
sffile=sftable.txt

loadflag="n"

echo restore base partitions from saved format file
echo sfdisk /dev/Sda </cdrom/

#give the user the chance to abort image restore

echo "This DVD may reload an image to this computer. This will destroy all programs and data on this computer."
echo "If you have booted from this disk by mistake, select Quit to reboot without changeing this computer."
echo ".."
echo "."

OPTIONS="Quit Continue"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo exiting
reboot
# exit 0
else
break
fi
done

#check for partition restore table file and set restore variables

if [ -e /cdrom/single.000 ]; then
imgfile1=/cdrom/single.000
imgfile2=/cdrom/xx
imgfile3=/cdrom/xx
mbrfile=/cdrom/master.mbr
fstabfile=/cdrom/fstab.sav
sffile=/cdrom/sftable.txt
loadflag="y"

elif [ -e /cdrom/double.000 ]; then
imgfile1=/cdrom/double-c.000
imgfile2=/cdrom/double-d.000
imgfile3=/cdrom/xx
mbrfile=/cdrom/master.mbr
fstabfile=/cdrom/fstab.sav
sffile=/cdrom/sftable.txt
loadflag="y"

elif [ -e /cdrom/triple.000 ]; then
imgfile1=/cdrom/triple-c.000
imgfile2=/cdrom/triple-d.000
imgfile3=/cdrom/triple-e.000
mbrfile=/cdrom/master.mbr
fstabfile=/cdrom/fstab.sav
sffile=/cdrom/sftable.txt
loadflag="y"
fi
echo "."
echo "."

#uncomment for debugging
#echo imgfile1: "$imgfile1"
#echo imgfile2: "$imgfile2"
#echo imgfile3: "$imgfile3"

#echo mbrfile: "$mbrfile"
#echo fstabfile: "$fstabfile"
#echo sffile: "$sffile"
#echo loadflag: "$loadflag"
#echo "."
#echo "."

errflag=0

#OPTYN="Pause";select opt in $OPTYN; do break; done

# if loadflag == y, then image file was found
if [ "$loadflag" = "y" ]; then

# erase current partitions. May be up to 4 per disk
# note: assumption that target drive is 'Sda'
parted /dev/sda rm 1
parted /dev/sda rm 2
parted /dev/sda rm 3
parted /dev/sda rm 4
echo Old partitions now cleaned
if [ -e "$mbrfile" ]
then
# if disk MBR saved, restore from file
/bin/dd if="$mbrfile" of=/dev/sda bs=512 count=1
fi
if [ -e "$fstabfile" ]
then
# if partition settings saved, restore from file
cp $fstabfile /etc/fstab
fi

echo restoring base partitions from saved format file
# echo "sfdisk /dev/sda $sffile"

sfdisk /dev/sda <"$sffile"
if [ -e $imgfile1 ]
then
echo restoring image $imgfile1 with partimage
/bin/dd if=/dev/zero of=/dev/sda1 bs=512 count=1

partimage -b restore /dev/sda1 "$imgfile1"
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
else
bootflag="Y"
fi

fi
if [ -e $imgfile2 ]
then
echo restoring image $imgfile2 with partimage
/bin/dd if=/dev/zero of=/dev/sda2 bs=512 count=1
partimage -b restore /dev/sda2 "$imgfile2"
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
else
bootflag="Y"
fi
fi
if [ -e $imgfile3 ]
then
echo restoring image $imgfile3 with partimage
/bin/dd if=/dev/zero of=/dev/sda3 bs=512 count=1
partimage -b restore /dev/sda3 "$imgfile3"
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
else
bootflag="Y"
fi
fi

if [ "$bootflag" = "Y" ]; then
echo exiting
reboot
else
clear
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "

echo "Partimage error code $errflag returned"
OPTYN="error";select opt in $OPTYN; do break; done
fi
else
# if loadflag != y, then image file was not found
#
echo "no partition restore data file found."
echo "."
echo 'Select 1 to run saveimage script. Select 2 to finish booting KNOPPIX.'
echo "."
echo "."

OPTIONS="Saveimage Knoppix"
select opt in $OPTIONS; do
if [ "$opt" = "Saveimage" ]; then
cd /
# run image capture script
/cdrom/saveimage.sh
reboot
else
break
fi
done

fi
---end copy

File #3: saveimage.sh
Copy this file to the root of the ISO image.
---start copy
#! /bin/bash -
#set -v
echo "Running saveimage.sh"
#sleep 10

echo select name of local hard drive
OPTIONS="sda hde hda hdg"
select opt in $OPTIONS; do
if [ "$opt" = "hde" ]; then
indrive1=/dev/hde1
indrive2=/dev/hde2
indrivez=/dev/hde3
sfdrive=/dev/hde
elif [ "$opt" = "hda" ]; then
indrive1=/dev/hda1
indrive2=/dev/hda2
indrivez=/dev/hda3
sfdrive=/dev/hda
elif [ "$opt" = "hda" ]; then
indrive1=/dev/hdg1
indrive2=/dev/hdg2
indrivez=/dev/hsg3
sfdrive=/dev/hdg
elif [ "$opt" = "sda" ]; then
indrive1=/dev/sda1
indrive2=/dev/sda2
indrivez=/dev/sda3
sfdrive=/dev/sda
fi
break
done

outname2=na
outnamez=na

echo select name of image file
OPTIONS="single double triple"
select opt in $OPTIONS; do
if [ "$opt" = "single" ]; then
outname1=/mnt/output/single/single
outpath=/mnt/output/single
elif [ "$opt" = "double" ]; then
outname1=/mnt/output/double/double-c
outname2=/mnt/output/double/double-d
outpath=/mnt/output/double
elif [ "$opt" = "triple" ]; then
outname1=/mnt/output/triple/triple-c
outname2=/mnt/output/triple/triple-d
outname3=/mnt/output/triple/triple-e
outpath=/mnt/output/triple
fi
break
done

mkdir /mnt/output
bootflag="Y"

#note: you must know this before the script starts. This should be an external hard drive that KNOPPIX can see during bootup. (External USB drives are ok, network drives may not be)
#note2: Different releases of KNOPPIX have different partition names.
#note3: Destination may be a FAT32 or Linux partition, but not NTFS.

echo select name of storage partitiion
OPTIONS="uba1 sda1 sde1 hde2 hde3 sda2 sda3"
select opt in $OPTIONS; do
if [ "$opt" = "hde2" ]; then
mount -o rw /dev/hde2 /mnt/output
if [ $? -ne 0 ]
# Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
if [ "$opt" = "hde3" ]; then
mount -o rw /dev/hde3 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
if [ "$opt" = "sda1" ]; then
mount -o rw /dev/sda1 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
if [ "$opt" = "sde1" ]; then
mount -o rw /dev/sde1 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi


fi
if [ "$opt" = "sda2" ]; then
mount -o rw /dev/sda2 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
if [ "$opt" = "sda3" ]; then
mount -o rw /dev/sda3 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then

echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
if [ "$opt" = "uba1" ]; then
mount -o rw /dev/uba1 /mnt/output
if [ $? -ne 0 ] # Test exit status of "mount" command.
then
echo "invalid drive: $opt"
OPTYN="error";select opt in $OPTYN; do break; done
else
break
fi
fi
done
#set -v



mkdir $outpath

echo "indrive1: $indrive1 indrive2: $indrive2 outpath: $outpath"
cd /

dd if="$sfdrive" of=/mnt/output/master.mbr count=1 bs=512
if [ $? -ne 0 ] # Test exit status of "dd" command.
then
bootflag="N"
OPTYN="error";select opt in $OPTYN; do break; done
fi
sfdisk -d "$sfdrive" >/mnt/output/sftable.txt
if [ $? -ne 0 ] # Test exit status of "sfdisk" command.
then
bootflag="N"
echo "error saving $sfdrive to file"

OPTYN="error";select opt in $OPTYN; do break; done
fi
cp /etc/fstab /mnt/output/fstab.sav
if [ $? -ne 0 ] # Test exit status of "cp /etc/fstab" comm
then
bootflag="N"
OPTYN="error-fstab";select opt in $OPTYN; do break; done
fi

mv /mnt/output/*.* $outpath

if [ "$outname3" != "na" ]; then
echo "Capturing drive E:"
partimage -z1 -o -d -f3 -b save $indrive3 $outname3
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
echo "error $errflag saving $indrive3"
OPTYN="error-partimage-E";select opt in $OPTYN; do break; done
fi
else
echo "drive E: not defined for th1s capture configuration"
fi

if [ "$outname2" != "na" ]; then
echo "Capturing drive D:"
partimage -z1 -o -d -f3 -b save $indrive2 $outname2
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
echo "error $errflag saving $indrive2"
OPTYN="error-partimage-D";select opt in $OPTYN; do break; done
fi
else
echo "drive D: not defined for th1s capture configuration"
fi

partimage -z1 -o -f3 -d -b save $indrive1 $outname1
errflag=$?
if [ $errflag -ne 0 ] # Test exit status of "partimage" command.
then
bootflag="N"
echo "error $errflag saving $indrive1"
OPTYN="error-partimage-C";select opt in $OPTYN; do break; done
fi

umount /mnt/output
rmdir /mnt/output
if [ "$bootflag" = "Y" ]; then
echo rebooting
reboot
fi

exit
---end copy