Cover V12, i12
dec2003.tar

Listing 2 copyroot

  1  #!/usr/dt/bin/dtksh
  2  #
  3  # Listing 2: copyroot
  4  # Author: Michael Wang
  5  # This script creates a backup boot disk from primary.
  6  #
  7  # This script was based on an idea by co-worker Dave Singer circa 1996.
  8  # Both of us worked for Lucent, then AT&T Bell Labs, in Whippany, NJ.
  9  # Dave originally used the dd command but I rewrote it using ufsdump and
 10  # ufsrestore, and updated for Solaris Volume Manager.
 11  
 12  PATH=/usr/xpg4/bin:/usr/bin:/usr/sbin
 13  builtin -d uname
 14  
 15  my_name=${0##*/}
 16  my_dir=${0%/*}
 17  my_logdir=$my_dir/log/$my_name
 18  [[ -d $my_logdir ]] || mkdir -p $my_logdir
 19  my_logfile=$my_logdir/$my_name.$(date +"%m-%d_%H:%M")
 20  
 21  exec 1>$my_logfile 2>&1
 22  
 23  date 
 24  my_conf=$my_dir/$my_name.cf
 25  
 26  [[ -r $my_conf ]] || {
 27    print "The configuration file ($my_conf) does not exists."
 28    exit 10
 29  }
 30  
 31  . $my_conf
 32  p_boot=$(print $p_boot)
 33  b_boot=$(print $b_boot)
 34  : ${log_keep:=30}
 35  find $my_logdir -type f -mtime +$log_keep -print -exec rm {} \;
 36  
 37  [[ "$(uname -n)" == "$machine_name" ]] || {
 38    print "This script should be run on $machine_name only."
 39    exit 20
 40  }
 41  
 42  pipe="|"
 43  mount | egrep "^/copyroot | on (${b_boot// /$pipe}) " && {
 44    print "/copyroot or backup disk mounted already."
 45    exit 30
 46  }
 47  
 48  set -A pa -- $p_boot
 49  for (( slice = 0; slice <= ${#pa[@]}-1; slice++ )); do
 50    (( slice == 1 )) && continue
 51    [[ -n $backup_dir ]] && dump_file=$backup_dir/$slice.dump ||
 52                            dump_file="/dev/null"
 53    ufsdump 0f $dump_file ${pa[$slice]} 2>&1 < /dev/null | while read j; do
 54      print -r -- "$j" | egrep "\
 55  DUMP: Writing [0-9.]+ Kilobyte records|\
 56  DUMP: Date of (this|last) level 0 dump:|\
 57  DUMP: Dumping /dev/.* \(.*\) to|\
 58  DUMP: (Mapping|Dumping) \(Pass .*\) \[(regular files|directories)\]|\
 59  DUMP: Estimated [0-9.]+ blocks \([0-9.]+(MB|KB)\)|\
 60  DUMP: [0-9.]+ blocks \([0-9.]+(MB|KB)\) on .* at|\
 61  DUMP: [0-9.]+% done, finished in|\
 62  DUMP: DUMP IS DONE" && continue
 63      print -r -- "$j"
 64      print "Primary root filesystem has problems."
 65      exit 40
 66    done
 67  done
 68   
 69  # prtvtoc /dev/rdsk/c0t0d0s0 | fmthard -s - /dev/rdsk/c0t9d0s0
 70  
 71  set -A ba -- $b_boot
 72  [[ -d /copyroot ]] || mkdir /copyroot
 73  tab=$(print "\t")
 74  cd /
 75  
 76  for (( slice = 0; slice <= ${#ba[@]}-1; slice++ )); do
 77    (( slice == 1 )) && continue
 78    print y | newfs ${ba[$slice]}
 79    fsck -y ${ba[$slice]}
 80   
 81    mount ${ba[$slice]} /copyroot
 82    df -Pk /copyroot | grep "^${ba[$slice]} " || exit 60
 83  
 84    if [[ $dump_file == "/dev/null" ]]; then
 85      ufsdump 0f - ${pa[$slice]} | ( cd /copyroot && ufsrestore rf - )
 86    else
 87      ( cd /copyroot && ufsrestore rf $backup_dir/$slice.dump )
 88    fi
 89  
 90    (( slice == 0 )) && {
 91      for (( s=0; s <= ${#pa[@]}-1; s++ )); do
 92        pd=${pa[$s]} bd=${ba[$s]} pr=${pd/dsk/rdsk} br=${bd/dsk/rdsk}
 93        sed "s:$pd\([ $tab]\):$bd\1:;
 94             s:$pr\([ $tab]\):$br\1:;
 95            " /copyroot/etc/vfstab > /copyroot/etc/vfstab.tmp
 96        cp /copyroot/etc/vfstab.tmp /copyroot/etc/vfstab
 97      done
 98  
 99      grep "^rootdev:" /etc/system && {
100        sed "/^rootdev:/s:^:* :" /etc/system > /copyroot/etc/system
101      }
102    }
103  
104    umount /copyroot
105    fsck -y ${ba[$slice]}
106  done
107   
108  installboot /usr/platform/$(uname -i)/lib/fs/ufs/bootblk ${ba[0]/dsk/rdsk}
109   
110  rmdir /copyroot
111  date
112  print "copyroot finished on $(uname -n) successfully."
113  exit 0