#!/bin/sh # 2000-03-07 Rafael Skodlar # Copy files from disk to disk per partition # All partitions mounted under root # 2003-01-15 Rafael Skodlar, modified DISK0=hda DISK1=hdb export DISK0 DISK1 START=`date '+%Y-%m-%d %H:%M'` PART_MOUNT=/tmp/partitions.$DISK0 PART_TABLE=/tmp/partitions.table DISK_USE=/tmp/disk.use LOG=/tmp/disk_clone.log DELAY=10 #DISK0=/dev/$1 #DISK1=/dev/$2 function compare_disks() { DISK_SIZE0=`sfdisk -s | awk "/$DISK0/{print \$2}"` DISK_SIZE1=`sfdisk -s | awk "/$DISK1/{print \$2}"` echo "=================================================================" echo "Disk size [/dev/$DISK0] = $DISK_SIZE0" echo "Disk size [/dev/$DISK1] = $DISK_SIZE1" echo "=================================================================" if [ "$DISK_SIZE0" != "$DISK_SIZE1" ] then echo "Disk drives are not the same size!" echo -e "$DISK0: $DISK_SIZE0\n$DISK1: $DISK_SIZE1\n" exit fi } function ismounted() { # make sure we don't mess with mounted drive FLAG=`df | grep $DISK1` if [ -n "$FLAG" ]; then echo "Disk drive in use!" df -h exit fi } function makepartitions() { sfdisk -d /dev/$DISK0 > $PART_TABLE sfdisk /dev/$DISK1 < $PART_TABLE >> $LOG.$$ while read line do part=`echo $line | awk '{print $1}' | sed "s/$DISK0/$DISK1/g"` pn=`echo $line | awk '{print $2}'` mpoint="/mnt/$DISK1$pn" echo "========== Processing: $part $mpoint" if [ ! -d $mpoint ] then mkdir -p "$mpoint" >> $LOG.$$ fi mke2fs -m 2 $part >> $LOG.$$ tune2fs -j $part >> $LOG.$$ fsck $part >> $LOG.$$ mount $part $mpoint >> $LOG.$$ done < $PART_MOUNT SWAP=`sfdisk -l /dev/$DISK1 | awk "/swap/{print \$1}"` >> $LOG.$$ mkswap $SWAP >> $LOG.$$ # boot sector dd if=/dev/$DISK1 of=/dev/$DISK2 bs=512 count=1 } function disk_use() { if [ ! -e $PART_MOUNT ]; then df | grep ${DISK0} | awk '{print $1 " " $6}' > $PART_MOUNT fi [ ! -e $PART_MOUNT ] && echo "Cannot stat partitions. Missing $PART_MOUNT" echo "______________ Disk use: $DISK0 and $DISK1 ______________" > $DISK_USE while read line do x=`echo $line | awk '{print $1}'` y=`echo $x | sed "s/$DISK0/$DISK1/g"` echo "------------------------------------------------------------------" >> $DISK_USE df | grep $x >> $DISK_USE df | grep $y >> $DISK_USE done < $PART_MOUNT cat $DISK_USE } function unmount() { if [ -e $PART_MOUNT ]; then awk '{print NL++ " " $1 " " $6}' $PART_MOUNT | sort -r | awk '{print $2 " " $3}' > $PART_MOUNT.$$ while read line do part=`echo $line | awk '{print $1}' | sed "s/$DISK0/$DISK1/g"` pn=`echo $line | awk '{print $2}'` mpoint="/mnt/$DISK1$pn" echo "umount $part" umount $part done < $PART_MOUNT.$$ rm $PART_MOUNT.$$ else echo "Cannot unmount. Drive unmounted already or missing: $PART_MOUNT" fi } function warning() { cat << EOM ********************************************************************* * WARNING: you are about to change partition table on drive $DISK1 * Changing the partion will destroy ALL data on the disk! * * Press Return to continue or Ctrl-C to abort ********************************************************************* EOM read ask } case "$1" in -c) ismounted compare_disks warning echo "========== Started: $START ==========" > $LOG df | grep ${DISK0} | awk '{print $1 " " $6}' > $PART_MOUNT makepartitions TIME=`date '+%Y-%m-%d %H:%M'` echo "==== Partition done: $TIME ==========" >> $LOG (cd /;tar cfp - bin boot etc dev home lib root sbin usr var)|(cd /mnt/$DISK1;tar xvfp -) STOP=`date '+%Y-%m-%d %H:%M'` echo "========== Finished: $STOP ==========" >> $LOG echo -e "\n========== Partition table ==========\n" >> $LOG cat $PART_TABLE >> $LOG cat $LOG.$$ >> $LOG rm $LOG.$$ ;; -d) compare_disks ;; -s) disk_use ;; -u) unmount ;; *) cat << EOM ============================================================== Usage: $0 [-c | -d | -s | -u] -c (clone disk) -d (compare disk drives) -s (compare disk use between the drives) -u (unmount partitions) Current settings Original disk: $DISK0 Destination Disk: $DISK1 To select different disk edit this script, change variables DISK0 and DISK1 ============================================================== EOM exit ;; esac