home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s053 / 5.ddi / usr / bin / .restore < prev    next >
Encoding:
Text File  |  1990-12-08  |  5.9 KB  |  279 lines

  1. #!/sbin/sh
  2. #    Copyright (c) 1990 UNIX System Laboratories, Inc.
  3. #    Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
  4. #      All Rights Reserved
  5.  
  6. #    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
  7. #    UNIX System Laboratories, Inc.
  8. #    The copyright notice above does not evidence any
  9. #    actual or intended publication of such source code.
  10.  
  11. #ident    "@(#)/usr/bin/.restore.sl 1.1 4.0 12/08/90 41671 AT&T-USL"
  12.  
  13. # PURPOSE: Restore ${CPIO} files from floppy disk
  14. # ---------------------------------------------------------------------
  15. #    Options:
  16. #    c - complete restore
  17. #    d - device; defaults to /dev/rdsk/f0
  18. #    t - tape device being used
  19. #    o - overwrite files
  20. #    i - index file
  21.  
  22. USAGE="restore [ -d <device> ] [ -c ] [ -i ] [ -o ] [ -t ] [pattern [pattern] ...]"
  23. MEDIA="floppy disk"
  24. CPIO=cpio
  25. CPIOFLGS="cik"
  26. BLKSIZE="-B"
  27. DEV=""
  28. PATTERNS=
  29. SFLAG=
  30. overwrite=false
  31.  
  32. trap "cd /; rm -f /tmp/R.chk$$ /tmp/R.err$$ /tmp/R.ovw$$ /tmp/cplst$$ /tmp/flp_index;\
  33. echo \"You have cancelled the restore.\"; exit 1 " 1 2 3 9 15
  34. type=c
  35. while getopts d:itcos c
  36. do
  37.     case $c in
  38.     c)    type=c
  39.         ;;
  40.     d)    DEV="$OPTARG"
  41.         if [ "/dev/rmt" = "`dirname ${DEV}`" ]
  42.         then
  43.             MEDIA="cartridge tape"
  44.             BLKSIZE=" -C 102400 "
  45.         fi
  46.         ;;
  47.     t)    MEDIA="cartridge tape"
  48.         BLKSIZE=" -C 102400 "
  49.         ;;
  50.     o)    CPIOFLGS="u$CPIOFLGS"
  51.         overwrite=true
  52.         ;;
  53.     i)    type=i
  54.         ;;
  55.     s)    SFLAG=s
  56.         ;;
  57.     \?)    echo "restore: $USAGE" >&2
  58.         exit 0
  59.         ;;
  60.     *)    echo "restore: $USAGE" >&2
  61.         exit 1
  62.         ;;
  63.     esac
  64. done
  65. CPIOFLGS="$CPIOFLGS $BLKSIZE"
  66. shift `expr $OPTIND - 1`
  67.  
  68. while [ -n "$1" ]
  69. do
  70.     PATTERNS="\"$1\" $PATTERNS"
  71.     shift
  72. done
  73.  
  74. #
  75. # If user is root, then raise the ulimit
  76. #
  77.  
  78. ULIMIT=0
  79. id | grep "uid=0" > /dev/null
  80. if [ $? -eq 0 ]
  81. then
  82.     ULIMIT=`ulimit`
  83.     ulimit 1048571
  84. fi
  85.  
  86. if [ ! "$DEV" ]
  87. then
  88.     if [ "$MEDIA" = "cartridge tape" ]
  89.     then
  90.         DEV="/dev/rmt/c0s0"
  91.     elif [ -c "/dev/rdsk/f1" ]
  92.     then
  93.         /sbin/flop_num
  94.         if [ $? = 1 ]
  95.         then
  96.             DEV="/dev/rdsk/f0"
  97.         else
  98.             while (true)
  99.             do
  100.                 echo "\nThe system has two floppy drives."  >&2
  101.                 echo "Strike ENTER to restore from drive 0" >&2
  102.                 echo "or 1 to restore from drive 1.  \c"    >&2
  103.  
  104.                 read ans
  105.                 if [ "x$ans" = "x0" -o "x$ans" = "x" ]
  106.                 then
  107.                     DEV="/dev/rdsk/f0"
  108.                     break
  109.                 elif [ "x$ans" = "x1" ]
  110.                 then
  111.                     DEV="/dev/rdsk/f1"
  112.                     break
  113.                 else
  114.                     continue
  115.                 fi
  116.             done
  117.         fi
  118.     else
  119.         DEV="/dev/rdsk/f0"
  120.     fi
  121. fi
  122.  
  123. MESSAGE="You may remove the archive for the input device. To exit, strike 'q' followed
  124. by ENTER.  To continue, insert the next archive and strike the ENTER key. "
  125.  
  126. case $type in
  127. c )
  128.     if [ "$SFLAG" = s ]
  129.     then
  130.         message -u "Be sure the first archive volume is inserted." >&2
  131.     else
  132.         message -u "Insert the backup archive in the input device." >&2
  133.     fi
  134.     echo "            Restore in progress\n\n" >&2
  135.  
  136.     trap '' 1 2 3 9 15
  137.     eval $CPIO -dmv${CPIOFLGS} -M \"'$MESSAGE'\" -I "$DEV" $PATTERNS 2>/tmp/R.err$$ | tee /tmp/R.chk$$ 
  138.     err=$?
  139.     trap 1 2 3 9 15
  140.     if [ $err -eq 4 ]
  141.     then
  142.         echo "You have cancelled the restore" >&2
  143.  
  144.         #
  145.         # lower ulimit to it's original value if it was raised
  146.         #
  147.         
  148.         if [ ${ULIMIT} -ne 0 ]
  149.         then
  150.             ulimit ${ULIMIT}
  151.         fi
  152.         rm -f /tmp/R.chk$$ /tmp/R.err$$
  153.         exit 1
  154.     elif [ $err -ne 0 ]
  155.     then
  156.         if [ "$overwrite" != "true" ]
  157.         then
  158.             # overwrite was not chosen so errors may be of the form
  159.             # cpio: Existing "file" same age or newer
  160.             # grep these lines + the "2688 blocks" line + the
  161.             #    "312 error(s)" line out of R.err$$
  162.             # If there are any lines left then they must be errors
  163.             # This is messy but since cpio reports these as errors,
  164.             # this is the best we can do.
  165.             egrep -v '(same age or newer)|([0-9]* blocks)|([0-9]* error\(s\))' /tmp/R.err$$ > /tmp/R.ovw$$
  166.             ERR=`wc -l /tmp/R.ovw$$ | tr -s " " " " | cut -f2 -d" "`
  167.             if [ $ERR -gt 0 ]
  168.             then
  169.                 cat /tmp/R.ovw$$ >&2
  170.                 echo "$ERR error(s)\n\n" >&2
  171.                 echo "An error was encountered while reading from the input device." >&2
  172.                 echo "Please be sure the archive is inserted properly, and wait" >&2
  173.                 echo "for notification before removing it." >&2
  174.  
  175.                 #
  176.                 # lower ulimit to it's original value if it was raised
  177.                 #
  178.                 if [ ${ULIMIT} -ne 0 ]
  179.                 then
  180.                     ulimit ${ULIMIT}
  181.                 fi
  182.  
  183.                 rm -f /tmp/R.chk$$ /tmp/R.err$$ /tmp/R.ovw$$
  184.                 exit 1
  185.             else
  186.                 rm -f /tmp/R.chk$$ /tmp/R.err$$ /tmp/R.ovw$$
  187.                 message -d "The restore is now finished." >&2
  188.             fi
  189.         else
  190.             echo "An error was encountered while reading from the input device." >&2
  191.             echo "Please be sure the archive is inserted properly, and wait" >&2
  192.             echo "for notification before removing it." >&2
  193.             #
  194.             # lower ulimit to it's original value if it was raised
  195.             #
  196.             if [ ${ULIMIT} -ne 0 ]
  197.             then
  198.                 ulimit ${ULIMIT}
  199.             fi
  200.  
  201.             rm -f /tmp/R.chk$$ /tmp/R.err$$
  202.             exit 1
  203.         fi
  204.     else
  205.         NOMATCH=
  206.         for i in $PATTERNS
  207.         do
  208.             A=`echo "$i" | sed 's;\";;g
  209.                 s;\.;\\\.;g
  210.                 s;\*;\.\*;g'`
  211.             NUMERR=`cat /tmp/R.chk$$ | sed -n "\;$A;p" 2>/dev/null | wc -l`
  212.             if [ "$NUMERR" -eq 0 ] 
  213.             then
  214.                 NOMATCH="$NOMATCH\n\t$i"
  215.             fi
  216.         done
  217.         if [ -n "$NOMATCH" ]
  218.         then
  219.             echo "The following files and directories were not found.\n${NOMATCH}\n\n" | pg # >&2
  220.         fi
  221.         rm -f /tmp/R.chk$$ /tmp/R.err$$
  222.         message -d "The restore is now finished." >&2
  223.     fi
  224.  
  225.     #
  226.     # lower ulimit to it's original value if it was raised
  227.     #
  228.  
  229.     if [ ${ULIMIT} -ne 0 ]
  230.     then
  231.         ulimit ${ULIMIT}
  232.     fi
  233.     exit
  234.     ;;
  235. i)    
  236.     message -c "Insert the backup archive into the input device." >&2
  237.     if [ "$?" -ne 0 ]
  238.     then
  239.         echo "You have cancelled the restore" >&2
  240.  
  241.         #
  242.         # lower ulimit to it's original value if it was raised
  243.         #
  244.     
  245.         if [ ${ULIMIT} -ne 0 ]
  246.         then
  247.             ulimit ${ULIMIT}
  248.         fi
  249.         exit 1
  250.     fi
  251.     message -d "Reading the backup archive.\nPlease do not remove." >&2
  252.     trap '' 1 2 3 9 15
  253.     xtract "${CPIOFLGS}" /tmp/flp_index  $DEV > /tmp/cplst$$
  254.     cat /tmp/flp_index | sed '/^BLOCK/d' 
  255.     rm -f /tmp/cplst$$ /tmp/flp_index
  256.     ;;
  257. *)    echo "Invalid type $type" >&2
  258.     echo "restore: $USAGE" >&2
  259.     #
  260.     # lower ulimit to it's original value if it was raised
  261.     #
  262.  
  263.     if [ ${ULIMIT} -ne 0 ]
  264.     then
  265.         ulimit ${ULIMIT}
  266.     fi
  267.     exit 1
  268.     ;;
  269. esac
  270.  
  271. #
  272. # lower ulimit to it's original value if it was raised
  273. #
  274.  
  275. if [ ${ULIMIT} -ne 0 ]
  276. then
  277.     ulimit ${ULIMIT}
  278. fi
  279.