Recent Changes - Search:

edit SideBar

Sh

Note: The wiki formating screws this up. If you want to copy the source code, you will need to edit the page to see it properly.

  1. !/bin/bash
  2. Note this code assumes you will be copying a one partition NTFS drive.
  3. If this is not the case then you will have to go through the code and
  4. change things. Eg. if you are just wanting to copy one partition of a
  5. multi partition drive then you will not want to copy the MBR.
  6. It is also assumed that you will probably be running this from the
  7. run_mrsync.sh script which was run at login in the background.
  8. Therefore no user input is needed but informational messages
  9. are displayed anyway, just in case.
  10. These are tweaking variables (file and folder locations) that you may want to change
  11. The uid is the user id of the user this script is run as (the LiveCD? boot user normally)

uid=dsl

  1. The sourcemnt is the mount point of the samba share on the source
  2. It needs to be the same as the one in the source.sh script.

sourcemnt="/mnt/tmp"

  1. The sambashare is a combination of the machine name of the source
  2. (which needs to be the same as the sourcename in source.sh) and the
  3. samba share name specified in the smb.conf file

sambashare="//mrsync_source/temp"

  1. The IP_LIST_FILE is where the list of IP addresses is stored and needs to be accessible from both the source and target machines.

IP_LIST_FILE=$sourcemnt"/iplist.txt"

  1. The HDDmnt? is the mount point reserved for the local partition to be overwritten

HDDmnt?="/mnt/disk1"

  1. The HDDdevice? is the local hard drive to copy the MBR to

HDDdevice?="/dev/hda"

  1. The HDDpartition? is the local partition to copy the partition boot sector
  2. to, format, then mount so the files can be copied to it.

HDDpartition?=$HDDdevice"1"

  1. The tmpexpectfile is just a temporary file that is used to build scripts
  2. for the expect command. tmpfile is just for generic temporary files.

tmpexpectfile="/tmp/expect.txt" tmpfile="/tmp/temp.txt"

  1. The finishflagfile is just a file the target script checks for to see if
  2. the copying process is finished.
  3. It must be the same file as specified in the source.sh

finishflagfile=$sourcemnt"/finished.txt"

  1. Here ends the tweaking variables

index=1

echo $index") Stopping Samba" sudo /etc/init.d/samba.dpkg-new stop let "index+=1"

echo $index") Mount share on source to "$sourcemnt sudo mkdir $sourcemnt echo "Waiting for source machine to be available" echo "or type 'q' to quit" sudo rm -rf $tmpfile touch $tmpfile answer=""

  1. This loop tries to mount the samba share with the output and error streams
  2. redirected to Null. It then waits for 5 seconds, and tests to see if
  3. the mount folder is not empty (i.e. mounted successfully) or the user
  4. pressed q; if so the loop exits.

while [ \( "$answer" != "q" \) -a \( ! -s $tmpfile \) ]; do

	sudo mount -t smbfs -o guest,rw,uid=$uid $sambashare $sourcemnt &>/dev/null
	read -n 1 -t 5 answer
	ls $sourcemnt > $tmpfile

done

  1. This beep is an audio indication that this machine has exited this loop

beep let "index+=1"

if [ -z $answer ] then

	echo $index") Writing IP Address to source "$IP_LIST_FILE
	ifconfig eth0 | grep 'inet addr' | sed -ne 's/^ *inet addr://' -e 's/ .*//p' > $tmpfile
	read IPAddress? < $tmpfile
	# This deletes the IP Address if it already exists then adds it to the bottom of the file
	sed -ie /$IPAddress/d $IP_LIST_FILE
	echo $IPAddress >> $IP_LIST_FILE
	let "index+=1"

fi

if [ -z $answer ] then

	echo $index") Read password from file on source share"
	read pass < $sourcemnt/password.txt
	let "index+=1"

fi

if [ -z $answer ] then

	echo $index") Change password for "$uid" to "$pass
	echo 'spawn sudo passwd '$uid > $tmpexpectfile
	echo 'after 5000' >> $tmpexpectfile
	echo 'expect "new UNIX password:"'  >> $tmpexpectfile
	echo 'send "'$pass'\r"' >> $tmpexpectfile
	echo 'expect "new UNIX password:"'  >> $tmpexpectfile
	echo 'send "'$pass'\r"' >> $tmpexpectfile
	echo 'expect "updated successfully"'  >> $tmpexpectfile
	echo 'send_user "\rDone\r"'  >> $tmpexpectfile
	# This is done twice as a kludge because the first time sometimes doesn't work for some reason
	expect -f $tmpexpectfile
	expect -f $tmpexpectfile
	echo 
	let "index+=1"

fi

if [ -z $answer ] then

	echo $index") Copy MBR and partition boot sector from Samba share (without overwriting the Partition table)"
	# This copies the Master Boot Record without the Partition table
	sudo dd if=$sourcemnt/mbr.bin of=$HDDdevice bs=446 count=1
	# This copies the Master Boot Record including the Partition table
	# and should be commented out normally
	# sudo dd if=$sourcemnt/mbr.bin of=$HDDdevice bs=512 count=1
	#This copies the Partition boot sector
	sudo dd if=$sourcemnt/bootsect.bin of=$HDDpartition bs=512 count=1
	let "index+=1"

fi

if [ -z $answer ] then

	echo $index") Format partition to NTFS and mount as Writable at "$HDDmnt
	sudo mkfs -t ntfs $HDDpartition --fast
	sudo mkdir $HDDmnt
	sudo mount -t ntfs-3g -o rw,uid=$uid $HDDpartition $HDDmnt -force
	let "index+=1"

fi

if [ -z $answer ] then

	echo $index") Wait for signal from source that copying is finished"
	# This loop justs checks for the existence of the flag file every 20 seconds
	# and exits when it finds it has been created.
	while [ ! -f $finishflagfile ]; do
		sleep 20
	done
	let "index+=1"

fi

echo $index") Unmount drives and tidy up" sudo umount $HDDmnt sudo rmdir $HDDmnt sudo umount $sourcemnt sudo rmdir $sourcemnt let "index+=1"

echo echo "Finished" echo

  1. This beep is an audio indication that this machine has finished

beep

Edit - History - Print - Recent Changes - Search
Page last modified on September 17, 2007, at 02:11 PM